Skip to content

Commit 7b37153

Browse files
authored
Merge pull request #930 from jketema/dataflow-3
Convert more queries to the new dataflow library
2 parents bd434ed + f4d7e9f commit 7b37153

8 files changed

+26
-29
lines changed

c/misra/src/rules/RULE-17-5/ArrayFunctionArgumentNumberOfElements.ql

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414

1515
import cpp
1616
import codingstandards.c.misra
17-
import semmle.code.cpp.dataflow.DataFlow
17+
import semmle.code.cpp.dataflow.new.DataFlow
1818

1919
/**
2020
* Models a function parameter of type array with specified size
@@ -49,7 +49,7 @@ module SmallArrayConfig implements DataFlow::ConfigSig {
4949
predicate isSource(DataFlow::Node src) { src.asExpr() instanceof ArrayAggregateLiteral }
5050

5151
predicate isSink(DataFlow::Node sink) {
52-
sink.asExpr() = any(ArrayParameter p).getAMatchingArgument()
52+
sink.asIndirectExpr() = any(ArrayParameter p).getAMatchingArgument()
5353
}
5454
}
5555

@@ -68,8 +68,8 @@ where
6868
or
6969
// the argument is a pointer and its value does not come from a literal of the correct
7070
arg.getType() instanceof PointerType and
71-
not exists(ArrayAggregateLiteral l |
72-
SmallArrayFlow::flow(DataFlow::exprNode(l), DataFlow::exprNode(arg)) and
71+
not exists(ArrayAggregateLiteral l, DataFlow::Node arg_node | arg_node.asIndirectExpr() = arg |
72+
SmallArrayFlow::flow(DataFlow::exprNode(l), arg_node) and
7373
countElements(l) >= p.getArraySize()
7474
)
7575
)

c/misra/test/rules/RULE-17-5/ArrayFunctionArgumentNumberOfElements.expected

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,3 @@
1-
WARNING: module 'DataFlow' has been deprecated and may be removed in future (ArrayFunctionArgumentNumberOfElements.ql:48,36-44)
2-
WARNING: module 'DataFlow' has been deprecated and may be removed in future (ArrayFunctionArgumentNumberOfElements.ql:49,22-30)
3-
WARNING: module 'DataFlow' has been deprecated and may be removed in future (ArrayFunctionArgumentNumberOfElements.ql:51,20-28)
4-
WARNING: module 'DataFlow' has been deprecated and may be removed in future (ArrayFunctionArgumentNumberOfElements.ql:56,25-33)
5-
WARNING: module 'DataFlow' has been deprecated and may be removed in future (ArrayFunctionArgumentNumberOfElements.ql:72,28-36)
6-
WARNING: module 'DataFlow' has been deprecated and may be removed in future (ArrayFunctionArgumentNumberOfElements.ql:72,51-59)
71
| test.c:18:6:18:6 | 0 | The function argument does not have a sufficient number or elements declared in the $@. | test.c:1:13:1:14 | ar | parameter |
82
| test.c:19:6:19:7 | ar | The function argument does not have a sufficient number or elements declared in the $@. | test.c:1:13:1:14 | ar | parameter |
93
| test.c:21:6:21:9 | ar2p | The function argument does not have a sufficient number or elements declared in the $@. | test.c:1:13:1:14 | ar | parameter |

cpp/autosar/src/rules/A15-2-2/ConstructorErrorLeavesObjectInInvalidState.ql

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
*/
1616

1717
import cpp
18-
import semmle.code.cpp.dataflow.DataFlow
18+
import semmle.code.cpp.dataflow.new.DataFlow
1919
import codingstandards.cpp.autosar
2020
import codingstandards.cpp.exceptions.ExceptionFlow
2121
import codingstandards.cpp.exceptions.ExceptionSpecifications
@@ -98,6 +98,18 @@ class ExceptionThrownInConstructor extends ExceptionThrowingExpr {
9898
Constructor getConstructor() { result = c }
9999
}
100100

101+
module NewDeleteConfig implements DataFlow::ConfigSig {
102+
predicate isSource(DataFlow::Node src) { src.asExpr() instanceof NewAllocationExpr }
103+
104+
predicate isSink(DataFlow::Node sink) { sink.asExpr() instanceof DeletedExpr }
105+
106+
DataFlow::FlowFeature getAFeature() {
107+
result instanceof DataFlow::FeatureEqualSourceSinkCallContext
108+
}
109+
}
110+
111+
module NewDeleteFlow = DataFlow::Global<NewDeleteConfig>;
112+
101113
from
102114
ExceptionThrowingConstructor c, ExceptionThrownInConstructor throwingExpr,
103115
NewAllocationExpr newExpr, ExceptionFlowNode exceptionSource,
@@ -127,7 +139,7 @@ where
127139
not exists(DeletedExpr deletedExpr |
128140
deletedExpr.getEnclosingFunction() = c and
129141
// Deletes the same memory location that was new'd
130-
DataFlow::localFlow(DataFlow::exprNode(newExpr), DataFlow::exprNode(deletedExpr)) and
142+
NewDeleteFlow::flow(DataFlow::exprNode(newExpr), DataFlow::exprNode(deletedExpr)) and
131143
newExpr.getASuccessor+() = deletedExpr and
132144
deletedExpr.getASuccessor+() = throwingExpr
133145
) and

cpp/autosar/src/rules/A18-9-4/ArgumentToForwardSubsequentlyUsed.ql

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,13 @@
1414
import cpp
1515
import codingstandards.cpp.autosar
1616
import codingstandards.cpp.standardlibrary.Utility
17-
import semmle.code.cpp.dataflow.DataFlow
17+
import semmle.code.cpp.dataflow.new.DataFlow
1818

