Skip to content

Commit 4685f19

Browse files
committed
CPP: Widen varMaybeStackAllocated.
1 parent c87036f commit 4685f19

File tree

3 files changed

+7
-13
lines changed

3 files changed

+7
-13
lines changed

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

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -22,16 +22,7 @@ predicate exprMaybeStackAllocated(Expr e) {
2222
// is initialized to/assigned a possibly stack allocated expression
2323
predicate varMaybeStackAllocated(LocalVariable lv) {
2424
not lv.isStatic() and
25-
(
26-
lv.getType().getUnderlyingType() instanceof ArrayType
27-
or
28-
exprMaybeStackAllocated(lv.getInitializer().getExpr())
29-
or
30-
exists(AssignExpr a |
31-
a.getLValue().(VariableAccess).getTarget() = lv and
32-
exprMaybeStackAllocated(a.getRValue())
33-
)
34-
)
25+
not lv.getType() instanceof ReferenceType
3526
}
3627

3728
// an expression possibly points to the stack if it takes the address of
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,4 @@
1+
| test.cpp:12:2:12:12 | return ... | May return stack-allocated memory. |
2+
| test.cpp:20:2:20:12 | return ... | May return stack-allocated memory. |
3+
| test.cpp:73:2:73:12 | return ... | May return stack-allocated memory. |
14
| test.cpp:93:2:93:12 | return ... | May return stack-allocated memory. |

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,15 @@ MyClass *test1()
99
{
1010
MyClass mc;
1111

12-
return &mc; // BAD [NOT DETECTED]
12+
return &mc; // BAD
1313
}
1414

1515
MyClass *test2()
1616
{
1717
MyClass mc;
1818
MyClass *ptr = &mc;
1919

20-
return ptr; // BAD [NOT DETECTED]
20+
return ptr; // BAD
2121
}
2222

2323
MyClass *test3()
@@ -70,7 +70,7 @@ MyClass *test10()
7070
ptr = &mc;
7171
}
7272

73-
return ptr; // BAD [NOT DETECTED]
73+
return ptr; // BAD
7474
}
7575

7676
MyClass *test11(MyClass *param)

0 commit comments

Comments
 (0)