Skip to content

Commit abb88e3

Browse files
committed
C++: Add a test file that was internal (results as on main).
1 parent c41add8 commit abb88e3

File tree

2 files changed

+80
-0
lines changed

2 files changed

+80
-0
lines changed

cpp/ql/test/query-tests/Security/CWE/CWE-120/semmle/tests/VeryLikelyOverrunWrite.expected

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,3 +19,14 @@
1919
| unions.c:26:2:26:7 | call to strcpy | This 'call to strcpy' operation requires 21 bytes but the destination is only 16 bytes. |
2020
| unions.c:27:2:27:7 | call to strcpy | This 'call to strcpy' operation requires 21 bytes but the destination is only 16 bytes. |
2121
| 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. |
22+
| varbuffer.c:15:5:15:10 | call to strcpy | This 'call to strcpy' operation requires 2 bytes but the destination is only 1 bytes. |
23+
| varbuffer.c:16:5:16:10 | call to strcpy | This 'call to strcpy' operation requires 10 bytes but the destination is only 1 bytes. |
24+
| varbuffer.c:23:5:23:10 | call to strcpy | This 'call to strcpy' operation requires 12 bytes but the destination is only 11 bytes. |
25+
| varbuffer.c:24:5:24:10 | call to strcpy | This 'call to strcpy' operation requires 17 bytes but the destination is only 11 bytes. |
26+
| varbuffer.c:39:5:39:10 | call to strcpy | This 'call to strcpy' operation requires 3 bytes but the destination is only 2 bytes. |
27+
| varbuffer.c:40:5:40:10 | call to strcpy | This 'call to strcpy' operation requires 10 bytes but the destination is only 2 bytes. |
28+
| varbuffer.c:45:5:45:10 | call to strcpy | This 'call to strcpy' operation requires 10 bytes but the destination is only 2 bytes. |
29+
| varbuffer.c:46:5:46:10 | call to strcpy | This 'call to strcpy' operation requires 17 bytes but the destination is only 2 bytes. |
30+
| varbuffer.c:60:5:60:10 | call to strcpy | This 'call to strcpy' operation requires 2 bytes but the destination is only 1 bytes. |
31+
| varbuffer.c:61:5:61:10 | call to strcpy | This 'call to strcpy' operation requires 10 bytes but the destination is only 1 bytes. |
32+
| varbuffer.c:67:5:67:10 | call to strcpy | This 'call to strcpy' operation requires 17 bytes but the destination is only 11 bytes. |
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
// Further test cases for CWE-120.
2+
3+
typedef unsigned long size_t;
4+
5+
typedef struct _MyVarStruct {
6+
size_t len;
7+
char buffer[1]; // variable size buffer
8+
} MyVarStruct;
9+
10+
void testMyVarStruct()
11+
{
12+
MyVarStruct *ptr1 = (MyVarStruct*)malloc(sizeof(MyVarStruct));
13+
ptr1->len = 0;
14+
strcpy(ptr1->buffer, ""); // GOOD
15+
strcpy(ptr1->buffer, "1"); // BAD: length 2, but destination only has length 1
16+
strcpy(ptr1->buffer, "123456789"); // BAD: length 10, but destination only has length 1
17+
// ...
18+
19+
MyVarStruct *ptr2 = (MyVarStruct*)malloc(sizeof(MyVarStruct) + (sizeof(char) * 10));
20+
ptr2->len = 10;
21+
strcpy(ptr2->buffer, "123456789"); // GOOD
22+
strcpy(ptr2->buffer, "1234567890"); // GOOD
23+
strcpy(ptr2->buffer, "1234567890a"); // BAD: length 12, but destination only has length 11
24+
strcpy(ptr2->buffer, "1234567890abcdef"); // BAD: length 17, but destination only has length 11
25+
// ...
26+
}
27+
28+
typedef struct MyFixedStruct1 {
29+
int len;
30+
char buffer[2]; // assumed to be a fixed size buffer
31+
} MyFixedStruct1;
32+
33+
void testMyFixedStruct()
34+
{
35+
MyFixedStruct1 *ptr1 = (MyFixedStruct1 *)malloc(sizeof(MyFixedStruct1));
36+
ptr1->len = 1;
37+
strcpy(ptr1->buffer, ""); // GOOD
38+
strcpy(ptr1->buffer, "1"); // GOOD
39+
strcpy(ptr1->buffer, "12"); // BAD: length 3, but destination only has length 2
40+
strcpy(ptr1->buffer, "123456789"); // BAD: length 10, but destination only has length 2
41+
// ...
42+
43+
MyFixedStruct1 *ptr2 = (MyFixedStruct1*)malloc(sizeof(MyFixedStruct1) + (sizeof(char) * 10));
44+
ptr2->len = 11;
45+
strcpy(ptr2->buffer, "123456789"); // BAD / DUBIOUS: length 10, but destination only has length 2
46+
strcpy(ptr2->buffer, "1234567890abcdef"); // BAD: length 17, but destination only has length 2
47+
// ...
48+
}
49+
50+
typedef struct _MyFixedStruct2 {
51+
char buffer[1]; // fixed size buffer
52+
size_t len;
53+
} MyFixedStruct2;
54+
55+
void testMyFixedStruct2()
56+
{
57+
MyFixedStruct2 *ptr1 = (MyFixedStruct2 *)malloc(sizeof(MyFixedStruct2));
58+
ptr1->len = 1;
59+
strcpy(ptr1->buffer, ""); // GOOD
60+
strcpy(ptr1->buffer, "1"); // BAD: length 2, but destination only has length 1
61+
strcpy(ptr1->buffer, "123456789"); // BAD: length 10, but destination only has length 1
62+
// ...
63+
64+
MyFixedStruct2 *ptr2 = (MyFixedStruct2*)malloc(sizeof(MyFixedStruct2) + (sizeof(char) * 10));
65+
ptr2->len = 11;
66+
strcpy(ptr2->buffer, "123456789"); // BAD: length 10, but destination only has length 1 [NOT DETECTED]
67+
strcpy(ptr2->buffer, "1234567890abcdef"); // BAD: length 17, but destination only has length 1
68+
// ...
69+
}

0 commit comments

Comments
 (0)