Skip to content

Commit 13a5249

Browse files
committed
C++: Enforce uniqueness in the other branch of 'getSize'.
1 parent 91752e5 commit 13a5249

File tree

1 file changed

+26
-24
lines changed

1 file changed

+26
-24
lines changed

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

Lines changed: 26 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -81,30 +81,32 @@ private int getSize(VariableAccess va) {
8181
not v instanceof Field and
8282
result = getVariableSize(v)
8383
or
84-
exists(Class c, int trueSize |
85-
// Otherwise, we find the "outermost" object and compute the size
86-
// as the difference between the size of the type of the "outermost
87-
// object" and the offset of the field relative to that type.
88-
// For example, consider the following structs:
89-
// ```
90-
// struct S {
91-
// uint32_t x;
92-
// uint32_t y;
93-
// };
94-
// struct S2 {
95-
// S s;
96-
// uint32_t z;
97-
// };
98-
// ```
99-
// Given an object `S2 s2` the size of the buffer `&s2.s.y`
100-
// is the size of the base object type (i.e., `S2`) minutes the offset
101-
// of `y` relative to the type `S2` (i.e., `4`). So the size of the
102-
// buffer is `12 - 4 = 8`.
103-
c = getRootType(va) and
104-
// we calculate the size based on the last field, to avoid including any padding after it
105-
trueSize = max(Field f | | f.getOffsetInClass(c) + getVariableSize(f)) and
106-
result = trueSize - v.(Field).getOffsetInClass(c)
107-
)
84+
result =
85+
unique(Class c, int trueSize |
86+
// Otherwise, we find the "outermost" object and compute the size
87+
// as the difference between the size of the type of the "outermost
88+
// object" and the offset of the field relative to that type.
89+
// For example, consider the following structs:
90+
// ```
91+
// struct S {
92+
// uint32_t x;
93+
// uint32_t y;
94+
// };
95+
// struct S2 {
96+
// S s;
97+
// uint32_t z;
98+
// };
99+
// ```
100+
// Given an object `S2 s2` the size of the buffer `&s2.s.y`
101+
// is the size of the base object type (i.e., `S2`) minutes the offset
102+
// of `y` relative to the type `S2` (i.e., `4`). So the size of the
103+
// buffer is `12 - 4 = 8`.
104+
c = getRootType(va) and
105+
// we calculate the size based on the last field, to avoid including any padding after it
106+
trueSize = max(Field f | | f.getOffsetInClass(c) + getVariableSize(f))
107+
|
108+
trueSize - v.(Field).getOffsetInClass(c)
109+
)
108110
)
109111
}
110112

0 commit comments

Comments
 (0)