@@ -10,44 +10,11 @@ import semmle.code.cpp.dataflow.DataFlow
10
10
* char data[1]; // v
11
11
* };
12
12
* ```
13
- * This requires that `v` is an array of size 0 or 1, and `v` is the last member of `c`.
14
- * In addition, if the size of the structure is taken, there must be at least one instance
15
- * where a `c` pointer is allocated with additional space.
16
- * For example, holds for `c` if it occurs as
17
- * ```
18
- * malloc(sizeof(c) + 100 * sizeof(char))
19
- * ```
20
- * but not if it only ever occurs as
21
- * ```
22
- * malloc(sizeof(c))
23
- * ```
13
+ * This requires that `v` is an array of size 0 or 1.
24
14
*/
25
15
predicate memberMayBeVarSize ( Class c , MemberVariable v ) {
26
- exists ( int i |
27
- // `v` is the last field in `c`
28
- i = max ( int j | c .getCanonicalMember ( j ) instanceof Field | j ) and
29
- v = c .getCanonicalMember ( i ) and
30
- // v is an array of size at most 1
31
- v .getUnspecifiedType ( ) .( ArrayType ) .getArraySize ( ) <= 1 and
32
- not c instanceof Union
33
- ) and
34
- // If the size is taken, then arithmetic is performed on the result at least once
35
- (
36
- // `sizeof(c)` is not taken
37
- not exists ( SizeofOperator so |
38
- so .( SizeofTypeOperator ) .getTypeOperand ( ) .getUnspecifiedType ( ) = c or
39
- so .( SizeofExprOperator ) .getExprOperand ( ) .getUnspecifiedType ( ) = c
40
- )
41
- or
42
- // or `sizeof(c)` is taken
43
- exists ( SizeofOperator so |
44
- so .( SizeofTypeOperator ) .getTypeOperand ( ) .getUnspecifiedType ( ) = c or
45
- so .( SizeofExprOperator ) .getExprOperand ( ) .getUnspecifiedType ( ) = c
46
- |
47
- // and arithmetic is performed on the result
48
- so .getParent * ( ) instanceof AddExpr
49
- )
50
- )
16
+ c = v .getDeclaringType ( ) and
17
+ v .getUnspecifiedType ( ) .( ArrayType ) .getArraySize ( ) <= 1
51
18
}
52
19
53
20
/**
@@ -60,10 +27,6 @@ int getBufferSize(Expr bufferExpr, Element why) {
60
27
result = bufferVar .getUnspecifiedType ( ) .( ArrayType ) .getSize ( ) and
61
28
why = bufferVar and
62
29
not memberMayBeVarSize ( _, bufferVar ) and
63
- not exists ( Union bufferType |
64
- bufferType .getAMemberVariable ( ) = why and
65
- bufferVar .getUnspecifiedType ( ) .( ArrayType ) .getSize ( ) <= 1
66
- ) and
67
30
not result = 0 // zero sized arrays are likely to have special usage, for example
68
31
or
69
32
// behaving a bit like a 'union' overlapping other fields.
@@ -85,13 +48,6 @@ int getBufferSize(Expr bufferExpr, Element why) {
85
48
parentPtr .getTarget ( ) .getUnspecifiedType ( ) .( PointerType ) .getBaseType ( ) = parentClass and
86
49
result = getBufferSize ( parentPtr , _) + bufferVar .getType ( ) .getSize ( ) - parentClass .getSize ( )
87
50
)
88
- or
89
- exists ( Union bufferType |
90
- bufferType .getAMemberVariable ( ) = why and
91
- why = bufferVar and
92
- bufferVar .getUnspecifiedType ( ) .( ArrayType ) .getSize ( ) <= 1 and
93
- result = bufferType .getSize ( )
94
- )
95
51
)
96
52
or
97
53
// buffer is a fixed size dynamic allocation
0 commit comments