Skip to content

Commit 36479d3

Browse files
committed
Support to keep bounds derived on implicit integer casts.
1 parent cf8efbb commit 36479d3

File tree

2 files changed

+23
-0
lines changed

2 files changed

+23
-0
lines changed

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,13 @@ private predicate boundFlowStepSsa(
187187
guard.controls(op2.getUse().getBlock(), testIsTrue) and
188188
reason = TCondReason(guard)
189189
)
190+
or
191+
exists(IRGuardCondition guard, boolean testIsTrue, SafeCastInstruction cast |
192+
valueNumberOfOperand(op2) = valueNumber(cast.getUnary()) and
193+
guard = boundFlowCond(valueNumber(cast), op1, delta, upper, testIsTrue) and
194+
guard.controls(op2.getUse().getBlock(), testIsTrue) and
195+
reason = TCondReason(guard)
196+
)
190197
}
191198

192199
/**

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

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -192,3 +192,19 @@ int test16(int i) {
192192
long l;
193193
l = i;
194194
}
195+
196+
// implicit integer casts
197+
void test17(int i, long l) {
198+
if (i < l) {
199+
sink(i);
200+
}
201+
if (i < l - 2) {
202+
sink (i);
203+
}
204+
}
205+
206+
void test18(int x, int y) {
207+
if (x < y - 2) {
208+
sink(x);
209+
}
210+
}

0 commit comments

Comments
 (0)