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 2
2
| test2.c:17:20:17:25 | call to malloc | Allocated memory (33 bytes) is not a multiple of the size of 'double' (8 bytes). |
3
3
| 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). |
4
4
| 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() {
60
60
}
61
61
62
62
// --- custom allocators ---
63
-
63
+
64
64
void * MyMalloc1 (size_t size ) { return malloc (size ); }
65
65
void * MyMalloc2 (size_t size );
66
66
Original file line number Diff line number Diff line change @@ -44,7 +44,7 @@ void good1(void) {
44
44
}
45
45
46
46
// --- custom allocators ---
47
-
47
+
48
48
void * MyMalloc1 (size_t size ) { return malloc (size ); }
49
49
void * MyMalloc2 (size_t size );
50
50
@@ -53,3 +53,34 @@ void customAllocatorTests()
53
53
double * dptr1 = MyMalloc1 (33 ); // BAD -- Not a multiple of sizeof(double) [NOT DETECTED]
54
54
double * dptr2 = MyMalloc2 (33 ); // BAD -- Not a multiple of sizeof(double) [NOT DETECTED]
55
55
}
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