Skip to content

Commit d76ce4f

Browse files
committed
C++: Also handle reference types when computing 'trueSize'.
1 parent 26be983 commit d76ce4f

File tree

1 file changed

+15
-7
lines changed

1 file changed

+15
-7
lines changed

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

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -57,19 +57,27 @@ private Class getRootType(FieldAccess fa) {
5757
)
5858
}
5959

60+
/**
61+
* Gets the size of `v`. This predicate does not have a result when the
62+
* unspecified type of `v` is a `ReferenceType`.
63+
*/
64+
private int getVariableSize(Variable v) {
65+
exists(Type t |
66+
t = v.getUnspecifiedType() and
67+
not t instanceof ReferenceType and
68+
result = t.getSize()
69+
)
70+
}
71+
6072
/**
6173
* Gets the size of the buffer access at `va`.
6274
*/
6375
private int getSize(VariableAccess va) {
6476
exists(Variable v | va.getTarget() = v |
6577
// If `v` is not a field then the size of the buffer is just
6678
// the size of the type of `v`.
67-
exists(Type t |
68-
t = v.getUnspecifiedType() and
69-
not v instanceof Field and
70-
not t instanceof ReferenceType and
71-
result = t.getSize()
72-
)
79+
not v instanceof Field and
80+
result = getVariableSize(v)
7381
or
7482
exists(Class c, int trueSize |
7583
// Otherwise, we find the "outermost" object and compute the size
@@ -92,7 +100,7 @@ private int getSize(VariableAccess va) {
92100
// buffer is `12 - 4 = 8`.
93101
c = getRootType(va) and
94102
// we calculate the size based on the last field, to avoid including any padding after it
95-
trueSize = max(Field f | | f.getOffsetInClass(c) + f.getUnspecifiedType().getSize()) and
103+
trueSize = max(Field f | | f.getOffsetInClass(c) + getVariableSize(f)) and
96104
result = trueSize - v.(Field).getOffsetInClass(c)
97105
)
98106
)

0 commit comments

Comments
 (0)