Skip to content

Commit 74799e5

Browse files
committed
C++: Use the 'StoreInstruction' instead of the 'ReturnValueInstruction' when detecting return expressions.
1 parent 26a8a4b commit 74799e5

File tree

3 files changed

+25
-5
lines changed

3 files changed

+25
-5
lines changed

cpp/ql/lib/semmle/code/cpp/controlflow/IRGuards.qll

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -361,9 +361,30 @@ module GuardsInput implements SharedGuards::InputSig<Cpp::Location, Instruction,
361361

362362
/** Gets an expression returned from this function. */
363363
GuardsInput::Expr getAReturnExpr() {
364-
exists(ReturnValueInstruction ret |
365-
ret.getEnclosingFunction() = this and
366-
result = ret.getReturnValue()
364+
exists(StoreInstruction store |
365+
// We use the `Store` instruction that writes the return value instead of the
366+
// `ReturnValue` instruction since the `ReturnValue` instruction is not always
367+
// dominated by certain guards. For example:
368+
// ```
369+
// if(b) {
370+
// return true;
371+
// } else {
372+
// return false;
373+
// }
374+
// ```
375+
// this will be translated into IR like:
376+
// ```
377+
// if(b) {
378+
// x = true;
379+
// } else {
380+
// x = false;
381+
// }
382+
// return x;
383+
// ```
384+
store.getDestinationAddress().(VariableAddressInstruction).getIRVariable() instanceof
385+
IRReturnVariable and
386+
store.getEnclosingFunction() = this and
387+
result = store
367388
)
368389
}
369390
}

cpp/ql/test/library-tests/dataflow/dataflow-tests/BarrierGuard.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,7 @@ void test_guarded_wrapper() {
139139
int x = source();
140140

141141
if(guarded_wrapper(x)) {
142-
sink(x); // $ SPURIOUS: ast,ir
142+
sink(x); // $ SPURIOUS: ast
143143
} else {
144144
sink(x); // $ ast,ir
145145
}

cpp/ql/test/library-tests/dataflow/dataflow-tests/test-source-sink.expected

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,6 @@ irFlow
158158
| BarrierGuard.cpp:49:10:49:15 | call to source | BarrierGuard.cpp:55:13:55:13 | x |
159159
| BarrierGuard.cpp:60:11:60:16 | call to source | BarrierGuard.cpp:64:14:64:14 | x |
160160
| BarrierGuard.cpp:60:11:60:16 | call to source | BarrierGuard.cpp:66:14:66:14 | x |
161-
| BarrierGuard.cpp:139:11:139:16 | call to source | BarrierGuard.cpp:142:10:142:10 | x |
162161
| BarrierGuard.cpp:139:11:139:16 | call to source | BarrierGuard.cpp:144:10:144:10 | x |
163162
| acrossLinkTargets.cpp:19:27:19:32 | call to source | acrossLinkTargets.cpp:12:8:12:8 | x |
164163
| clang.cpp:12:9:12:20 | sourceArray1 | clang.cpp:18:8:18:19 | sourceArray1 |

0 commit comments

Comments
 (0)