Skip to content

Commit 47f3116

Browse files
committed
C++: Fix type error for 'NotExpr's in C code.
1 parent 893b413 commit 47f3116

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: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,8 @@ newtype TInstructionTag =
9696
exists(Expr e | exists(e.getImplicitDestructorCall(index))) or
9797
exists(Stmt s | exists(s.getImplicitDestructorCall(index)))
9898
} or
99-
CoAwaitBranchTag()
99+
CoAwaitBranchTag() or
100+
NotExprConversionTag()
100101

101102
class InstructionTag extends TInstructionTag {
102103
final string toString() { result = getInstructionTagId(this) }
@@ -286,4 +287,6 @@ string getInstructionTagId(TInstructionTag tag) {
286287
)
287288
or
288289
tag = CoAwaitBranchTag() and result = "CoAwaitBranch"
290+
or
291+
tag = NotExprConversionTag() and result = "NotExprConversion"
289292
}

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

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1359,6 +1359,15 @@ class TranslatedNotExpr extends TranslatedNonConstantExpr {
13591359

13601360
override Type getExprType() { result instanceof BoolType }
13611361

1362+
private predicate shouldHaveConversion() {
1363+
exists(TranslatedElement parent, Type t |
1364+
parent = this.getParent() and
1365+
parent.expectsBooleanChild(this) and
1366+
t = super.getExprType() and
1367+
not t instanceof BoolType
1368+
)
1369+
}
1370+
13621371
private Type getOperandType() { result = this.getOperand().getExprType().getUnspecifiedType() }
13631372

13641373
predicate shouldGenerateEq() { not this.getOperandType() instanceof BoolType }
@@ -1386,10 +1395,24 @@ class TranslatedNotExpr extends TranslatedNonConstantExpr {
13861395
if this.shouldGenerateEq()
13871396
then opcode instanceof Opcode::CompareEQ
13881397
else opcode instanceof Opcode::LogicalNot
1398+
or
1399+
this.shouldHaveConversion() and
1400+
tag = NotExprConversionTag() and
1401+
opcode instanceof Opcode::Convert and
1402+
resultType = getIntType()
13891403
}
13901404

13911405
final override Instruction getInstructionSuccessorInternal(InstructionTag tag, EdgeKind kind) {
1406+
this.shouldHaveConversion() and
13921407
tag = NotExprOperationTag() and
1408+
kind instanceof GotoEdge and
1409+
result = this.getInstruction(NotExprConversionTag())
1410+
or
1411+
(
1412+
if this.shouldHaveConversion()
1413+
then tag = NotExprConversionTag()
1414+
else tag = NotExprOperationTag()
1415+
) and
13931416
result = this.getParent().getChildSuccessor(this, kind)
13941417
or
13951418
tag = NotExprConstantTag() and
@@ -1418,13 +1441,22 @@ class TranslatedNotExpr extends TranslatedNonConstantExpr {
14181441
operandTag instanceof UnaryOperandTag and
14191442
result = this.getOperand().getResult()
14201443
)
1444+
or
1445+
this.shouldHaveConversion() and
1446+
tag = NotExprConversionTag() and
1447+
operandTag instanceof UnaryOperandTag and
1448+
result = this.getInstruction(NotExprOperationTag())
14211449
}
14221450

14231451
private TranslatedExpr getOperand() {
14241452
result = getTranslatedExpr(expr.getOperand().getFullyConverted())
14251453
}
14261454

1427-
final override Instruction getResult() { result = this.getInstruction(NotExprOperationTag()) }
1455+
final override Instruction getResult() {
1456+
if this.shouldHaveConversion()
1457+
then result = this.getInstruction(NotExprConversionTag())
1458+
else result = this.getInstruction(NotExprOperationTag())
1459+
}
14281460

14291461
override string getInstructionConstantValue(InstructionTag tag) {
14301462
this.shouldGenerateEq() and

0 commit comments

Comments
 (0)