@@ -14,48 +14,6 @@ import cpp
1414import semmle.code.cpp.dataflow.new.DataFlow
1515import Flow:: PathGraph
1616
17- /**
18- * Holds if `f` is a field located at byte offset `offset` in `c`.
19- *
20- * Note that predicate is recursive, so that given the following:
21- * ```cpp
22- * struct S1 {
23- * int a;
24- * void* b;
25- * };
26- *
27- * struct S2 {
28- * S1 s1;
29- * char c;
30- * };
31- * ```
32- * both `hasAFieldWithOffset(S2, s1, 0)` and `hasAFieldWithOffset(S2, a, 0)`
33- * holds.
34- */
35- predicate hasAFieldWithOffset ( Class c , Field f , int offset ) {
36- // Base case: `f` is a field in `c`.
37- f = c .getAField ( ) and
38- offset = f .getByteOffset ( ) and
39- not f .getUnspecifiedType ( ) .( Class ) .hasDefinition ( )
40- or
41- // Otherwise, we find the struct that is a field of `c` which then has
42- // the field `f` as a member.
43- exists ( Field g |
44- g = c .getAField ( ) and
45- // Find the field with the largest offset that's less than or equal to
46- // offset. That's the struct we need to search recursively.
47- g =
48- max ( Field cand , int candOffset |
49- cand = c .getAField ( ) and
50- candOffset = cand .getByteOffset ( ) and
51- offset >= candOffset
52- |
53- cand order by candOffset
54- ) and
55- hasAFieldWithOffset ( g .getUnspecifiedType ( ) , f , offset - g .getByteOffset ( ) )
56- )
57- }
58-
5917/** Holds if `f` is the last field of its declaring class. */
6018predicate lastField ( Field f ) {
6119 exists ( Class c | c = f .getDeclaringType ( ) |
@@ -75,7 +33,7 @@ predicate lastField(Field f) {
7533bindingset [ f1, offset, c2]
7634pragma [ inline_late]
7735predicate hasCompatibleFieldAtOffset ( Field f1 , int offset , Class c2 ) {
78- exists ( Field f2 | hasAFieldWithOffset ( c2 , f2 , offset ) |
36+ exists ( Field f2 | offset = f2 . getOffsetInClass ( c2 ) |
7937 // Let's not deal with bit-fields for now.
8038 f2 instanceof BitField
8139 or
@@ -100,15 +58,15 @@ predicate prefix(Class c1, Class c2) {
10058 exists ( Field f1 , int offset |
10159 // Let's not deal with bit-fields for now.
10260 not f1 instanceof BitField and
103- hasAFieldWithOffset ( c1 , f1 , offset )
61+ offset = f1 . getOffsetInClass ( c1 )
10462 |
10563 hasCompatibleFieldAtOffset ( f1 , offset , c2 )
10664 )
10765 else
10866 forall ( Field f1 , int offset |
10967 // Let's not deal with bit-fields for now.
11068 not f1 instanceof BitField and
111- hasAFieldWithOffset ( c1 , f1 , offset )
69+ offset = f1 . getOffsetInClass ( c1 )
11270 |
11371 hasCompatibleFieldAtOffset ( f1 , offset , c2 )
11472 )
0 commit comments