@@ -14,48 +14,6 @@ import cpp
14
14
import semmle.code.cpp.dataflow.new.DataFlow
15
15
import Flow:: PathGraph
16
16
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
-
59
17
/** Holds if `f` is the last field of its declaring class. */
60
18
predicate lastField ( Field f ) {
61
19
exists ( Class c | c = f .getDeclaringType ( ) |
@@ -75,7 +33,7 @@ predicate lastField(Field f) {
75
33
bindingset [ f1, offset, c2]
76
34
pragma [ inline_late]
77
35
predicate hasCompatibleFieldAtOffset ( Field f1 , int offset , Class c2 ) {
78
- exists ( Field f2 | hasAFieldWithOffset ( c2 , f2 , offset ) |
36
+ exists ( Field f2 | offset = f2 . getOffsetInClass ( c2 ) |
79
37
// Let's not deal with bit-fields for now.
80
38
f2 instanceof BitField
81
39
or
@@ -100,15 +58,15 @@ predicate prefix(Class c1, Class c2) {
100
58
exists ( Field f1 , int offset |
101
59
// Let's not deal with bit-fields for now.
102
60
not f1 instanceof BitField and
103
- hasAFieldWithOffset ( c1 , f1 , offset )
61
+ offset = f1 . getOffsetInClass ( c1 )
104
62
|
105
63
hasCompatibleFieldAtOffset ( f1 , offset , c2 )
106
64
)
107
65
else
108
66
forall ( Field f1 , int offset |
109
67
// Let's not deal with bit-fields for now.
110
68
not f1 instanceof BitField and
111
- hasAFieldWithOffset ( c1 , f1 , offset )
69
+ offset = f1 . getOffsetInClass ( c1 )
112
70
|
113
71
hasCompatibleFieldAtOffset ( f1 , offset , c2 )
114
72
)
0 commit comments