Skip to content

Commit 6dba2a4

Browse files
committed
C++: Fix type error for comparisons in C code.
1 parent e1eeecf commit 6dba2a4

File tree

2 files changed

+37
-2
lines changed

2 files changed

+37
-2
lines changed

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,7 @@ newtype TInstructionTag =
9898
} or
9999
CoAwaitBranchTag() or
100100
BinaryOperationOperationTag() or
101+
BinaryOperationConversionTag() or
101102
NotExprConversionTag()
102103

103104
class InstructionTag extends TInstructionTag {
@@ -291,5 +292,7 @@ string getInstructionTagId(TInstructionTag tag) {
291292
or
292293
tag = BinaryOperationOperationTag() and result = "BinaryOperationOperation"
293294
or
295+
tag = BinaryOperationConversionTag() and result = "BinaryOperationConversion"
296+
or
294297
tag = NotExprConversionTag() and result = "NotExprConversion"
295298
}

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

Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1861,21 +1861,40 @@ class TranslatedBinaryOperation extends TranslatedNonConstantExpr {
18611861
}
18621862

18631863
final override Instruction getResult() {
1864-
result = this.getInstruction(BinaryOperationOperationTag())
1864+
if this.shouldHaveConversion()
1865+
then result = this.getInstruction(BinaryOperationConversionTag())
1866+
else result = this.getInstruction(BinaryOperationOperationTag())
1867+
}
1868+
1869+
private predicate shouldHaveConversion() {
1870+
exists(TranslatedElement parent, Type t |
1871+
parent = this.getParent() and
1872+
parent.expectsBooleanChild(this) and
1873+
exists(comparisonOpcode(expr)) and
1874+
t = super.getExprType() and
1875+
not t instanceof BoolType
1876+
)
18651877
}
18661878

18671879
final override predicate hasInstruction(Opcode opcode, InstructionTag tag, CppType resultType) {
18681880
opcode = this.getOpcode() and
18691881
tag = BinaryOperationOperationTag() and
18701882
resultType = this.getResultType()
1883+
or
1884+
this.shouldHaveConversion() and
1885+
opcode instanceof Opcode::Convert and
1886+
tag = BinaryOperationConversionTag() and
1887+
resultType = getIntType()
18711888
}
18721889

18731890
override Instruction getFirstInstruction(EdgeKind kind) {
18741891
result = this.getLeftOperand().getFirstInstruction(kind)
18751892
}
18761893

18771894
override Instruction getALastInstructionInternal() {
1878-
result = this.getInstruction(BinaryOperationOperationTag())
1895+
if this.shouldHaveConversion()
1896+
then result = this.getInstruction(BinaryOperationConversionTag())
1897+
else result = this.getInstruction(BinaryOperationOperationTag())
18791898
}
18801899

18811900
final override TranslatedElement getChildInternal(int id) {
@@ -1900,10 +1919,23 @@ class TranslatedBinaryOperation extends TranslatedNonConstantExpr {
19001919
operandTag instanceof RightOperandTag and
19011920
result = this.getRightOperand().getResult()
19021921
)
1922+
or
1923+
this.shouldHaveConversion() and
1924+
tag = BinaryOperationConversionTag() and
1925+
operandTag instanceof UnaryOperandTag and
1926+
result = this.getInstruction(BinaryOperationOperationTag())
19031927
}
19041928

19051929
override Instruction getInstructionSuccessorInternal(InstructionTag tag, EdgeKind kind) {
19061930
tag = BinaryOperationOperationTag() and
1931+
if this.shouldHaveConversion()
1932+
then
1933+
kind instanceof GotoEdge and
1934+
result = this.getInstruction(BinaryOperationConversionTag())
1935+
else result = this.getParent().getChildSuccessor(this, kind)
1936+
or
1937+
this.shouldHaveConversion() and
1938+
tag = BinaryOperationConversionTag() and
19071939
result = this.getParent().getChildSuccessor(this, kind)
19081940
}
19091941

0 commit comments

Comments
 (0)