Skip to content

Commit 7dd2677

Browse files
authored
Merge pull request github#3950 from MathiasVP/simple-range-analysis-unsigned-multiplication-tests
C++: Add test cases for range analysis for unsigned multiplication
2 parents dcff87f + 174b304 commit 7dd2677

File tree

4 files changed

+133
-2
lines changed

4 files changed

+133
-2
lines changed

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

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -440,6 +440,47 @@
440440
| test.c:424:3:424:3 | i | -2.147483648E9 |
441441
| test.c:424:13:424:13 | j | -2.147483648E9 |
442442
| test.c:425:7:425:7 | i | -2.147483648E9 |
443+
| test.c:432:12:432:12 | a | 0.0 |
444+
| test.c:432:17:432:17 | a | 3.0 |
445+
| test.c:432:33:432:33 | b | 0.0 |
446+
| test.c:432:38:432:38 | b | 5.0 |
447+
| test.c:433:13:433:13 | a | 3.0 |
448+
| test.c:433:15:433:15 | b | 5.0 |
449+
| test.c:434:5:434:9 | total | 0.0 |
450+
| test.c:434:14:434:14 | r | -2.147483648E9 |
451+
| test.c:436:12:436:12 | a | 0.0 |
452+
| test.c:436:17:436:17 | a | 3.0 |
453+
| test.c:436:33:436:33 | b | 0.0 |
454+
| test.c:436:38:436:38 | b | 0.0 |
455+
| test.c:437:13:437:13 | a | 3.0 |
456+
| test.c:437:15:437:15 | b | 0.0 |
457+
| test.c:438:5:438:9 | total | -2.147483648E9 |
458+
| test.c:438:14:438:14 | r | -2.147483648E9 |
459+
| test.c:440:12:440:12 | a | 0.0 |
460+
| test.c:440:17:440:17 | a | 3.0 |
461+
| test.c:440:34:440:34 | b | 0.0 |
462+
| test.c:440:39:440:39 | b | 13.0 |
463+
| test.c:441:13:441:13 | a | 3.0 |
464+
| test.c:441:15:441:15 | b | 13.0 |
465+
| test.c:442:5:442:9 | total | -2.147483648E9 |
466+
| test.c:442:14:442:14 | r | -2.147483648E9 |
467+
| test.c:445:10:445:14 | total | -2.147483648E9 |
468+
| test.c:451:12:451:12 | b | 0.0 |
469+
| test.c:451:17:451:17 | b | 5.0 |
470+
| test.c:452:16:452:16 | b | 5.0 |
471+
| test.c:453:5:453:9 | total | 0.0 |
472+
| test.c:453:14:453:14 | r | -2.147483648E9 |
473+
| test.c:455:12:455:12 | b | 0.0 |
474+
| test.c:455:17:455:17 | b | 0.0 |
475+
| test.c:456:16:456:16 | b | 0.0 |
476+
| test.c:457:5:457:9 | total | -2.147483648E9 |
477+
| test.c:457:14:457:14 | r | -2.147483648E9 |
478+
| test.c:459:13:459:13 | b | 0.0 |
479+
| test.c:459:18:459:18 | b | 13.0 |
480+
| test.c:460:16:460:16 | b | 13.0 |
481+
| test.c:461:5:461:9 | total | -2.147483648E9 |
482+
| test.c:461:14:461:14 | r | -2.147483648E9 |
483+
| test.c:464:10:464:14 | total | -2.147483648E9 |
443484
| test.cpp:10:7:10:7 | b | -2.147483648E9 |
444485
| test.cpp:11:5:11:5 | x | -2.147483648E9 |
445486
| test.cpp:13:10:13:10 | x | -2.147483648E9 |

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

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -424,3 +424,42 @@ void test17() {
424424
i = 20 + (j -= 10);
425425
out(i); // 60 [BUG: the analysis thinks it's 2^-31 .. 2^31-1]
426426
}
427+
428+
// Tests for unsigned multiplication.
429+
int test_unsigned_mult01(unsigned int a, unsigned b) {
430+
int total = 0;
431+
432+
if (3 <= a && a <= 11 && 5 <= b && b <= 23) {
433+
int r = a*b; // 15 .. 253
434+
total += r;
435+
}
436+
if (3 <= a && a <= 11 && 0 <= b && b <= 23) {
437+
int r = a*b; // 0 .. 253
438+
total += r;
439+
}
440+
if (3 <= a && a <= 11 && 13 <= b && b <= 23) {
441+
int r = a*b; // 39 .. 253
442+
total += r;
443+
}
444+
445+
return total;
446+
}
447+
448+
int test_unsigned_mult02(unsigned b) {
449+
int total = 0;
450+
451+
if (5 <= b && b <= 23) {
452+
int r = 11*b; // 55 .. 253
453+
total += r;
454+
}
455+
if (0 <= b && b <= 23) {
456+
int r = 11*b; // 0 .. 253
457+
total += r;
458+
}
459+
if (13 <= b && b <= 23) {
460+
int r = 11*b; // 143 .. 253
461+
total += r;
462+
}
463+
464+
return total;
465+
}

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

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -440,6 +440,47 @@
440440
| test.c:424:3:424:3 | i | 2.147483647E9 |
441441
| test.c:424:13:424:13 | j | 2.147483647E9 |
442442
| test.c:425:7:425:7 | i | 2.147483647E9 |
443+
| test.c:432:12:432:12 | a | 4.294967295E9 |
444+
| test.c:432:17:432:17 | a | 4.294967295E9 |
445+
| test.c:432:33:432:33 | b | 4.294967295E9 |
446+
| test.c:432:38:432:38 | b | 4.294967295E9 |
447+
| test.c:433:13:433:13 | a | 11.0 |
448+
| test.c:433:15:433:15 | b | 23.0 |
449+
| test.c:434:5:434:9 | total | 0.0 |
450+
| test.c:434:14:434:14 | r | 2.147483647E9 |
451+
| test.c:436:12:436:12 | a | 4.294967295E9 |
452+
| test.c:436:17:436:17 | a | 4.294967295E9 |
453+
| test.c:436:33:436:33 | b | 4.294967295E9 |
454+
| test.c:436:38:436:38 | b | 4.294967295E9 |
455+
| test.c:437:13:437:13 | a | 11.0 |
456+
| test.c:437:15:437:15 | b | 23.0 |
457+
| test.c:438:5:438:9 | total | 2.147483647E9 |
458+
| test.c:438:14:438:14 | r | 2.147483647E9 |
459+
| test.c:440:12:440:12 | a | 4.294967295E9 |
460+
| test.c:440:17:440:17 | a | 4.294967295E9 |
461+
| test.c:440:34:440:34 | b | 4.294967295E9 |
462+
| test.c:440:39:440:39 | b | 4.294967295E9 |
463+
| test.c:441:13:441:13 | a | 11.0 |
464+
| test.c:441:15:441:15 | b | 23.0 |
465+
| test.c:442:5:442:9 | total | 2.147483647E9 |
466+
| test.c:442:14:442:14 | r | 2.147483647E9 |
467+
| test.c:445:10:445:14 | total | 2.147483647E9 |
468+
| test.c:451:12:451:12 | b | 4.294967295E9 |
469+
| test.c:451:17:451:17 | b | 4.294967295E9 |
470+
| test.c:452:16:452:16 | b | 23.0 |
471+
| test.c:453:5:453:9 | total | 0.0 |
472+
| test.c:453:14:453:14 | r | 2.147483647E9 |
473+
| test.c:455:12:455:12 | b | 4.294967295E9 |
474+
| test.c:455:17:455:17 | b | 4.294967295E9 |
475+
| test.c:456:16:456:16 | b | 23.0 |
476+
| test.c:457:5:457:9 | total | 2.147483647E9 |
477+
| test.c:457:14:457:14 | r | 2.147483647E9 |
478+
| test.c:459:13:459:13 | b | 4.294967295E9 |
479+
| test.c:459:18:459:18 | b | 4.294967295E9 |
480+
| test.c:460:16:460:16 | b | 23.0 |
481+
| test.c:461:5:461:9 | total | 2.147483647E9 |
482+
| test.c:461:14:461:14 | r | 2.147483647E9 |
483+
| test.c:464:10:464:14 | total | 2.147483647E9 |
443484
| test.cpp:10:7:10:7 | b | 2.147483647E9 |
444485
| test.cpp:11:5:11:5 | x | 2.147483647E9 |
445486
| test.cpp:13:10:13:10 | x | 2.147483647E9 |

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

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -365,7 +365,7 @@ int callCommand(void)
365365
return 0;
366366
}
367367

368-
int shifts(void)
368+
void shifts(void)
369369
{
370370
unsigned int x = 3;
371371

@@ -374,11 +374,21 @@ int shifts(void)
374374
if (x >> 1 == 1) {} // always true [NOT DETECTED]
375375
}
376376

377-
int bitwise_ands()
377+
void bitwise_ands()
378378
{
379379
unsigned int x = 0xFF;
380380

381381
if ((x & 2) >= 1) {}
382382
if ((x & 2) >= 2) {}
383383
if ((x & 2) >= 3) {} // always false
384384
}
385+
386+
void unsigned_mult(unsigned int x, unsigned int y) {
387+
if(x < 13 && y < 35) {
388+
if(x * y > 1024) {} // always false [NOT DETECTED]
389+
if(x * y < 204) {}
390+
if(x >= 3 && y >= 2) {
391+
if(x * y < 5) {} // always false [NOT DETECTED]
392+
}
393+
}
394+
}

0 commit comments

Comments
 (0)