Skip to content

Commit 8869b9e

Browse files
committed
Include cases where both guards are required.
The previous logic would succeed if any of the guards were present, but that wasn't sufficient in all cases.
1 parent 1c881e3 commit 8869b9e

File tree

1 file changed

+15
-18
lines changed

1 file changed

+15
-18
lines changed

cpp/common/src/codingstandards/cpp/UndefinedBehavior.qll

Lines changed: 15 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -21,19 +21,12 @@ class ShiftByNegativeOrGreaterPrecisionOperand extends UndefinedBehavior, BitShi
2121
string reason;
2222

2323
ShiftByNegativeOrGreaterPrecisionOperand() {
24-
(
25-
getPrecision(this.getLeftOperand().getExplicitlyConverted().getUnderlyingType()) <=
26-
upperBound(this.getRightOperand()) and
27-
reason =
28-
"The operand " + this.getLeftOperand() + " is shifted by an expression " +
29-
this.getRightOperand() + " whose upper bound (" + upperBound(this.getRightOperand()) +
30-
") is greater than or equal to the precision."
31-
or
32-
lowerBound(this.getRightOperand()) < 0 and
33-
reason =
34-
"The operand " + this.getLeftOperand() + " is shifted by an expression " +
35-
this.getRightOperand() + " which may be negative."
36-
) and
24+
getPrecision(this.getLeftOperand().getExplicitlyConverted().getUnderlyingType()) <=
25+
upperBound(this.getRightOperand()) and
26+
reason =
27+
"The operand " + this.getLeftOperand() + " is shifted by an expression " +
28+
this.getRightOperand() + " whose upper bound (" + upperBound(this.getRightOperand()) +
29+
") is greater than or equal to the precision." and
3730
/*
3831
* this statement is not at a basic block where
3932
* `this_rhs < PRECISION(...)` is ensured
@@ -49,18 +42,22 @@ class ShiftByNegativeOrGreaterPrecisionOperand extends UndefinedBehavior, BitShi
4942
|
5043
globalValueNumber(lTLhs) = globalValueNumber(this.getRightOperand()) and
5144
gc.ensuresLt(lTLhs, precisionCall, 0, block, true)
52-
) and
45+
)
46+
or
47+
lowerBound(this.getRightOperand()) < 0 and
48+
reason =
49+
"The operand " + this.getLeftOperand() + " is shifted by an expression " +
50+
this.getRightOperand() + " which may be negative." and
5351
/*
5452
* this statement is not at a basic block where
55-
* `this_rhs < 0` is ensured
53+
* `this_rhs > 0` is ensured
5654
*/
5755

5856
not exists(GuardCondition gc, BasicBlock block, Expr literalZero, Expr lTLhs |
5957
block = this.getBasicBlock() and
60-
literalZero instanceof LiteralZero
61-
|
58+
literalZero instanceof LiteralZero and
6259
globalValueNumber(lTLhs) = globalValueNumber(this.getRightOperand()) and
63-
gc.ensuresLt(lTLhs, literalZero, 0, block, true)
60+
gc.ensuresLt(literalZero, lTLhs, 0, block, true)
6461
)
6562
}
6663

0 commit comments

Comments
 (0)