File tree Expand file tree Collapse file tree 3 files changed +9
-5
lines changed
src/Likely Bugs/Memory Management
test/query-tests/Likely Bugs/Memory Management/ReturnStackAllocatedMemory Expand file tree Collapse file tree 3 files changed +9
-5
lines changed Original file line number Diff line number Diff line change @@ -15,7 +15,8 @@ import cpp
1515// or accesses a possibly stack allocated local variables
1616predicate 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
Original file line number Diff line number Diff line change 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. |
Original file line number Diff line number Diff 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
103103char testArray3 ()
You can’t perform that action at this time.
0 commit comments