Skip to content

Commit 48064c1

Browse files
committed
C++: Fix false positive.
1 parent 7c8c209 commit 48064c1

File tree

3 files changed

+10
-15
lines changed

3 files changed

+10
-15
lines changed

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

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,17 +15,24 @@ import cpp
1515
import semmle.code.cpp.ir.IR
1616
import semmle.code.cpp.ir.dataflow.DataFlow::DataFlow
1717

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+
1823
/**
1924
* Holds if `source` is a node that represents the use of a stack variable
2025
*/
2126
predicate isSource(Node source) {
22-
exists(VariableAddressInstruction var |
27+
exists(VariableAddressInstruction var, Function func |
2328
var = source.asInstruction() and
29+
func = var.getEnclosingFunction() and
2430
var.getASTVariable() instanceof StackVariable and
2531
// Pointer-to-member types aren't properly handled in the dbscheme.
2632
not var.getResultType() instanceof PointerToMemberType and
2733
// 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)
2936
)
3037
}
3138

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

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -100,11 +100,6 @@ edges
100100
| test.cpp:190:10:190:13 | Unary | test.cpp:190:10:190:13 | (reference dereference) |
101101
| test.cpp:190:10:190:13 | Unary | test.cpp:190:10:190:13 | (reference to) |
102102
| test.cpp:190:10:190:13 | pRef | test.cpp:190:10:190:13 | Unary |
103-
| test.cpp:222:9:222:17 | (void *)... | test.cpp:222:9:222:17 | StoreValue |
104-
| test.cpp:222:16:222:17 | & ... | test.cpp:222:16:222:17 | Unary |
105-
| test.cpp:222:16:222:17 | Unary | test.cpp:222:9:222:17 | (void *)... |
106-
| test.cpp:222:17:222:17 | Unary | test.cpp:222:16:222:17 | & ... |
107-
| test.cpp:222:17:222:17 | p | test.cpp:222:17:222:17 | Unary |
108103
nodes
109104
| test.cpp:17:9:17:11 | & ... | semmle.label | & ... |
110105
| test.cpp:17:9:17:11 | StoreValue | semmle.label | StoreValue |
@@ -220,12 +215,6 @@ nodes
220215
| test.cpp:190:10:190:13 | Unary | semmle.label | Unary |
221216
| test.cpp:190:10:190:13 | Unary | semmle.label | Unary |
222217
| test.cpp:190:10:190:13 | pRef | semmle.label | pRef |
223-
| test.cpp:222:9:222:17 | (void *)... | semmle.label | (void *)... |
224-
| test.cpp:222:9:222:17 | StoreValue | semmle.label | StoreValue |
225-
| test.cpp:222:16:222:17 | & ... | semmle.label | & ... |
226-
| test.cpp:222:16:222:17 | Unary | semmle.label | Unary |
227-
| test.cpp:222:17:222:17 | Unary | semmle.label | Unary |
228-
| test.cpp:222:17:222:17 | p | semmle.label | p |
229218
#select
230219
| test.cpp:17:9:17:11 | StoreValue | test.cpp:17:10:17:11 | mc | test.cpp:17:9:17:11 | StoreValue | May return stack-allocated memory from $@. | test.cpp:17:10:17:11 | mc | mc |
231220
| test.cpp:25:9:25:11 | StoreValue | test.cpp:23:18:23:19 | mc | test.cpp:25:9:25:11 | StoreValue | May return stack-allocated memory from $@. | test.cpp:23:18:23:19 | mc | mc |
@@ -240,4 +229,3 @@ nodes
240229
| test.cpp:177:10:177:23 | StoreValue | test.cpp:176:25:176:34 | localArray | test.cpp:177:10:177:23 | StoreValue | May return stack-allocated memory from $@. | test.cpp:176:25:176:34 | localArray | localArray |
241230
| test.cpp:183:10:183:19 | StoreValue | test.cpp:182:21:182:27 | myLocal | test.cpp:183:10:183:19 | StoreValue | May return stack-allocated memory from $@. | test.cpp:182:21:182:27 | myLocal | myLocal |
242231
| test.cpp:190:10:190:13 | StoreValue | test.cpp:189:16:189:16 | p | test.cpp:190:10:190:13 | StoreValue | May return stack-allocated memory from $@. | test.cpp:189:16:189:16 | p | p |
243-
| test.cpp:222:9:222:17 | StoreValue | test.cpp:222:17:222:17 | p | test.cpp:222:9:222:17 | StoreValue | May return stack-allocated memory from $@. | test.cpp:222:17:222:17 | p | p |

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
@@ -219,5 +219,5 @@ auto make_read_port()
219219

220220
void* get_sp() {
221221
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. [FALSE POSITIVE]
222+
return (void*)&p; // GOOD: The function name makes it sound like the programmer intended to get the value of the stack pointer.
223223
}

0 commit comments

Comments
 (0)