Skip to content

Commit 111a462

Browse files
committed
C++: Recover some of the good results we lost
My recent changes to suppress FPs in `ReturnStackAllocatedMemory.ql` caused us to lose all results where there was a `Conversion` at the initial address escape. We cannot handle conversions in general, but this commit restores the good results for the trivial types of conversion that we can handle.
1 parent d864df5 commit 111a462

File tree

3 files changed

+24
-6
lines changed

3 files changed

+24
-6
lines changed

cpp/ql/src/Likely Bugs/Memory Management/ReturnStackAllocatedMemory.ql

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,24 @@ import semmle.code.cpp.dataflow.DataFlow
2222
*/
2323
predicate conservativeDataFlowStep(DataFlow::Node n1, DataFlow::Node n2) {
2424
DataFlow::localFlowStep(n1, n2) and
25-
not n2.asExpr() instanceof FieldAccess
25+
not n2.asExpr() instanceof FieldAccess and
26+
not hasNontrivialConversion(n2.asExpr())
27+
}
28+
29+
/**
30+
* Holds if `e` has a conversion that changes it from lvalue to pointer or
31+
* back. As the data-flow library does not support conversions, we cannot track
32+
* data flow through such expressions.
33+
*/
34+
predicate hasNontrivialConversion(Expr e) {
35+
e instanceof Conversion and not
36+
(
37+
e instanceof Cast
38+
or
39+
e instanceof ParenthesisExpr
40+
)
41+
or
42+
hasNontrivialConversion(e.getConversion())
2643
}
2744

2845
from LocalScopeVariable var, VariableAccess va, ReturnStmt r
@@ -39,9 +56,10 @@ where
3956
or
4057
// The data flow library doesn't support conversions, so here we check that
4158
// the address escapes into some expression `pointerToLocal`, which flows
42-
// in a non-trivial way (one or more steps) to a returned expression.
59+
// in a one or more steps to a returned expression.
4360
exists(Expr pointerToLocal |
44-
variableAddressEscapesTree(va, pointerToLocal) and
61+
variableAddressEscapesTree(va, pointerToLocal.getFullyConverted()) and
62+
not hasNontrivialConversion(pointerToLocal) and
4563
conservativeDataFlowStep+(
4664
DataFlow::exprNode(pointerToLocal),
4765
DataFlow::exprNode(r.getExpr())

cpp/ql/test/query-tests/Likely Bugs/Memory Management/ReturnStackAllocatedMemory/ReturnStackAllocatedMemory.expected

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,4 @@
66
| test.cpp:112:2:112:12 | return ... | May return stack-allocated memory from $@. | test.cpp:112:9:112:11 | arr | arr |
77
| test.cpp:119:2:119:19 | return ... | May return stack-allocated memory from $@. | test.cpp:119:11:119:13 | arr | arr |
88
| test.cpp:149:3:149:22 | return ... | May return stack-allocated memory from $@. | test.cpp:149:11:149:21 | threadLocal | threadLocal |
9-
| test.cpp:190:3:190:14 | return ... | May return stack-allocated memory from $@. | test.cpp:188:13:188:19 | myLocal | myLocal |
9+
| test.cpp:171:3:171:24 | return ... | May return stack-allocated memory from $@. | test.cpp:170:35:170:41 | myLocal | myLocal |

cpp/ql/test/query-tests/Likely Bugs/Memory Management/ReturnStackAllocatedMemory/test.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,7 @@ char *returnAfterCopy() {
168168
void *conversionBeforeDataFlow() {
169169
int myLocal;
170170
void *pointerToLocal = (void *)&myLocal; // has conversion
171-
return pointerToLocal; // BAD [NOT DETECTED]
171+
return pointerToLocal; // BAD
172172
}
173173

174174
void *arrayConversionBeforeDataFlow() {
@@ -187,5 +187,5 @@ int *&conversionInFlow() {
187187
int myLocal;
188188
int *p = &myLocal;
189189
int *&pRef = p; // has conversion in the middle of data flow
190-
return pRef; // BAD [MISLEADING ALERT MESSAGE]
190+
return pRef; // BAD [NOT DETECTED]
191191
}

0 commit comments

Comments
 (0)