Skip to content

Commit 493ea6d

Browse files
committed
C++: Add a new consistency check for branching on non-boolean values.
1 parent e081b9a commit 493ea6d

12 files changed

+80
-0
lines changed

cpp/ql/lib/semmle/code/cpp/ir/implementation/aliased_ssa/IRConsistency.qll

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -546,4 +546,26 @@ module InstructionConsistency {
546546
"' has no associated variable, in function '$@'." and
547547
irFunc = getInstructionIRFunction(instr, irFuncText)
548548
}
549+
550+
query predicate nonBooleanOperand(
551+
Instruction instr, string message, OptionalIRFunction irFunc, string irFuncText
552+
) {
553+
exists(Instruction unary |
554+
unary = instr.(LogicalNotInstruction).getUnary() and
555+
not unary.getResultIRType() instanceof IRBooleanType and
556+
irFunc = getInstructionIRFunction(instr, irFuncText) and
557+
message =
558+
"Logical Not instruction " + instr.toString() +
559+
" with non-Boolean operand, in function '$@'."
560+
)
561+
or
562+
exists(Instruction cond |
563+
cond = instr.(ConditionalBranchInstruction).getCondition() and
564+
not cond.getResultIRType() instanceof IRBooleanType and
565+
irFunc = getInstructionIRFunction(instr, irFuncText) and
566+
message =
567+
"Conditional branch instruction " + instr.toString() +
568+
" with non-Boolean condition, in function '$@'."
569+
)
570+
}
549571
}

cpp/ql/lib/semmle/code/cpp/ir/implementation/raw/IRConsistency.qll

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -546,4 +546,26 @@ module InstructionConsistency {
546546
"' has no associated variable, in function '$@'." and
547547
irFunc = getInstructionIRFunction(instr, irFuncText)
548548
}
549+
550+
query predicate nonBooleanOperand(
551+
Instruction instr, string message, OptionalIRFunction irFunc, string irFuncText
552+
) {
553+
exists(Instruction unary |
554+
unary = instr.(LogicalNotInstruction).getUnary() and
555+
not unary.getResultIRType() instanceof IRBooleanType and
556+
irFunc = getInstructionIRFunction(instr, irFuncText) and
557+
message =
558+
"Logical Not instruction " + instr.toString() +
559+
" with non-Boolean operand, in function '$@'."
560+
)
561+
or
562+
exists(Instruction cond |
563+
cond = instr.(ConditionalBranchInstruction).getCondition() and
564+
not cond.getResultIRType() instanceof IRBooleanType and
565+
irFunc = getInstructionIRFunction(instr, irFuncText) and
566+
message =
567+
"Conditional branch instruction " + instr.toString() +
568+
" with non-Boolean condition, in function '$@'."
569+
)
570+
}
549571
}

cpp/ql/lib/semmle/code/cpp/ir/implementation/unaliased_ssa/IRConsistency.qll

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -546,4 +546,26 @@ module InstructionConsistency {
546546
"' has no associated variable, in function '$@'." and
547547
irFunc = getInstructionIRFunction(instr, irFuncText)
548548
}
549+
550+
query predicate nonBooleanOperand(
551+
Instruction instr, string message, OptionalIRFunction irFunc, string irFuncText
552+
) {
553+
exists(Instruction unary |
554+
unary = instr.(LogicalNotInstruction).getUnary() and
555+
not unary.getResultIRType() instanceof IRBooleanType and
556+
irFunc = getInstructionIRFunction(instr, irFuncText) and
557+
message =
558+
"Logical Not instruction " + instr.toString() +
559+
" with non-Boolean operand, in function '$@'."
560+
)
561+
or
562+
exists(Instruction cond |
563+
cond = instr.(ConditionalBranchInstruction).getCondition() and
564+
not cond.getResultIRType() instanceof IRBooleanType and
565+
irFunc = getInstructionIRFunction(instr, irFuncText) and
566+
message =
567+
"Conditional branch instruction " + instr.toString() +
568+
" with non-Boolean condition, in function '$@'."
569+
)
570+
}
549571
}

