mirror of
https://github.com/github/codeql.git
synced 2026-04-29 02:35:15 +02:00
C++: Extend tests for cpp/suspicious-allocation-size.
This commit is contained in:
@@ -2,3 +2,7 @@
|
||||
| test2.c:17:20:17:25 | call to malloc | Allocated memory (33 bytes) is not a multiple of the size of 'double' (8 bytes). |
|
||||
| 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). |
|
||||
| test2.c:33:20:33:25 | call to malloc | Allocated memory (20 bytes) is not a multiple of the size of 'double' (8 bytes). |
|
||||
| test2.c:82:23:82:28 | call to malloc | Allocated memory (135 bytes) is not a multiple of the size of 'MyVarStruct1' (8 bytes). |
|
||||
| test2.c:83:23:83:28 | call to malloc | Allocated memory (143 bytes) is not a multiple of the size of 'MyVarStruct2' (16 bytes). |
|
||||
| test2.c:84:23:84:28 | call to malloc | Allocated memory (135 bytes) is not a multiple of the size of 'MyVarStruct3' (8 bytes). |
|
||||
| test2.c:85:24:85:29 | call to malloc | Allocated memory (1159 bytes) is not a multiple of the size of 'MyFixedStruct' (1032 bytes). |
|
||||
|
||||
@@ -60,7 +60,7 @@ void test_union() {
|
||||
}
|
||||
|
||||
// --- custom allocators ---
|
||||
|
||||
|
||||
void *MyMalloc1(size_t size) { return malloc(size); }
|
||||
void *MyMalloc2(size_t size);
|
||||
|
||||
|
||||
@@ -44,7 +44,7 @@ void good1(void) {
|
||||
}
|
||||
|
||||
// --- custom allocators ---
|
||||
|
||||
|
||||
void *MyMalloc1(size_t size) { return malloc(size); }
|
||||
void *MyMalloc2(size_t size);
|
||||
|
||||
@@ -53,3 +53,34 @@ void customAllocatorTests()
|
||||
double *dptr1 = MyMalloc1(33); // BAD -- Not a multiple of sizeof(double) [NOT DETECTED]
|
||||
double *dptr2 = MyMalloc2(33); // BAD -- Not a multiple of sizeof(double) [NOT DETECTED]
|
||||
}
|
||||
|
||||
// --- variable length data structures ---
|
||||
|
||||
typedef unsigned char uint8_t;
|
||||
|
||||
typedef struct _MyVarStruct1 {
|
||||
size_t dataLen;
|
||||
uint8_t data[0];
|
||||
} MyVarStruct1;
|
||||
|
||||
typedef struct _MyVarStruct2 {
|
||||
size_t dataLen;
|
||||
uint8_t data[1];
|
||||
} MyVarStruct2;
|
||||
|
||||
typedef struct _MyVarStruct3 {
|
||||
size_t dataLen;
|
||||
uint8_t data[];
|
||||
} MyVarStruct3;
|
||||
|
||||
typedef struct _MyFixedStruct {
|
||||
size_t dataLen;
|
||||
uint8_t data[1024];
|
||||
} MyFixedStruct;
|
||||
|
||||
void varStructTests() {
|
||||
MyVarStruct1 *a = malloc(sizeof(MyVarStruct1) + 127); // GOOD [FALSE POSITIVE]
|
||||
MyVarStruct2 *b = malloc(sizeof(MyVarStruct2) + 127); // GOOD [FALSE POSITIVE]
|
||||
MyVarStruct3 *c = malloc(sizeof(MyVarStruct3) + 127); // GOOD [FALSE POSITIVE]
|
||||
MyFixedStruct *d = malloc(sizeof(MyFixedStruct) + 127); // BAD --- Not a multiple of sizeof(MyFixedStruct)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user