Skip to content
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

C++: Improve and promote cpp/overflow-buffer #18837

Merged
merged 20 commits into from
Mar 3, 2025
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
C++: Add a test file that was internal (results as on main).
  • Loading branch information
geoffw0 committed Feb 26, 2025
commit abb88e3dba486b674d2f85d77f90e4314c6f5fd9
Original file line number Diff line number Diff line change
@@ -19,3 +19,14 @@
| unions.c:26:2:26:7 | call to strcpy | This 'call to strcpy' operation requires 21 bytes but the destination is only 16 bytes. |
| unions.c:27:2:27:7 | call to strcpy | This 'call to strcpy' operation requires 21 bytes but the destination is only 16 bytes. |
| var_size_struct.cpp:22:3:22:8 | call to strcpy | This 'call to strcpy' operation requires 10 bytes but the destination is only 9 bytes. |
| varbuffer.c:15:5:15:10 | call to strcpy | This 'call to strcpy' operation requires 2 bytes but the destination is only 1 bytes. |
| varbuffer.c:16:5:16:10 | call to strcpy | This 'call to strcpy' operation requires 10 bytes but the destination is only 1 bytes. |
| varbuffer.c:23:5:23:10 | call to strcpy | This 'call to strcpy' operation requires 12 bytes but the destination is only 11 bytes. |
| varbuffer.c:24:5:24:10 | call to strcpy | This 'call to strcpy' operation requires 17 bytes but the destination is only 11 bytes. |
| varbuffer.c:39:5:39:10 | call to strcpy | This 'call to strcpy' operation requires 3 bytes but the destination is only 2 bytes. |
| varbuffer.c:40:5:40:10 | call to strcpy | This 'call to strcpy' operation requires 10 bytes but the destination is only 2 bytes. |
| varbuffer.c:45:5:45:10 | call to strcpy | This 'call to strcpy' operation requires 10 bytes but the destination is only 2 bytes. |
| varbuffer.c:46:5:46:10 | call to strcpy | This 'call to strcpy' operation requires 17 bytes but the destination is only 2 bytes. |
| varbuffer.c:60:5:60:10 | call to strcpy | This 'call to strcpy' operation requires 2 bytes but the destination is only 1 bytes. |
| varbuffer.c:61:5:61:10 | call to strcpy | This 'call to strcpy' operation requires 10 bytes but the destination is only 1 bytes. |
| varbuffer.c:67:5:67:10 | call to strcpy | This 'call to strcpy' operation requires 17 bytes but the destination is only 11 bytes. |
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
// Further test cases for CWE-120.

typedef unsigned long size_t;

typedef struct _MyVarStruct {
size_t len;
char buffer[1]; // variable size buffer
} MyVarStruct;

void testMyVarStruct()
{
MyVarStruct *ptr1 = (MyVarStruct*)malloc(sizeof(MyVarStruct));
ptr1->len = 0;
strcpy(ptr1->buffer, ""); // GOOD
strcpy(ptr1->buffer, "1"); // BAD: length 2, but destination only has length 1
strcpy(ptr1->buffer, "123456789"); // BAD: length 10, but destination only has length 1
// ...

MyVarStruct *ptr2 = (MyVarStruct*)malloc(sizeof(MyVarStruct) + (sizeof(char) * 10));
ptr2->len = 10;
strcpy(ptr2->buffer, "123456789"); // GOOD
strcpy(ptr2->buffer, "1234567890"); // GOOD
strcpy(ptr2->buffer, "1234567890a"); // BAD: length 12, but destination only has length 11
strcpy(ptr2->buffer, "1234567890abcdef"); // BAD: length 17, but destination only has length 11
// ...
}

typedef struct MyFixedStruct1 {
int len;
char buffer[2]; // assumed to be a fixed size buffer
} MyFixedStruct1;

void testMyFixedStruct()
{
MyFixedStruct1 *ptr1 = (MyFixedStruct1 *)malloc(sizeof(MyFixedStruct1));
ptr1->len = 1;
strcpy(ptr1->buffer, ""); // GOOD
strcpy(ptr1->buffer, "1"); // GOOD
strcpy(ptr1->buffer, "12"); // BAD: length 3, but destination only has length 2
strcpy(ptr1->buffer, "123456789"); // BAD: length 10, but destination only has length 2
// ...

MyFixedStruct1 *ptr2 = (MyFixedStruct1*)malloc(sizeof(MyFixedStruct1) + (sizeof(char) * 10));
ptr2->len = 11;
strcpy(ptr2->buffer, "123456789"); // BAD / DUBIOUS: length 10, but destination only has length 2
strcpy(ptr2->buffer, "1234567890abcdef"); // BAD: length 17, but destination only has length 2
// ...
}

typedef struct _MyFixedStruct2 {
char buffer[1]; // fixed size buffer
size_t len;
} MyFixedStruct2;

void testMyFixedStruct2()
{
MyFixedStruct2 *ptr1 = (MyFixedStruct2 *)malloc(sizeof(MyFixedStruct2));
ptr1->len = 1;
strcpy(ptr1->buffer, ""); // GOOD
strcpy(ptr1->buffer, "1"); // BAD: length 2, but destination only has length 1
strcpy(ptr1->buffer, "123456789"); // BAD: length 10, but destination only has length 1
// ...

MyFixedStruct2 *ptr2 = (MyFixedStruct2*)malloc(sizeof(MyFixedStruct2) + (sizeof(char) * 10));
ptr2->len = 11;
strcpy(ptr2->buffer, "123456789"); // BAD: length 10, but destination only has length 1 [NOT DETECTED]
strcpy(ptr2->buffer, "1234567890abcdef"); // BAD: length 17, but destination only has length 1
// ...
}