File tree Expand file tree Collapse file tree 2 files changed +14
-2
lines changed
src/Likely Bugs/Memory Management
test/query-tests/Likely Bugs/Memory Management/ReturnStackAllocatedMemory Expand file tree Collapse file tree 2 files changed +14
-2
lines changed Original file line number Diff line number Diff line change @@ -15,17 +15,24 @@ import cpp
15
15
import semmle.code.cpp.ir.IR
16
16
import semmle.code.cpp.ir.dataflow.DataFlow:: DataFlow
17
17
18
+ /** Holds if `f` has a name that we intrepret as evidence of intentionally returning the value of the stack pointer. */
19
+ predicate intentionallyReturnsStackPointer ( Function f ) {
20
+ f .getName ( ) .toLowerCase ( ) .matches ( [ "%stack%" , "%sp%" ] )
21
+ }
22
+
18
23
/**
19
24
* Holds if `source` is a node that represents the use of a stack variable
20
25
*/
21
26
predicate isSource ( Node source ) {
22
- exists ( VariableAddressInstruction var |
27
+ exists ( VariableAddressInstruction var , Function func |
23
28
var = source .asInstruction ( ) and
29
+ func = var .getEnclosingFunction ( ) and
24
30
var .getASTVariable ( ) instanceof StackVariable and
25
31
// Pointer-to-member types aren't properly handled in the dbscheme.
26
32
not var .getResultType ( ) instanceof PointerToMemberType and
27
33
// Rule out FPs caused by extraction errors.
28
- not any ( ErrorExpr e ) .getEnclosingFunction ( ) = var .getEnclosingFunction ( )
34
+ not any ( ErrorExpr e ) .getEnclosingFunction ( ) = func and
35
+ not intentionallyReturnsStackPointer ( func )
29
36
)
30
37
}
31
38
Original file line number Diff line number Diff line change @@ -216,3 +216,8 @@ auto make_read_port()
216
216
auto ptr = port.get ();
217
217
return ptr; // GOOD
218
218
}
219
+
220
+ void * get_sp () {
221
+ int p;
222
+ return (void *)&p; // GOOD: The function name makes it sound like the programmer intended to get the value of the stack pointer.
223
+ }
You can’t perform that action at this time.
0 commit comments