cpp/ql/test/library-tests/ir/ir/aliased_ssa_consistency.expected

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,4 +29,6 @@ fieldAddressOnNonPointer
2929
thisArgumentIsNonPointer
3030
| ir.cpp:2546:34:2546:34 | Call: call to operator bool | Call instruction 'Call: call to operator bool' has a `this` argument operand that is not an address, in function '$@'. | ir.cpp:2545:6:2545:23 | void this_inconsistency(bool) | void this_inconsistency(bool) |
3131
nonUniqueIRVariable
32+
nonBooleanOperand
33+
| try_except.c:35:13:35:13 | ConditionalBranch: b | Conditional branch instruction ConditionalBranch: b with non-Boolean condition, in function '$@'. | try_except.c:32:6:32:6 | void h(int) | void h(int) |
3234
missingCppType

cpp/ql/test/library-tests/ir/ir/aliased_ssa_consistency_unsound.expected

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,4 +29,6 @@ fieldAddressOnNonPointer
2929
thisArgumentIsNonPointer
3030
| ir.cpp:2546:34:2546:34 | Call: call to operator bool | Call instruction 'Call: call to operator bool' has a `this` argument operand that is not an address, in function '$@'. | ir.cpp:2545:6:2545:23 | void this_inconsistency(bool) | void this_inconsistency(bool) |
3131
nonUniqueIRVariable
32+
nonBooleanOperand
33+
| try_except.c:35:13:35:13 | ConditionalBranch: b | Conditional branch instruction ConditionalBranch: b with non-Boolean condition, in function '$@'. | try_except.c:32:6:32:6 | void h(int) | void h(int) |
3234
missingCppType

cpp/ql/test/library-tests/ir/ir/raw_consistency.expected

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,4 +38,6 @@ fieldAddressOnNonPointer
3838
thisArgumentIsNonPointer
3939
| ir.cpp:2546:34:2546:34 | Call: call to operator bool | Call instruction 'Call: call to operator bool' has a `this` argument operand that is not an address, in function '$@'. | ir.cpp:2545:6:2545:23 | void this_inconsistency(bool) | void this_inconsistency(bool) |
4040
nonUniqueIRVariable
41+
nonBooleanOperand
42+
| try_except.c:35:13:35:13 | ConditionalBranch: b | Conditional branch instruction ConditionalBranch: b with non-Boolean condition, in function '$@'. | try_except.c:32:6:32:6 | void h(int) | void h(int) |
4143
missingCppType

cpp/ql/test/library-tests/ir/ir/unaliased_ssa_consistency.expected

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,4 +29,6 @@ fieldAddressOnNonPointer
2929
thisArgumentIsNonPointer
3030
| ir.cpp:2546:34:2546:34 | Call: call to operator bool | Call instruction 'Call: call to operator bool' has a `this` argument operand that is not an address, in function '$@'. | ir.cpp:2545:6:2545:23 | void this_inconsistency(bool) | void this_inconsistency(bool) |
3131
nonUniqueIRVariable
32+
nonBooleanOperand
33+
| try_except.c:35:13:35:13 | ConditionalBranch: b | Conditional branch instruction ConditionalBranch: b with non-Boolean condition, in function '$@'. | try_except.c:32:6:32:6 | void h(int) | void h(int) |
3234
missingCppType

cpp/ql/test/library-tests/ir/ir/unaliased_ssa_consistency_unsound.expected

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,4 +29,6 @@ fieldAddressOnNonPointer
2929
thisArgumentIsNonPointer
3030
| ir.cpp:2546:34:2546:34 | Call: call to operator bool | Call instruction 'Call: call to operator bool' has a `this` argument operand that is not an address, in function '$@'. | ir.cpp:2545:6:2545:23 | void this_inconsistency(bool) | void this_inconsistency(bool) |
3131
nonUniqueIRVariable
32+
nonBooleanOperand
33+
| try_except.c:35:13:35:13 | ConditionalBranch: b | Conditional branch instruction ConditionalBranch: b with non-Boolean condition, in function '$@'. | try_except.c:32:6:32:6 | void h(int) | void h(int) |
3234
missingCppType

cpp/ql/test/library-tests/ir/ssa/aliased_ssa_consistency.expected

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,4 +28,5 @@ nonUniqueEnclosingIRFunction
2828
fieldAddressOnNonPointer
2929
thisArgumentIsNonPointer
3030
nonUniqueIRVariable
31+
nonBooleanOperand
3132
missingCppType

cpp/ql/test/library-tests/ir/ssa/aliased_ssa_consistency_unsound.expected

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,4 +28,5 @@ nonUniqueEnclosingIRFunction
2828
fieldAddressOnNonPointer
2929
thisArgumentIsNonPointer
3030
nonUniqueIRVariable
31+
nonBooleanOperand
3132
missingCppType

0 commit comments

Comments
 (0)