@@ -27,16 +27,26 @@ class ReturnStackAllocatedMemoryConfig extends MustFlowConfiguration {
27
27
ReturnStackAllocatedMemoryConfig ( ) { this = "ReturnStackAllocatedMemoryConfig" }
28
28
29
29
override predicate isSource ( Instruction source ) {
30
- // Holds if `source` is a node that represents the use of a stack variable
31
- exists ( VariableAddressInstruction var , Function func |
32
- var = source and
33
- func = source .getEnclosingFunction ( ) and
34
- var .getAstVariable ( ) instanceof StackVariable and
35
- // Pointer-to-member types aren't properly handled in the dbscheme.
36
- not var .getResultType ( ) instanceof PointerToMemberType and
30
+ exists ( Function func |
37
31
// Rule out FPs caused by extraction errors.
38
32
not any ( ErrorExpr e ) .getEnclosingFunction ( ) = func and
39
- not intentionallyReturnsStackPointer ( func )
33
+ not intentionallyReturnsStackPointer ( func ) and
34
+ func = source .getEnclosingFunction ( )
35
+ |
36
+ // `source` is an instruction that represents the use of a stack variable
37
+ exists ( VariableAddressInstruction var |
38
+ var = source and
39
+ var .getAstVariable ( ) instanceof StackVariable and
40
+ // Pointer-to-member types aren't properly handled in the dbscheme.
41
+ not var .getResultType ( ) instanceof PointerToMemberType
42
+ )
43
+ or
44
+ // `source` is an instruction that represents the return value of a
45
+ // function that is known to return stack-allocated memory.
46
+ exists ( Call call |
47
+ call .getTarget ( ) .hasGlobalName ( [ "alloca" , "strdupa" , "strndupa" ] ) and
48
+ source .getUnconvertedResultExpression ( ) = call
49
+ )
40
50
)
41
51
}
42
52
@@ -85,10 +95,10 @@ class ReturnStackAllocatedMemoryConfig extends MustFlowConfiguration {
85
95
}
86
96
87
97
from
88
- MustFlowPathNode source , MustFlowPathNode sink , VariableAddressInstruction var ,
98
+ MustFlowPathNode source , MustFlowPathNode sink , Instruction instr ,
89
99
ReturnStackAllocatedMemoryConfig conf
90
100
where
91
101
conf .hasFlowPath ( pragma [ only_bind_into ] ( source ) , pragma [ only_bind_into ] ( sink ) ) and
92
- source .getInstruction ( ) = var
102
+ source .getInstruction ( ) = instr
93
103
select sink .getInstruction ( ) , source , sink , "May return stack-allocated memory from $@." ,
94
- var .getAst ( ) , var .getAst ( ) .toString ( )
104
+ instr .getAst ( ) , instr .getAst ( ) .toString ( )
0 commit comments