Skip to content

Conversation

@saikumarsv
Copy link

test: Add test function to trigger Coverity scan

Copilot AI review requested due to automatic review settings December 10, 2025 04:57
@saikumarsv saikumarsv requested a review from a team as a code owner December 10, 2025 04:57
Copy link

Copilot AI left a 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.

Comment on lines +207 to +211
char *buffer = NULL;
int *ptr = (int *)malloc(sizeof(int) * 10);

/* Issue 1: Potential null pointer dereference */
strcpy(buffer, "test");
Copy link

Copilot AI Dec 10, 2025

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.

Suggested change
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);
}

Copilot uses AI. Check for mistakes.
Comment on lines +208 to +214
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;
Copy link

Copilot AI Dec 10, 2025

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.

Copilot uses AI. Check for mistakes.
*ptr = 42;

/* Issue 3: Buffer overflow */
char small_buf[5];
Copy link

Copilot AI Dec 10, 2025

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.

Suggested change
char small_buf[5];
char small_buf[sizeof("This is a very long string that will overflow")];

Copilot uses AI. Check for mistakes.
}

/* Test function to trigger Coverity scan - intentional issues for testing */
void test_coverity_scan(void)
Copy link

Copilot AI Dec 10, 2025

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.

Suggested change
void test_coverity_scan(void)
static void test_coverity_scan(void)

Copilot uses AI. Check for mistakes.
Comment on lines +204 to +219
/* 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");
}
Copy link

Copilot AI Dec 10, 2025

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.

Suggested change
/* 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 uses AI. Check for mistakes.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants