Skip to content

Convert more queries to the new dataflow library #930

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Jul 15, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

import cpp
import codingstandards.c.misra
import semmle.code.cpp.dataflow.DataFlow
import semmle.code.cpp.dataflow.new.DataFlow

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

predicate isSink(DataFlow::Node sink) {
sink.asExpr() = any(ArrayParameter p).getAMatchingArgument()
sink.asIndirectExpr() = any(ArrayParameter p).getAMatchingArgument()
}
}

Expand All @@ -68,8 +68,8 @@ where
or
// the argument is a pointer and its value does not come from a literal of the correct
arg.getType() instanceof PointerType and
not exists(ArrayAggregateLiteral l |
SmallArrayFlow::flow(DataFlow::exprNode(l), DataFlow::exprNode(arg)) and
not exists(ArrayAggregateLiteral l, DataFlow::Node arg_node | arg_node.asIndirectExpr() = arg |
SmallArrayFlow::flow(DataFlow::exprNode(l), arg_node) and
countElements(l) >= p.getArraySize()
)
)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,3 @@
WARNING: module 'DataFlow' has been deprecated and may be removed in future (ArrayFunctionArgumentNumberOfElements.ql:48,36-44)
WARNING: module 'DataFlow' has been deprecated and may be removed in future (ArrayFunctionArgumentNumberOfElements.ql:49,22-30)
WARNING: module 'DataFlow' has been deprecated and may be removed in future (ArrayFunctionArgumentNumberOfElements.ql:51,20-28)
WARNING: module 'DataFlow' has been deprecated and may be removed in future (ArrayFunctionArgumentNumberOfElements.ql:56,25-33)
WARNING: module 'DataFlow' has been deprecated and may be removed in future (ArrayFunctionArgumentNumberOfElements.ql:72,28-36)
WARNING: module 'DataFlow' has been deprecated and may be removed in future (ArrayFunctionArgumentNumberOfElements.ql:72,51-59)
| 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 |
| 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 |
| 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 |
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
*/

import cpp
import semmle.code.cpp.dataflow.DataFlow
import semmle.code.cpp.dataflow.new.DataFlow
import codingstandards.cpp.autosar
import codingstandards.cpp.exceptions.ExceptionFlow
import codingstandards.cpp.exceptions.ExceptionSpecifications
Expand Down Expand Up @@ -98,6 +98,18 @@ class ExceptionThrownInConstructor extends ExceptionThrowingExpr {
Constructor getConstructor() { result = c }
}

module NewDeleteConfig implements DataFlow::ConfigSig {
predicate isSource(DataFlow::Node src) { src.asExpr() instanceof NewAllocationExpr }

predicate isSink(DataFlow::Node sink) { sink.asExpr() instanceof DeletedExpr }

DataFlow::FlowFeature getAFeature() {
result instanceof DataFlow::FeatureEqualSourceSinkCallContext
}
}

module NewDeleteFlow = DataFlow::Global<NewDeleteConfig>;

from
ExceptionThrowingConstructor c, ExceptionThrownInConstructor throwingExpr,
NewAllocationExpr newExpr, ExceptionFlowNode exceptionSource,
Expand Down Expand Up @@ -127,7 +139,7 @@ where
not exists(DeletedExpr deletedExpr |
deletedExpr.getEnclosingFunction() = c and
// Deletes the same memory location that was new'd
DataFlow::localFlow(DataFlow::exprNode(newExpr), DataFlow::exprNode(deletedExpr)) and
NewDeleteFlow::flow(DataFlow::exprNode(newExpr), DataFlow::exprNode(deletedExpr)) and
newExpr.getASuccessor+() = deletedExpr and
deletedExpr.getASuccessor+() = throwingExpr
) and
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,13 @@
import cpp
import codingstandards.cpp.autosar
import codingstandards.cpp.standardlibrary.Utility
import semmle.code.cpp.dataflow.DataFlow
import semmle.code.cpp.dataflow.new.DataFlow

