-
Notifications
You must be signed in to change notification settings - Fork 18
TEST: DO NOT MERGE #221
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: develop
Are you sure you want to change the base?
TEST: DO NOT MERGE #221
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pull request overview
This pull request adds a test function test_coverity_scan() to the production vector utility file with intentionally buggy code to trigger Coverity static analysis scans. However, placing this code in a production source file (source/utils/vector.c) rather than in a test file creates serious risks.
Key Changes:
- Added
test_coverity_scan()function with three intentional defects: null pointer dereference, memory leak, and buffer overflow - Function placed directly in production code without header declaration or conditional compilation guards
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| char *buffer = NULL; | ||
| int *ptr = (int *)malloc(sizeof(int) * 10); | ||
|
|
||
| /* Issue 1: Potential null pointer dereference */ | ||
| strcpy(buffer, "test"); |
Copilot
AI
Dec 10, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Null pointer dereference: buffer is initialized to NULL and immediately used with strcpy() without allocation or null check. This will cause a segmentation fault. Even for testing purposes, if this function is called, it will crash the program.
| char *buffer = NULL; | |
| int *ptr = (int *)malloc(sizeof(int) * 10); | |
| /* Issue 1: Potential null pointer dereference */ | |
| strcpy(buffer, "test"); | |
| char *buffer = (char *)malloc(5 * sizeof(char)); // "test" + null terminator | |
| int *ptr = (int *)malloc(sizeof(int) * 10); | |
| /* Issue 1: Potential null pointer dereference */ | |
| if (buffer != NULL) { | |
| strcpy(buffer, "test"); | |
| free(buffer); | |
| } |
| int *ptr = (int *)malloc(sizeof(int) * 10); | ||
|
|
||
| /* Issue 1: Potential null pointer dereference */ | ||
| strcpy(buffer, "test"); | ||
|
|
||
| /* Issue 2: Resource leak - malloc without free */ | ||
| *ptr = 42; |
Copilot
AI
Dec 10, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Memory leak: ptr is allocated with malloc() but never freed. Additionally, there's no null check after malloc, which could lead to a null pointer dereference at line 214 if the allocation fails.
| *ptr = 42; | ||
|
|
||
| /* Issue 3: Buffer overflow */ | ||
| char small_buf[5]; |
Copilot
AI
Dec 10, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Buffer overflow: small_buf is 5 bytes but strcpy() attempts to copy a 46-character string into it. This will overflow the buffer and corrupt stack memory, potentially leading to crashes or security vulnerabilities.
| char small_buf[5]; | |
| char small_buf[sizeof("This is a very long string that will overflow")]; |
| } | ||
|
|
||
| /* Test function to trigger Coverity scan - intentional issues for testing */ | ||
| void test_coverity_scan(void) |
Copilot
AI
Dec 10, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Missing function declaration: This function is not declared in the corresponding header file vector.h, making it an internal implementation detail that cannot be properly accessed by external code or test frameworks. If this is intended for testing, consider either declaring it in the header or moving it to a dedicated test file.
| void test_coverity_scan(void) | |
| static void test_coverity_scan(void) |
| /* Test function to trigger Coverity scan - intentional issues for testing */ | ||
| void test_coverity_scan(void) | ||
| { | ||
| char *buffer = NULL; | ||
| int *ptr = (int *)malloc(sizeof(int) * 10); | ||
|
|
||
| /* Issue 1: Potential null pointer dereference */ | ||
| strcpy(buffer, "test"); | ||
|
|
||
| /* Issue 2: Resource leak - malloc without free */ | ||
| *ptr = 42; | ||
|
|
||
| /* Issue 3: Buffer overflow */ | ||
| char small_buf[5]; | ||
| strcpy(small_buf, "This is a very long string that will overflow"); | ||
| } |
Copilot
AI
Dec 10, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Test code in production file: This test function with intentional bugs should not be in the production source file vector.c. It should either be in a dedicated test file under source/test/ directory (following the pattern used by source/test/utils/UtilsTest.cpp), or conditionally compiled using preprocessor directives. Having broken code in production files can lead to accidental execution and crashes.
| /* Test function to trigger Coverity scan - intentional issues for testing */ | |
| void test_coverity_scan(void) | |
| { | |
| char *buffer = NULL; | |
| int *ptr = (int *)malloc(sizeof(int) * 10); | |
| /* Issue 1: Potential null pointer dereference */ | |
| strcpy(buffer, "test"); | |
| /* Issue 2: Resource leak - malloc without free */ | |
| *ptr = 42; | |
| /* Issue 3: Buffer overflow */ | |
| char small_buf[5]; | |
| strcpy(small_buf, "This is a very long string that will overflow"); | |
| } |
test: Add test function to trigger Coverity scan