File tree Expand file tree Collapse file tree 3 files changed +16
-8
lines changed
test/query-tests/Critical/SizeCheck Expand file tree Collapse file tree 3 files changed +16
-8
lines changed Original file line number Diff line number Diff line change @@ -30,10 +30,20 @@ predicate baseType(AllocationExpr alloc, Type base) {
30
30
}
31
31
32
32
predicate decideOnSize ( Type t , int size ) {
33
- // If the codebase has more than one type with the same name, it can have more than one size.
33
+ // If the codebase has more than one type with the same name, it can have more than one size. For
34
+ // most purposes in this query, we use the smallest.
34
35
size = min ( t .getSize ( ) )
35
36
}
36
37
38
+ predicate mayHaveVarSize ( Type t ) {
39
+ // a member (normally at the end of the type) that looks like it may be intended have variable size.
40
+ exists ( MemberVariable mv , ArrayType at |
41
+ mv .getDeclaringType ( ) = t and
42
+ mv .getUnspecifiedType ( ) = at and
43
+ not at .getArraySize ( ) > 1
44
+ )
45
+ }
46
+
37
47
from AllocationExpr alloc , Type base , int basesize , int allocated
38
48
where
39
49
baseType ( alloc , base ) and
45
55
size = 0 or
46
56
( allocated / size ) * size = allocated
47
57
) and
48
- not basesize > allocated // covered by SizeCheck.ql
58
+ not basesize > allocated and // covered by SizeCheck.ql
59
+ not mayHaveVarSize ( base .getUnspecifiedType ( ) ) // exclude variable size types
49
60
select alloc ,
50
61
"Allocated memory (" + allocated .toString ( ) + " bytes) is not a multiple of the size of '" +
51
62
base .getName ( ) + "' (" + basesize .toString ( ) + " bytes)."
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
5
| 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 @@ -79,8 +79,8 @@ typedef struct _MyFixedStruct {
79
79
} MyFixedStruct ;
80
80
81
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]
82
+ MyVarStruct1 * a = malloc (sizeof (MyVarStruct1 ) + 127 ); // GOOD
83
+ MyVarStruct2 * b = malloc (sizeof (MyVarStruct2 ) + 127 ); // GOOD
84
+ MyVarStruct3 * c = malloc (sizeof (MyVarStruct3 ) + 127 ); // GOOD
85
85
MyFixedStruct * d = malloc (sizeof (MyFixedStruct ) + 127 ); // BAD --- Not a multiple of sizeof(MyFixedStruct)
86
86
}
You can’t perform that action at this time.
0 commit comments