Skip to content

Commit a91bcb9

Browse files
Update EXP39-C to use the new dataflow library
1 parent e7d90ba commit a91bcb9

3 files changed

Lines changed: 60 additions & 54 deletions

File tree

‎c/cert/src/rules/EXP39-C/DoNotAccessVariableViaPointerOfIncompatibleType.ql‎

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818

1919
import cpp
2020
import codingstandards.c.cert
21-
import semmle.code.cpp.dataflow.DataFlow
21+
import semmle.code.cpp.dataflow.new.DataFlow
2222
import semmle.code.cpp.controlflow.Dominance
2323
import IndirectCastFlow::PathGraph
2424

@@ -35,12 +35,12 @@ class MemsetFunction extends Function {
3535
}
3636
}
3737

38-
class IndirectCastAnalysisUnconvertedCastExpr extends Expr {
39-
IndirectCastAnalysisUnconvertedCastExpr() { this = any(Cast c).getUnconverted() }
38+
class CastedExpr extends Expr {
39+
CastedExpr() { this = any(Cast c).getExpr() }
4040
}
4141

42-
class IndirectCastAnalysisDereferenceSink extends Expr {
43-
IndirectCastAnalysisDereferenceSink() { dereferenced(this) }
42+
class DereferencedExpr extends Expr {
43+
DereferencedExpr() { dereferenced(this) }
4444
}
4545

4646
class ReallocationFunction extends AllocationFunction {
@@ -68,14 +68,14 @@ module IndirectCastConfig implements DataFlow::StateConfigSig {
6868

6969
predicate isSource(DataFlow::Node source, FlowState state) {
7070
state instanceof IndirectCastDefaultFlowState and
71-
source.asExpr() instanceof IndirectCastAnalysisUnconvertedCastExpr
71+
source.asExpr() instanceof CastedExpr
7272
}
7373

7474
predicate isSink(DataFlow::Node sink, FlowState state) {
75-
sink.asExpr() instanceof IndirectCastAnalysisUnconvertedCastExpr and
75+
sink.asExpr() instanceof CastedExpr and
7676
state instanceof IndirectCastDefaultFlowState
7777
or
78-
sink.asExpr() instanceof IndirectCastAnalysisDereferenceSink and
78+
sink.asExpr() instanceof DereferencedExpr and
7979
state instanceof IndirectCastReallocatedFlowState and
8080
// The memset call won't always have an edge to subsequent dereferences.
8181
//
@@ -193,17 +193,18 @@ Type compatibleTypes(Type type) {
193193
}
194194

195195
from
196-
IndirectCastFlow::PathNode source, IndirectCastFlow::PathNode sink, Cast cast, Type fromType,
197-
Type toType
196+
Expr sinkExpr, IndirectCastFlow::PathNode source, IndirectCastFlow::PathNode sink, Cast cast,
197+
Type fromType, Type toType
198198
where
199-
not isExcluded(sink.getNode().asExpr(),
200-
Pointers3Package::doNotAccessVariableViaPointerOfIncompatibleTypeQuery()) and
199+
not isExcluded(sinkExpr, Pointers3Package::doNotAccessVariableViaPointerOfIncompatibleTypeQuery()) and
201200
cast.getFile().compiledAsC() and
202201
IndirectCastFlow::flowPath(source, sink) and
203202
// include only sinks which are not a compatible type to the associated source
204203
source.getNode().asExpr() = cast.getUnconverted() and
205204
fromType = cast.getUnconverted().getType().(PointerType).getBaseType() and
206205
toType = sink.getNode().asExpr().getActualType().(PointerType).getBaseType() and
207-
not toType = compatibleTypes(fromType)
208-
select sink.getNode().asExpr().getUnconverted(), source, sink,
206+
not toType = compatibleTypes(fromType) and
207+
sinkExpr = sink.getNode().asExpr() and
208+
(sinkExpr instanceof CastedExpr or sinkExpr instanceof DereferencedExpr)
209+
select sinkExpr, source, sink,
209210
"Cast from " + fromType + " to " + toType + " results in an incompatible pointer base type."
Lines changed: 42 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1,67 +1,69 @@
1-
WARNING: module 'DataFlow' has been deprecated and may be removed in future (DoNotAccessVariableViaPointerOfIncompatibleType.ql:66,38-46)
2-
WARNING: module 'DataFlow' has been deprecated and may be removed in future (DoNotAccessVariableViaPointerOfIncompatibleType.ql:69,22-30)
3-
WARNING: module 'DataFlow' has been deprecated and may be removed in future (DoNotAccessVariableViaPointerOfIncompatibleType.ql:74,20-28)
4-
WARNING: module 'DataFlow' has been deprecated and may be removed in future (DoNotAccessVariableViaPointerOfIncompatibleType.ql:107,23-31)
5-
WARNING: module 'DataFlow' has been deprecated and may be removed in future (DoNotAccessVariableViaPointerOfIncompatibleType.ql:116,5-13)
6-
WARNING: module 'DataFlow' has been deprecated and may be removed in future (DoNotAccessVariableViaPointerOfIncompatibleType.ql:116,45-53)
7-
WARNING: module 'DataFlow' has been deprecated and may be removed in future (DoNotAccessVariableViaPointerOfIncompatibleType.ql:138,27-35)
81
edges
9-
| test.c:49:8:49:9 | s3 | test.c:50:8:50:9 | s1 | provenance | |
2+
| test.c:49:3:49:9 | ... = ... | test.c:50:8:50:9 | s1 | provenance | |
3+
| test.c:49:8:49:9 | s3 | test.c:49:3:49:9 | ... = ... | provenance | |
4+
| test.c:60:16:60:18 | E1A | test.c:60:16:60:18 | E1A | provenance | |
105
| test.c:60:16:60:18 | E1A | test.c:61:16:61:17 | e1 | provenance | |
11-
| test.c:60:16:60:18 | E1A | test.c:65:10:65:12 | & ... | provenance | |
12-
| test.c:68:22:68:22 | v | test.c:68:41:68:41 | v | provenance | |
6+
| test.c:68:22:68:22 | v | test.c:68:34:68:41 | v | provenance | |
137
| test.c:72:13:72:15 | & ... | test.c:68:22:68:22 | v | provenance | |
148
| test.c:74:13:74:15 | & ... | test.c:68:22:68:22 | v | provenance | |
15-
| test.c:97:32:97:37 | call to malloc | test.c:98:40:98:41 | s2 | provenance | |
16-
| test.c:97:32:97:37 | call to malloc | test.c:98:40:98:41 | s2 | provenance | |
17-
| test.c:98:32:98:38 | call to realloc | test.c:99:3:99:4 | s3 | provenance | |
18-
| test.c:98:32:98:38 | call to realloc | test.c:100:10:100:11 | s3 | provenance | |
19-
| test.c:98:40:98:41 | s2 | test.c:98:32:98:38 | call to realloc | provenance | Config |
9+
| test.c:97:19:97:56 | call to malloc | test.c:97:19:97:56 | call to malloc | provenance | |
10+
| test.c:97:19:97:56 | call to malloc | test.c:98:40:98:41 | s2 | provenance | |
11+
| test.c:97:19:97:56 | call to malloc | test.c:98:40:98:41 | s2 | provenance | |
12+
| test.c:98:19:98:61 | call to realloc | test.c:98:19:98:61 | call to realloc | provenance | |
13+
| test.c:98:19:98:61 | call to realloc | test.c:98:19:98:61 | call to realloc | provenance | |
14+
| test.c:98:19:98:61 | call to realloc | test.c:99:3:99:4 | s3 | provenance | |
15+
| test.c:98:19:98:61 | call to realloc | test.c:100:10:100:11 | s3 | provenance | |
16+
| test.c:98:40:98:41 | s2 | test.c:98:19:98:61 | call to realloc | provenance | Config |
2017
nodes
21-
| test.c:6:19:6:20 | & ... | semmle.label | & ... |
22-
| test.c:11:10:11:11 | & ... | semmle.label | & ... |
23-
| test.c:13:17:13:19 | & ... | semmle.label | & ... |
24-
| test.c:15:17:15:19 | & ... | semmle.label | & ... |
25-
| test.c:19:18:19:20 | & ... | semmle.label | & ... |
26-
| test.c:20:20:20:22 | & ... | semmle.label | & ... |
27-
| test.c:22:11:22:13 | & ... | semmle.label | & ... |
28-
| test.c:27:17:27:19 | & ... | semmle.label | & ... |
29-
| test.c:28:10:28:12 | & ... | semmle.label | & ... |
30-
| test.c:29:13:29:15 | & ... | semmle.label | & ... |
31-
| test.c:30:19:30:21 | & ... | semmle.label | & ... |
32-
| test.c:31:16:31:18 | & ... | semmle.label | & ... |
18+
| test.c:6:12:6:20 | & ... | semmle.label | & ... |
19+
| test.c:11:3:11:11 | & ... | semmle.label | & ... |
20+
| test.c:13:3:13:19 | & ... | semmle.label | & ... |
21+
| test.c:15:3:15:19 | & ... | semmle.label | & ... |
22+
| test.c:19:3:19:20 | & ... | semmle.label | & ... |
23+
| test.c:20:3:20:22 | & ... | semmle.label | & ... |
24+
| test.c:22:3:22:13 | & ... | semmle.label | & ... |
25+
| test.c:27:3:27:19 | & ... | semmle.label | & ... |
26+
| test.c:28:3:28:12 | & ... | semmle.label | & ... |
27+
| test.c:29:3:29:15 | & ... | semmle.label | & ... |
28+
| test.c:30:3:30:21 | & ... | semmle.label | & ... |
29+
| test.c:31:3:31:18 | & ... | semmle.label | & ... |
3330
| test.c:47:8:47:9 | s2 | semmle.label | s2 |
31+
| test.c:49:3:49:9 | ... = ... | semmle.label | ... = ... |
3432
| test.c:49:8:49:9 | s3 | semmle.label | s3 |
3533
| test.c:49:8:49:9 | s3 | semmle.label | s3 |
3634
| test.c:50:8:50:9 | s1 | semmle.label | s1 |
3735
| test.c:60:16:60:18 | E1A | semmle.label | E1A |
3836
| test.c:60:16:60:18 | E1A | semmle.label | E1A |
37+
| test.c:60:16:60:18 | E1A | semmle.label | E1A |
3938
| test.c:61:16:61:17 | e1 | semmle.label | e1 |
40-
| test.c:65:10:65:12 | & ... | semmle.label | & ... |
39+
| test.c:65:3:65:12 | & ... | semmle.label | & ... |
4140
| test.c:68:22:68:22 | v | semmle.label | v |
42-
| test.c:68:41:68:41 | v | semmle.label | v |
41+
| test.c:68:34:68:41 | v | semmle.label | v |
4342
| test.c:72:13:72:15 | & ... | semmle.label | & ... |
4443
| test.c:72:13:72:15 | & ... | semmle.label | & ... |
4544
| test.c:74:13:74:15 | & ... | semmle.label | & ... |
4645
| test.c:74:13:74:15 | & ... | semmle.label | & ... |
47-
| test.c:97:32:97:37 | call to malloc | semmle.label | call to malloc |
48-
| test.c:97:32:97:37 | call to malloc | semmle.label | call to malloc |
49-
| test.c:98:32:98:38 | call to realloc | semmle.label | call to realloc |
50-
| test.c:98:32:98:38 | call to realloc | semmle.label | call to realloc |
51-
| test.c:98:32:98:38 | call to realloc | semmle.label | call to realloc |
46+
| test.c:97:19:97:56 | call to malloc | semmle.label | call to malloc |
47+
| test.c:97:19:97:56 | call to malloc | semmle.label | call to malloc |
48+
| test.c:97:19:97:56 | call to malloc | semmle.label | call to malloc |
49+
| test.c:98:19:98:61 | call to realloc | semmle.label | call to realloc |
50+
| test.c:98:19:98:61 | call to realloc | semmle.label | call to realloc |
51+
| test.c:98:19:98:61 | call to realloc | semmle.label | call to realloc |
52+
| test.c:98:19:98:61 | call to realloc | semmle.label | call to realloc |
53+
| test.c:98:19:98:61 | call to realloc | semmle.label | call to realloc |
5254
| test.c:98:40:98:41 | s2 | semmle.label | s2 |
5355
| test.c:98:40:98:41 | s2 | semmle.label | s2 |
5456
| test.c:99:3:99:4 | s3 | semmle.label | s3 |
5557
| test.c:100:10:100:11 | s3 | semmle.label | s3 |
5658
subpaths
5759
#select
58-
| test.c:6:19:6:20 | & ... | test.c:6:19:6:20 | & ... | test.c:6:19:6:20 | & ... | Cast from float to int results in an incompatible pointer base type. |
59-
| test.c:11:10:11:11 | & ... | test.c:11:10:11:11 | & ... | test.c:11:10:11:11 | & ... | Cast from short[2] to int results in an incompatible pointer base type. |
60-
| test.c:13:17:13:19 | & ... | test.c:13:17:13:19 | & ... | test.c:13:17:13:19 | & ... | Cast from short[2] to short[4] results in an incompatible pointer base type. |
61-
| test.c:19:18:19:20 | & ... | test.c:19:18:19:20 | & ... | test.c:19:18:19:20 | & ... | Cast from char to signed char results in an incompatible pointer base type. |
62-
| test.c:30:19:30:21 | & ... | test.c:30:19:30:21 | & ... | test.c:30:19:30:21 | & ... | Cast from int to unsigned int results in an incompatible pointer base type. |
60+
| test.c:6:19:6:20 | & ... | test.c:6:12:6:20 | & ... | test.c:6:12:6:20 | & ... | Cast from float to int results in an incompatible pointer base type. |
61+
| test.c:11:10:11:11 | & ... | test.c:11:3:11:11 | & ... | test.c:11:3:11:11 | & ... | Cast from short[2] to int results in an incompatible pointer base type. |
62+
| test.c:13:17:13:19 | & ... | test.c:13:3:13:19 | & ... | test.c:13:3:13:19 | & ... | Cast from short[2] to short[4] results in an incompatible pointer base type. |
63+
| test.c:19:18:19:20 | & ... | test.c:19:3:19:20 | & ... | test.c:19:3:19:20 | & ... | Cast from char to signed char results in an incompatible pointer base type. |
64+
| test.c:30:19:30:21 | & ... | test.c:30:3:30:21 | & ... | test.c:30:3:30:21 | & ... | Cast from int to unsigned int results in an incompatible pointer base type. |
6365
| test.c:47:8:47:9 | s2 | test.c:47:8:47:9 | s2 | test.c:47:8:47:9 | s2 | Cast from (unnamed class/struct/union) to (unnamed class/struct/union) results in an incompatible pointer base type. |
6466
| test.c:49:8:49:9 | s3 | test.c:49:8:49:9 | s3 | test.c:49:8:49:9 | s3 | Cast from S1 to (unnamed class/struct/union) results in an incompatible pointer base type. |
6567
| test.c:50:8:50:9 | s1 | test.c:50:8:50:9 | s1 | test.c:50:8:50:9 | s1 | Cast from (unnamed class/struct/union) to S1 results in an incompatible pointer base type. |
66-
| test.c:68:41:68:41 | v | test.c:72:13:72:15 | & ... | test.c:68:41:68:41 | v | Cast from float to int results in an incompatible pointer base type. |
68+
| test.c:68:41:68:41 | v | test.c:72:13:72:15 | & ... | test.c:68:34:68:41 | v | Cast from float to int results in an incompatible pointer base type. |
6769
| test.c:99:3:99:4 | s3 | test.c:98:40:98:41 | s2 | test.c:99:3:99:4 | s3 | Cast from S2 to S3 results in an incompatible pointer base type. |
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
- `EXP39-C` - `DoNotAccessVariableViaPointerOfIncompatibleType.ql`
2+
- Updated the underlying dataflow library to use the new dataflow from the deprecated old data flow. This may cause behavioral changes in some analyses, usually with improved results and performance.
3+
- Some refactoring required to accommodate functional changes in how dataflow nodes attach to the AST. No functional changes to the analysis from this are expected.

0 commit comments

Comments
 (0)