Skip to content

Commit b827e7a

Browse files
committed
C++: Fix use-after-cast bug in SimpleRangeAnalysis
Like everywhere else in the range analysis, operands to comparison operators must be considered in their fully-converted form.
1 parent ad61b4f commit b827e7a

File tree

3 files changed

+8
-8
lines changed

3 files changed

+8
-8
lines changed

cpp/ql/src/semmle/code/cpp/rangeanalysis/RangeAnalysisUtils.qll

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,8 @@ predicate relOp(
4747
RelationalOperation rel, Expr lhs, Expr rhs,
4848
RelationDirection dir, RelationStrictness strict
4949
) {
50-
lhs = rel.getLeftOperand() and
51-
rhs = rel.getRightOperand() and
50+
lhs = rel.getLeftOperand().getFullyConverted() and
51+
rhs = rel.getRightOperand().getFullyConverted() and
5252
((rel instanceof LTExpr and dir = Lesser() and strict = Strict()) or
5353
(rel instanceof LEExpr and dir = Lesser() and strict = Nonstrict()) or
5454
(rel instanceof GTExpr and dir = Greater() and strict = Strict()) or
@@ -104,8 +104,8 @@ predicate relOpWithSwapAndNegate(
104104
*/
105105
private
106106
predicate eqOp(EqualityOperation cmp, Expr lhs, Expr rhs, boolean isEQ) {
107-
lhs = cmp.getLeftOperand() and
108-
rhs = cmp.getRightOperand() and
107+
lhs = cmp.getLeftOperand().getFullyConverted() and
108+
rhs = cmp.getRightOperand().getFullyConverted() and
109109
((cmp instanceof EQExpr and isEQ = true) or
110110
(cmp instanceof NEExpr and isEQ = false))
111111
}

cpp/ql/test/library-tests/rangeanalysis/SimpleRangeAnalysis/test.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ void use_after_cast(unsigned char c)
7474
unsigned short c_times_2 = c + c;
7575
if ((unsigned char)c_times_2 == 0)
7676
{
77-
c_times_2; // BUG: upper bound should be 510, not 0
77+
c_times_2;
7878
}
79-
c_times_2; // BUG: upper bound should be 510, not 255
79+
c_times_2;
8080
}

cpp/ql/test/library-tests/rangeanalysis/SimpleRangeAnalysis/upperBound.expected

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -462,5 +462,5 @@
462462
| test.cpp:74:30:74:30 | c | 255.0 |
463463
| test.cpp:74:34:74:34 | c | 255.0 |
464464
| test.cpp:75:22:75:30 | c_times_2 | 510.0 |
465-
| test.cpp:77:5:77:13 | c_times_2 | 0.0 |
466-
| test.cpp:79:3:79:11 | c_times_2 | 255.0 |
465+
| test.cpp:77:5:77:13 | c_times_2 | 510.0 |
466+
| test.cpp:79:3:79:11 | c_times_2 | 510.0 |

0 commit comments

Comments
 (0)