Skip to content

Commit cec785c

Browse files
committed
C++: Respond to review comments.
1 parent f284fde commit cec785c

File tree

3 files changed

+15
-13
lines changed

3 files changed

+15
-13
lines changed

cpp/ql/src/Critical/DoubleFree.ql

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,11 @@ import semmle.code.cpp.dataflow.new.DataFlow
1616
import FlowAfterFree
1717
import DoubleFree::PathGraph
1818

19-
predicate isFree(DataFlow::Node n, Expr e) {
20-
n.asExpr() = e and
21-
isFree(_, e, _)
22-
}
19+
/**
20+
* Holds if `n` is a dataflow node that represents a pointer going into a
21+
* deallocation function, and `e` is the corresponding expression.
22+
*/
23+
predicate isFree(DataFlow::Node n, Expr e) { isFree(_, n, e, _) }
2324

2425
/**
2526
* `dealloc1` is a deallocation expression and `e` is an expression such
@@ -31,7 +32,7 @@ predicate isFree(DataFlow::Node n, Expr e) {
3132
*/
3233
bindingset[dealloc1, e]
3334
predicate isExcludeFreePair(DeallocationExpr dealloc1, Expr e) {
34-
exists(DeallocationExpr dealloc2 | isFree(_, e, dealloc2) |
35+
exists(DeallocationExpr dealloc2 | isFree(_, _, e, dealloc2) |
3536
dealloc1.(FunctionCall).getTarget().hasGlobalName("MmFreePagesFromMdl") and
3637
// From https://learn.microsoft.com/en-us/windows-hardware/drivers/ddi/wdm/nf-wdm-mmfreepagesfrommdl:
3738
// "After calling MmFreePagesFromMdl, the caller must also call ExFreePool
@@ -45,7 +46,7 @@ module DoubleFree = FlowFromFree<isFree/2, isExcludeFreePair/2>;
4546
from DoubleFree::PathNode source, DoubleFree::PathNode sink, DeallocationExpr dealloc, Expr e2
4647
where
4748
DoubleFree::flowPath(source, sink) and
48-
isFree(source.getNode(), _, dealloc) and
49+
isFree(source.getNode(), _, _, dealloc) and
4950
isFree(sink.getNode(), e2)
5051
select sink.getNode(), source, sink,
5152
"Memory pointed to by '" + e2.toString() + "' may already have been freed by $@.", dealloc,

cpp/ql/src/Critical/FlowAfterFree.qll

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -50,12 +50,12 @@ predicate strictlyDominates(IRBlock b1, int i1, IRBlock b2, int i2) {
5050
module FlowFromFree<isSinkSig/2 isASink, isExcludedSig/2 isExcluded> {
5151
module FlowFromFreeConfig implements DataFlow::StateConfigSig {
5252
class FlowState instanceof Expr {
53-
FlowState() { isFree(_, this, _) }
53+
FlowState() { isFree(_, _, this, _) }
5454

5555
string toString() { result = super.toString() }
5656
}
5757

58-
predicate isSource(DataFlow::Node node, FlowState state) { isFree(node, state, _) }
58+
predicate isSource(DataFlow::Node node, FlowState state) { isFree(node, _, state, _) }
5959

6060
pragma[inline]
6161
predicate isSink(DataFlow::Node sink, FlowState state) {
@@ -64,7 +64,7 @@ module FlowFromFree<isSinkSig/2 isASink, isExcludedSig/2 isExcluded> {
6464
DeallocationExpr dealloc
6565
|
6666
isASink(sink, e) and
67-
isFree(source, state, dealloc) and
67+
isFree(source, _, state, dealloc) and
6868
e != state and
6969
source.hasIndexInBlock(b1, i1) and
7070
sink.hasIndexInBlock(b2, i2) and
@@ -98,11 +98,12 @@ module FlowFromFree<isSinkSig/2 isASink, isExcludedSig/2 isExcluded> {
9898
* `dealloc` after the call returns (i.e., the post-update node associated with
9999
* the argument to `dealloc`).
100100
*/
101-
predicate isFree(DataFlow::Node n, Expr e, DeallocationExpr dealloc) {
101+
predicate isFree(DataFlow::Node outgoing, DataFlow::Node incoming, Expr e, DeallocationExpr dealloc) {
102102
exists(Expr conv |
103103
e = conv.getUnconverted() and
104104
conv = dealloc.getFreedExpr().getFullyConverted() and
105-
conv = n.(DataFlow::PostUpdateNode).getPreUpdateNode().asConvertedExpr()
105+
incoming = outgoing.(DataFlow::PostUpdateNode).getPreUpdateNode() and
106+
conv = incoming.asConvertedExpr()
106107
) and
107108
// Ignore realloc functions
108109
not exists(dealloc.(FunctionCall).getTarget().(AllocationFunction).getReallocPtrArg())

cpp/ql/src/Critical/UseAfterFree.ql

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ private predicate externalCallNeverDereferences(FormattingFunctionCall call, int
3030
}
3131

3232
predicate isUse0(Expr e) {
33-
not isFree(_, e, _) and
33+
not isFree(_, _, e, _) and
3434
(
3535
e = any(PointerDereferenceExpr pde).getOperand()
3636
or
@@ -170,6 +170,6 @@ module UseAfterFree = FlowFromFree<isUse/2, isExcludeFreeUsePair/2>;
170170
from UseAfterFree::PathNode source, UseAfterFree::PathNode sink, DeallocationExpr dealloc
171171
where
172172
UseAfterFree::flowPath(source, sink) and
173-
isFree(source.getNode(), _, dealloc)
173+
isFree(source.getNode(), _, _, dealloc)
174174
select sink.getNode(), source, sink, "Memory may have been previously freed by $@.", dealloc,
175175
dealloc.toString()

0 commit comments

Comments
 (0)