Skip to content

Commit 07adf6f

Browse files
committed
CPP: Handle array accesses.
1 parent 4685f19 commit 07adf6f

File tree

3 files changed

+9
-5
lines changed

3 files changed

+9
-5
lines changed

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

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,8 @@ import cpp
1515
// or accesses a possibly stack allocated local variables
1616
predicate exprMaybeStackAllocated(Expr e) {
1717
e instanceof AggregateLiteral or
18-
varMaybeStackAllocated(e.(VariableAccess).getTarget())
18+
varMaybeStackAllocated(e.(VariableAccess).getTarget()) or
19+
exprMayPointToStack(e.(ArrayExpr).getArrayBase())
1920
}
2021

2122
// a local variable is possibly stack allocated if it is not static and
@@ -34,9 +35,11 @@ predicate exprMayPointToStack(Expr e) {
3435
or
3536
varMayPointToStack(e.(VariableAccess).getTarget())
3637
or
37-
exprMaybeStackAllocated(e) and
38-
e.getType() instanceof ArrayType and
39-
e.getFullyConverted().getType() instanceof PointerType
38+
(
39+
exprMaybeStackAllocated(e) and
40+
e.getType() instanceof ArrayType and
41+
e.getFullyConverted().getType() instanceof PointerType
42+
)
4043
}
4144

4245
// a local variable possibly points to the stack if it is initialized to/assigned to

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,4 @@
22
| test.cpp:20:2:20:12 | return ... | May return stack-allocated memory. |
33
| test.cpp:73:2:73:12 | return ... | May return stack-allocated memory. |
44
| test.cpp:93:2:93:12 | return ... | May return stack-allocated memory. |
5+
| test.cpp:100:2:100:19 | return ... | May return stack-allocated memory. |

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ char *testArray2()
9797
{
9898
char arr[256];
9999

100-
return &(arr[10]); // BAD [NOT DETECTED]
100+
return &(arr[10]); // BAD
101101
}
102102

103103
char testArray3()

0 commit comments

Comments
 (0)