File tree Expand file tree Collapse file tree 1 file changed +18
-3
lines changed
cpp/ql/lib/semmle/code/cpp/commons Expand file tree Collapse file tree 1 file changed +18
-3
lines changed Original file line number Diff line number Diff line change @@ -54,13 +54,28 @@ private int isSource(Expr bufferExpr, Element why) {
54
54
result = bufferExpr .( AllocationExpr ) .getSizeBytes ( ) and
55
55
why = bufferExpr
56
56
or
57
- exists ( Type bufferType |
57
+ exists ( Type bufferType , Variable v |
58
+ v = why and
58
59
// buffer is the address of a variable
59
60
why = bufferExpr .( AddressOfExpr ) .getAddressable ( ) and
60
- bufferType = why .( Variable ) .getUnspecifiedType ( ) and
61
- result = bufferType .getSize ( ) and
61
+ bufferType = v .getUnspecifiedType ( ) and
62
62
not bufferType instanceof ReferenceType and
63
63
not any ( Union u ) .getAMemberVariable ( ) = why
64
+ |
65
+ not v instanceof Field and
66
+ result = bufferType .getSize ( )
67
+ or
68
+ // If it's an address of a field (i.e., a non-static member variable)
69
+ // then it's okay to use that address to access the other member variables.
70
+ // For example, this is okay:
71
+ // ```
72
+ // struct S { uint8_t a, b, c; };
73
+ // S s;
74
+ // memset(&s.a, 0, sizeof(S) - offsetof(S, a));
75
+ exists ( Field f |
76
+ v = f and
77
+ result = f .getDeclaringType ( ) .getSize ( ) - f .getByteOffset ( )
78
+ )
64
79
)
65
80
or
66
81
exists ( Union bufferType |
You can’t perform that action at this time.
0 commit comments