Skip to content

Commit 45e92ce

Browse files
committed
C++: Extend tests for cpp/suspicious-allocation-size.
1 parent a63e70d commit 45e92ce

File tree

3 files changed

+37
-2
lines changed

3 files changed

+37
-2
lines changed

cpp/ql/test/query-tests/Critical/SizeCheck/SizeCheck2.expected

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,7 @@
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). |

cpp/ql/test/query-tests/Critical/SizeCheck/test.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ void test_union() {
6060
}
6161

6262
// --- custom allocators ---
63-
63+
6464
void *MyMalloc1(size_t size) { return malloc(size); }
6565
void *MyMalloc2(size_t size);
6666

cpp/ql/test/query-tests/Critical/SizeCheck/test2.c

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ void good1(void) {
4444
}
4545

4646
// --- custom allocators ---
47-
47+
4848
void *MyMalloc1(size_t size) { return malloc(size); }
4949
void *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+
}

0 commit comments

Comments
 (0)