Skip to content

Commit 62a5aef

Browse files
Merge pull request #410 from jbj/range-analysis-tests
C++: Tests for two range analysis bugs
2 parents b671ef5 + 4a02b39 commit 62a5aef

File tree

2 files changed

+25
-0
lines changed

2 files changed

+25
-0
lines changed

cpp/ql/test/query-tests/Likely Bugs/Arithmetic/PointlessComparison/PointlessComparison.c

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -241,3 +241,27 @@ void macroExpansionTest() {
241241
MAYBE_DO(x = 1); // GOOD (the problem is in the macro)
242242
MAYBE_DO(if (global_setting >= 0) {x = 2;}); // BAD (the problem is in the invocation)
243243
}
244+
245+
int overeager_wraparound(unsigned int u32bound, unsigned long long u64bound) {
246+
unsigned int u32idx;
247+
unsigned long long u64idx;
248+
249+
for (u32idx = 1; u32idx < u32bound; u32idx++) {
250+
if (u32idx == 0) // BAD [NOT DETECTED]
251+
return 0;
252+
}
253+
254+
for (u64idx = 1; u64idx < u64bound; u64idx++) {
255+
if (u64idx == 0) // BAD [NOT DETECTED]
256+
return 0;
257+
}
258+
259+
return 1;
260+
}
261+
262+
int negative_zero(double dbl) {
263+
if (dbl >= 0) {
264+
return dbl >= -dbl; // GOOD [FALSE POSITIVE]
265+
}
266+
return 0;
267+
}

cpp/ql/test/query-tests/Likely Bugs/Arithmetic/PointlessComparison/PointlessComparison.expected

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,5 +31,6 @@
3131
| PointlessComparison.c:126:12:126:18 | ... >= ... | Comparison is always true because a >= 20. |
3232
| PointlessComparison.c:129:12:129:16 | ... > ... | Comparison is always false because a <= 3. |
3333
| PointlessComparison.c:197:7:197:11 | ... < ... | Comparison is always false because x >= 0. |
34+
| PointlessComparison.c:264:12:264:22 | ... >= ... | Comparison is always true because dbl >= 0 and -0 >= - .... |
3435
| RegressionTests.cpp:57:7:57:22 | ... <= ... | Comparison is always true because * ... <= 4294967295. |
3536
| Templates.cpp:9:10:9:24 | ... <= ... | Comparison is always true because local <= 32767. |

0 commit comments

Comments
 (0)