Skip to content

Commit 4d702e2

Browse files
committed
C++: Fix IRGuards ternary behaviour
1 parent d849615 commit 4d702e2

File tree

2 files changed

+20
-9
lines changed

2 files changed

+20
-9
lines changed

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

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55

66
import cpp
77
import semmle.code.cpp.ir.IR
8+
private import semmle.code.cpp.ir.implementation.raw.internal.TranslatedExpr
9+
private import semmle.code.cpp.ir.implementation.raw.internal.InstructionTag
810

911
/**
1012
* Holds if `block` consists of an `UnreachedInstruction`.
@@ -201,10 +203,25 @@ private class GuardConditionFromIR extends GuardCondition {
201203
* `&&` and `||`. See the detailed explanation on predicate `controls`.
202204
*/
203205
private predicate controlsBlock(BasicBlock controlled, boolean testIsTrue) {
204-
exists(IRBlock irb |
206+
exists(IRBlock irb, Instruction instr |
205207
ir.controls(irb, testIsTrue) and
206-
irb.getAnInstruction().getAst().(ControlFlowNode).getBasicBlock() = controlled and
207-
not isUnreachedBlock(irb)
208+
instr = irb.getAnInstruction() and
209+
instr.getAst().(ControlFlowNode).getBasicBlock() = controlled and
210+
not isUnreachedBlock(irb) and
211+
not this.excludeAsControlledInstruction(instr)
212+
)
213+
}
214+
215+
private predicate excludeAsControlledInstruction(Instruction instr) {
216+
// Exclude the temporaries generated by a ternary expression.
217+
exists(TranslatedConditionalExpr tce |
218+
instr = tce.getInstruction(ConditionValueFalseStoreTag())
219+
or
220+
instr = tce.getInstruction(ConditionValueTrueStoreTag())
221+
or
222+
instr = tce.getInstruction(ConditionValueTrueTempAddressTag())
223+
or
224+
instr = tce.getInstruction(ConditionValueFalseTempAddressTag())
208225
)
209226
}
210227
}

cpp/ql/test/library-tests/controlflow/guards-ir/tests.expected

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -253,9 +253,7 @@ astGuardsControl
253253
| test.c:159:9:159:19 | ... == ... | true | 159 | 160 |
254254
| test.c:162:9:162:18 | ... < ... | true | 162 | 163 |
255255
| test.c:165:9:165:18 | ... < ... | true | 165 | 166 |
256-
| test.c:175:13:175:32 | ... == ... | false | 174 | 175 |
257256
| test.c:175:13:175:32 | ... == ... | false | 175 | 175 |
258-
| test.c:175:13:175:32 | ... == ... | true | 174 | 175 |
259257
| test.c:175:13:175:32 | ... == ... | true | 175 | 175 |
260258
| test.cpp:18:8:18:10 | call to get | true | 19 | 19 |
261259
| test.cpp:31:7:31:13 | ... == ... | false | 30 | 30 |
@@ -429,13 +427,9 @@ astGuardsEnsure
429427
| test.c:165:9:165:18 | ... < ... | test.c:165:9:165:9 | x | < | test.c:165:13:165:18 | ... - ... | 0 | 165 | 166 |
430428
| test.c:165:9:165:18 | ... < ... | test.c:165:13:165:13 | y | >= | test.c:165:9:165:9 | x | 43 | 165 | 166 |
431429
| test.c:165:9:165:18 | ... < ... | test.c:165:13:165:18 | ... - ... | >= | test.c:165:9:165:9 | x | 1 | 165 | 166 |
432-
| test.c:175:13:175:32 | ... == ... | test.c:175:13:175:15 | call to foo | != | test.c:175:32:175:32 | 0 | 0 | 174 | 175 |
433430
| test.c:175:13:175:32 | ... == ... | test.c:175:13:175:15 | call to foo | != | test.c:175:32:175:32 | 0 | 0 | 175 | 175 |
434-
| test.c:175:13:175:32 | ... == ... | test.c:175:13:175:15 | call to foo | == | test.c:175:32:175:32 | 0 | 0 | 174 | 175 |
435431
| test.c:175:13:175:32 | ... == ... | test.c:175:13:175:15 | call to foo | == | test.c:175:32:175:32 | 0 | 0 | 175 | 175 |
436-
| test.c:175:13:175:32 | ... == ... | test.c:175:32:175:32 | 0 | != | test.c:175:13:175:15 | call to foo | 0 | 174 | 175 |
437432
| test.c:175:13:175:32 | ... == ... | test.c:175:32:175:32 | 0 | != | test.c:175:13:175:15 | call to foo | 0 | 175 | 175 |
438-
| test.c:175:13:175:32 | ... == ... | test.c:175:32:175:32 | 0 | == | test.c:175:13:175:15 | call to foo | 0 | 174 | 175 |
439433
| test.c:175:13:175:32 | ... == ... | test.c:175:32:175:32 | 0 | == | test.c:175:13:175:15 | call to foo | 0 | 175 | 175 |
440434
| test.cpp:31:7:31:13 | ... == ... | test.cpp:31:7:31:7 | x | != | test.cpp:31:12:31:13 | - ... | 0 | 30 | 30 |
441435
| test.cpp:31:7:31:13 | ... == ... | test.cpp:31:7:31:7 | x | != | test.cpp:31:12:31:13 | - ... | 0 | 34 | 34 |

0 commit comments

Comments
 (0)