Skip to content

Commit 6577161

Browse files
committed
C++: Insert int-to-bool conversions at binary conditional expressions.
1 parent 9d3bc7f commit 6577161

File tree

2 files changed

+55
-2
lines changed

2 files changed

+55
-2
lines changed

cpp/ql/lib/semmle/code/cpp/ir/implementation/raw/internal/InstructionTag.qll

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,8 @@ newtype TInstructionTag =
4141
ValueConditionCompareTag() or
4242
ValueConditionConstantTag() or
4343
ValueConditionConditionalBranchTag() or
44+
ValueConditionConditionalConstantTag() or
45+
ValueConditionConditionalCompareTag() or
4446
ConditionValueTrueTempAddressTag() or
4547
ConditionValueTrueConstantTag() or
4648
ConditionValueTrueStoreTag() or
@@ -171,6 +173,10 @@ string getInstructionTagId(TInstructionTag tag) {
171173
or
172174
tag = ValueConditionConditionalBranchTag() and result = "ValCondCondBranch"
173175
or
176+
tag = ValueConditionConditionalConstantTag() and result = "ValueConditionConditionalConstant"
177+
or
178+
tag = ValueConditionConditionalCompareTag() and result = "ValueConditionConditionalCompare"
179+
or
174180
tag = ValueConditionCompareTag() and result = "ValCondCondCompare"
175181
or
176182
tag = ValueConditionConstantTag() and result = "ValCondConstant"

cpp/ql/lib/semmle/code/cpp/ir/implementation/raw/internal/TranslatedExpr.qll

Lines changed: 49 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2965,18 +2965,46 @@ class TranslatedBinaryConditionalExpr extends TranslatedConditionalExpr {
29652965
result = this.getCondition().getFirstInstruction(kind)
29662966
}
29672967

2968+
private Type getConditionType() {
2969+
result = this.getCondition().getExprType().getUnspecifiedType()
2970+
}
2971+
29682972
override predicate hasInstruction(Opcode opcode, InstructionTag tag, CppType resultType) {
29692973
super.hasInstruction(opcode, tag, resultType)
29702974
or
29712975
// For the binary variant, we create our own conditional branch.
29722976
tag = ValueConditionConditionalBranchTag() and
29732977
opcode instanceof Opcode::ConditionalBranch and
29742978
resultType = getVoidType()
2979+
or
2980+
exists(Type t |
2981+
t = this.getConditionType() and
2982+
not t instanceof BoolType
2983+
|
2984+
tag = ValueConditionConditionalConstantTag() and
2985+
opcode instanceof Opcode::Constant and
2986+
resultType = getTypeForPRValue(t)
2987+
or
2988+
tag = ValueConditionConditionalCompareTag() and
2989+
opcode instanceof Opcode::CompareNE and
2990+
resultType = getBoolType()
2991+
)
29752992
}
29762993

29772994
override Instruction getInstructionSuccessorInternal(InstructionTag tag, EdgeKind kind) {
29782995
result = super.getInstructionSuccessorInternal(tag, kind)
29792996
or
2997+
not this.getConditionType() instanceof BoolType and
2998+
(
2999+
tag = ValueConditionConditionalConstantTag() and
3000+
kind instanceof GotoEdge and
3001+
result = this.getInstruction(ValueConditionConditionalCompareTag())
3002+
or
3003+
tag = ValueConditionConditionalCompareTag() and
3004+
kind instanceof GotoEdge and
3005+
result = this.getInstruction(ValueConditionConditionalBranchTag())
3006+
)
3007+
or
29803008
tag = ValueConditionConditionalBranchTag() and
29813009
(
29823010
kind instanceof TrueEdge and
@@ -2992,15 +3020,29 @@ class TranslatedBinaryConditionalExpr extends TranslatedConditionalExpr {
29923020
or
29933021
tag = ValueConditionConditionalBranchTag() and
29943022
operandTag instanceof ConditionOperandTag and
2995-
result = this.getCondition().getResult()
3023+
if this.getConditionType() instanceof BoolType
3024+
then result = this.getCondition().getResult()
3025+
else result = this.getInstruction(ValueConditionConditionalCompareTag())
3026+
or
3027+
not this.getConditionType() instanceof BoolType and
3028+
tag = ValueConditionConditionalCompareTag() and
3029+
(
3030+
operandTag instanceof LeftOperandTag and
3031+
result = this.getCondition().getResult()
3032+
or
3033+
operandTag instanceof RightOperandTag and
3034+
result = this.getInstruction(ValueConditionConditionalConstantTag())
3035+
)
29963036
}
29973037

29983038
override Instruction getChildSuccessorInternal(TranslatedElement child, EdgeKind kind) {
29993039
result = super.getChildSuccessorInternal(child, kind)
30003040
or
30013041
kind instanceof GotoEdge and
30023042
child = this.getCondition() and
3003-
result = this.getInstruction(ValueConditionConditionalBranchTag())
3043+
if this.getConditionType() instanceof BoolType
3044+
then result = this.getInstruction(ValueConditionConditionalBranchTag())
3045+
else result = this.getInstruction(ValueConditionConditionalConstantTag())
30043046
}
30053047

30063048
private TranslatedExpr getCondition() {
@@ -3017,6 +3059,11 @@ class TranslatedBinaryConditionalExpr extends TranslatedConditionalExpr {
30173059
// always converting the "then" operand to `bool`, which is almost always the wrong type.
30183060
result = getTranslatedExpr(expr.getThen().getExplicitlyConverted())
30193061
}
3062+
3063+
override string getInstructionConstantValue(InstructionTag tag) {
3064+
tag = ValueConditionConditionalConstantTag() and
3065+
result = "0"
3066+
}
30203067
}
30213068

30223069
/**

0 commit comments

Comments
 (0)