File tree Expand file tree Collapse file tree 3 files changed +37
-2
lines changed
cpp/ql/test/query-tests/Critical/SizeCheck Expand file tree Collapse file tree 3 files changed +37
-2
lines changed Original file line number Diff line number Diff line change 22| test2.c:17:20:17:25 | call to malloc | Allocated memory (33 bytes) is not a multiple of the size of 'double' (8 bytes). |
33| test2.c:32:23:32:28 | call to malloc | Allocated memory (28 bytes) is not a multiple of the size of 'long long' (8 bytes). |
44| test2.c:33:20:33:25 | call to malloc | Allocated memory (20 bytes) is not a multiple of the size of 'double' (8 bytes). |
5+ | test2.c:82:23:82:28 | call to malloc | Allocated memory (135 bytes) is not a multiple of the size of 'MyVarStruct1' (8 bytes). |
6+ | test2.c:83:23:83:28 | call to malloc | Allocated memory (143 bytes) is not a multiple of the size of 'MyVarStruct2' (16 bytes). |
7+ | test2.c:84:23:84:28 | call to malloc | Allocated memory (135 bytes) is not a multiple of the size of 'MyVarStruct3' (8 bytes). |
8+ | test2.c:85:24:85:29 | call to malloc | Allocated memory (1159 bytes) is not a multiple of the size of 'MyFixedStruct' (1032 bytes). |
Original file line number Diff line number Diff line change @@ -60,7 +60,7 @@ void test_union() {
6060}
6161
6262// --- custom allocators ---
63-
63+
6464void * MyMalloc1 (size_t size ) { return malloc (size ); }
6565void * MyMalloc2 (size_t size );
6666
Original file line number Diff line number Diff line change @@ -44,7 +44,7 @@ void good1(void) {
4444}
4545
4646// --- custom allocators ---
47-
47+
4848void * MyMalloc1 (size_t size ) { return malloc (size ); }
4949void * MyMalloc2 (size_t size );
5050
@@ -53,3 +53,34 @@ void customAllocatorTests()
5353 double * dptr1 = MyMalloc1 (33 ); // BAD -- Not a multiple of sizeof(double) [NOT DETECTED]
5454 double * dptr2 = MyMalloc2 (33 ); // BAD -- Not a multiple of sizeof(double) [NOT DETECTED]
5555}
56+
57+ // --- variable length data structures ---
58+
59+ typedef unsigned char uint8_t ;
60+
61+ typedef struct _MyVarStruct1 {
62+ size_t dataLen ;
63+ uint8_t data [0 ];
64+ } MyVarStruct1 ;
65+
66+ typedef struct _MyVarStruct2 {
67+ size_t dataLen ;
68+ uint8_t data [1 ];
69+ } MyVarStruct2 ;
70+
71+ typedef struct _MyVarStruct3 {
72+ size_t dataLen ;
73+ uint8_t data [];
74+ } MyVarStruct3 ;
75+
76+ typedef struct _MyFixedStruct {
77+ size_t dataLen ;
78+ uint8_t data [1024 ];
79+ } MyFixedStruct ;
80+
81+ void varStructTests () {
82+ MyVarStruct1 * a = malloc (sizeof (MyVarStruct1 ) + 127 ); // GOOD [FALSE POSITIVE]
83+ MyVarStruct2 * b = malloc (sizeof (MyVarStruct2 ) + 127 ); // GOOD [FALSE POSITIVE]
84+ MyVarStruct3 * c = malloc (sizeof (MyVarStruct3 ) + 127 ); // GOOD [FALSE POSITIVE]
85+ MyFixedStruct * d = malloc (sizeof (MyFixedStruct ) + 127 ); // BAD --- Not a multiple of sizeof(MyFixedStruct)
86+ }
You can’t perform that action at this time.
0 commit comments