from StdForwardCall f, Access a
where
not isExcluded(a, MoveForwardPackage::movedFromObjectReadAccessedQuery()) and
exists(DataFlow::DefinitionByReferenceNode def |
def.asDefiningArgument() = f and
def.asDefiningArgument() = f.getArgument(0) and
DataFlow::localFlow(def, DataFlow::exprNode(a))
)
select a, "The argument $@ of `std::forward` may be indeterminate when accessed at this location.",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
import cpp
import codingstandards.cpp.autosar
import codingstandards.cpp.SmartPointers
import semmle.code.cpp.dataflow.DataFlow
import semmle.code.cpp.dataflow.new.DataFlow

/*
* Finds `std::shared_ptr` local variables which are not copy or move initialized, and are not used in
Expand Down Expand Up @@ -44,7 +44,11 @@ from AutosarSharedPointerLocalScopeVariable var, SharedPointerLocalAllocInitiali
where
not isExcluded(var, SmartPointers1Package::sharedPointerUsedWithNoOwnershipSharingQuery()) and
var.getAnAssignedValue() = src and
not DataFlow::localExprFlow(src, varOwnershipSharingExpr(var.getType(), var.getFunction()))
not exists(DataFlow::Node n |
n.asIndirectExpr() = varOwnershipSharingExpr(var.getType(), var.getFunction())
|
DataFlow::localFlow(DataFlow::exprNode(src), n)
)
select var,
"The ownership of shared_ptr $@ is not shared within or passed out of the local scope of function $@.",
var, var.getName(), var.getFunction(), var.getFunction().getQualifiedName()
Original file line number Diff line number Diff line change
@@ -1,12 +1,3 @@
WARNING: module 'DataFlow' has been deprecated and may be removed in future (ConstructorErrorLeavesObjectInInvalidState.ql:47,12-20)
WARNING: module 'DataFlow' has been deprecated and may be removed in future (ConstructorErrorLeavesObjectInInvalidState.ql:48,30-38)
WARNING: module 'DataFlow' has been deprecated and may be removed in future (ConstructorErrorLeavesObjectInInvalidState.ql:48,57-65)
WARNING: module 'DataFlow' has been deprecated and may be removed in future (ConstructorErrorLeavesObjectInInvalidState.ql:74,5-13)
WARNING: module 'DataFlow' has been deprecated and may be removed in future (ConstructorErrorLeavesObjectInInvalidState.ql:74,25-33)
WARNING: module 'DataFlow' has been deprecated and may be removed in future (ConstructorErrorLeavesObjectInInvalidState.ql:75,7-15)
WARNING: module 'DataFlow' has been deprecated and may be removed in future (ConstructorErrorLeavesObjectInInvalidState.ql:130,5-13)
WARNING: module 'DataFlow' has been deprecated and may be removed in future (ConstructorErrorLeavesObjectInInvalidState.ql:130,25-33)
WARNING: module 'DataFlow' has been deprecated and may be removed in future (ConstructorErrorLeavesObjectInInvalidState.ql:130,54-62)
edges
| test.cpp:12:16:12:27 | new [bad_alloc] | test.cpp:14:33:16:5 | { ... } [bad_alloc] |
| test.cpp:13:7:13:28 | throw ... [exception] | test.cpp:14:33:16:5 | { ... } [exception] |
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1 @@
WARNING: module 'DataFlow' has been deprecated and may be removed in future (ArgumentToForwardSubsequentlyUsed.ql:22,10-18)
WARNING: module 'DataFlow' has been deprecated and may be removed in future (ArgumentToForwardSubsequentlyUsed.ql:24,5-13)
WARNING: module 'DataFlow' has been deprecated and may be removed in future (ArgumentToForwardSubsequentlyUsed.ql:24,30-38)
| 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 |
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
WARNING: module 'DataFlow' has been deprecated and may be removed in future (SharedPointerUsedWithNoOwnershipSharing.ql:47,7-15)
| 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 |
| 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 |
| 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 |
Loading