1919
from StdForwardCall f, Access a
2020
where
2121
not isExcluded(a, MoveForwardPackage::movedFromObjectReadAccessedQuery()) and
2222
exists(DataFlow::DefinitionByReferenceNode def |
23-
def.asDefiningArgument() = f and
23+
def.asDefiningArgument() = f.getArgument(0) and
2424
DataFlow::localFlow(def, DataFlow::exprNode(a))
2525
)
2626
select a, "The argument $@ of `std::forward` may be indeterminate when accessed at this location.",

cpp/autosar/src/rules/A20-8-4/SharedPointerUsedWithNoOwnershipSharing.ql

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
import cpp
1717
import codingstandards.cpp.autosar
1818
import codingstandards.cpp.SmartPointers
19-
import semmle.code.cpp.dataflow.DataFlow
19+
import semmle.code.cpp.dataflow.new.DataFlow
2020

2121
/*
2222
* Finds `std::shared_ptr` local variables which are not copy or move initialized, and are not used in
@@ -44,7 +44,11 @@ from AutosarSharedPointerLocalScopeVariable var, SharedPointerLocalAllocInitiali
4444
where
4545
not isExcluded(var, SmartPointers1Package::sharedPointerUsedWithNoOwnershipSharingQuery()) and
4646
var.getAnAssignedValue() = src and
47-
not DataFlow::localExprFlow(src, varOwnershipSharingExpr(var.getType(), var.getFunction()))
47+
not exists(DataFlow::Node n |
48+
n.asIndirectExpr() = varOwnershipSharingExpr(var.getType(), var.getFunction())
49+
|
50+
DataFlow::localFlow(DataFlow::exprNode(src), n)
51+
)
4852
select var,
4953
"The ownership of shared_ptr $@ is not shared within or passed out of the local scope of function $@.",
5054
var, var.getName(), var.getFunction(), var.getFunction().getQualifiedName()

cpp/autosar/test/rules/A15-2-2/ConstructorErrorLeavesObjectInInvalidState.expected

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,3 @@
1-
WARNING: module 'DataFlow' has been deprecated and may be removed in future (ConstructorErrorLeavesObjectInInvalidState.ql:47,12-20)
2-
WARNING: module 'DataFlow' has been deprecated and may be removed in future (ConstructorErrorLeavesObjectInInvalidState.ql:48,30-38)
3-
WARNING: module 'DataFlow' has been deprecated and may be removed in future (ConstructorErrorLeavesObjectInInvalidState.ql:48,57-65)
4-
WARNING: module 'DataFlow' has been deprecated and may be removed in future (ConstructorErrorLeavesObjectInInvalidState.ql:74,5-13)
5-
WARNING: module 'DataFlow' has been deprecated and may be removed in future (ConstructorErrorLeavesObjectInInvalidState.ql:74,25-33)
6-
WARNING: module 'DataFlow' has been deprecated and may be removed in future (ConstructorErrorLeavesObjectInInvalidState.ql:75,7-15)
7-
WARNING: module 'DataFlow' has been deprecated and may be removed in future (ConstructorErrorLeavesObjectInInvalidState.ql:130,5-13)
8-
WARNING: module 'DataFlow' has been deprecated and may be removed in future (ConstructorErrorLeavesObjectInInvalidState.ql:130,25-33)
9-
WARNING: module 'DataFlow' has been deprecated and may be removed in future (ConstructorErrorLeavesObjectInInvalidState.ql:130,54-62)
101
edges
112
| test.cpp:12:16:12:27 | new [bad_alloc] | test.cpp:14:33:16:5 | { ... } [bad_alloc] |
123
| test.cpp:13:7:13:28 | throw ... [exception] | test.cpp:14:33:16:5 | { ... } [exception] |
Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1 @@
1-
WARNING: module 'DataFlow' has been deprecated and may be removed in future (ArgumentToForwardSubsequentlyUsed.ql:22,10-18)
2-
WARNING: module 'DataFlow' has been deprecated and may be removed in future (ArgumentToForwardSubsequentlyUsed.ql:24,5-13)
3-
WARNING: module 'DataFlow' has been deprecated and may be removed in future (ArgumentToForwardSubsequentlyUsed.ql:24,30-38)
41
| test.cpp:8:5:8:6 | t2 | The argument $@ of `std::forward` may be indeterminate when accessed at this location. | test.cpp:7:45:7:46 | t2 | t2 |
Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
WARNING: module 'DataFlow' has been deprecated and may be removed in future (SharedPointerUsedWithNoOwnershipSharing.ql:47,7-15)
21
| test.cpp:14:24:14:26 | sp3 | The ownership of shared_ptr $@ is not shared within or passed out of the local scope of function $@. | test.cpp:14:24:14:26 | sp3 | sp3 | test.cpp:11:22:11:23 | f1 | f1 |
32
| test.cpp:16:24:16:26 | sp5 | The ownership of shared_ptr $@ is not shared within or passed out of the local scope of function $@. | test.cpp:16:24:16:26 | sp5 | sp5 | test.cpp:11:22:11:23 | f1 | f1 |
43
| test.cpp:17:24:17:26 | sp6 | The ownership of shared_ptr $@ is not shared within or passed out of the local scope of function $@. | test.cpp:17:24:17:26 | sp6 | sp6 | test.cpp:11:22:11:23 | f1 | f1 |

0 commit comments

Comments
 (0)