Skip to content

Commit d6054c9

Browse files
committed
C++: Infer larger buffer sizes for non-static member variables.
1 parent 1643a66 commit d6054c9

File tree

1 file changed

+18
-3
lines changed

1 file changed

+18
-3
lines changed

cpp/ql/lib/semmle/code/cpp/commons/Buffer.qll

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -54,13 +54,28 @@ private int isSource(Expr bufferExpr, Element why) {
5454
result = bufferExpr.(AllocationExpr).getSizeBytes() and
5555
why = bufferExpr
5656
or
57-
exists(Type bufferType |
57+
exists(Type bufferType, Variable v |
58+
v = why and
5859
// buffer is the address of a variable
5960
why = bufferExpr.(AddressOfExpr).getAddressable() and
60-
bufferType = why.(Variable).getUnspecifiedType() and
61-
result = bufferType.getSize() and
61+
bufferType = v.getUnspecifiedType() and
6262
not bufferType instanceof ReferenceType and
6363
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+
)
6479
)
6580
or
6681
exists(Union bufferType |

0 commit comments

Comments
 (0)