Skip to content

Commit c110508

Browse files
committed
C++: Add tests to expose potential improvements available to SimpleRangeAnalysis
1 parent 59b3d55 commit c110508

File tree

9 files changed

+69
-32
lines changed

9 files changed

+69
-32
lines changed

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

Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -594,6 +594,9 @@
594594
| test.c:659:9:659:9 | u | 0 |
595595
| test.c:664:12:664:12 | s | -2147483648 |
596596
| test.c:665:7:665:8 | s2 | -4 |
597+
| test.c:670:7:670:7 | x | -2147483648 |
598+
| test.c:671:9:671:9 | y | -2147483648 |
599+
| test.c:675:7:675:7 | y | -2147483648 |
597600
| test.cpp:10:7:10:7 | b | -2147483648 |
598601
| test.cpp:11:5:11:5 | x | -2147483648 |
599602
| test.cpp:13:10:13:10 | x | -2147483648 |
@@ -647,16 +650,18 @@
647650
| test.cpp:97:10:97:10 | i | -2147483648 |
648651
| test.cpp:97:22:97:22 | i | -2147483648 |
649652
| test.cpp:98:5:98:5 | i | -2147483648 |
650-
| test.cpp:105:7:105:7 | n | -32768 |
651-
| test.cpp:108:7:108:7 | n | 0 |
652-
| test.cpp:109:5:109:5 | n | 1 |
653-
| test.cpp:111:5:111:5 | n | 0 |
654-
| test.cpp:114:8:114:8 | n | 0 |
655-
| test.cpp:115:5:115:5 | n | 0 |
656-
| test.cpp:117:5:117:5 | n | 1 |
657-
| test.cpp:120:3:120:3 | n | 0 |
658-
| test.cpp:120:8:120:8 | n | 1 |
659-
| test.cpp:120:12:120:12 | n | 0 |
660-
| test.cpp:121:4:121:4 | n | 0 |
661-
| test.cpp:121:8:121:8 | n | 0 |
662-
| test.cpp:121:12:121:12 | n | 1 |
653+
| test.cpp:98:9:98:9 | i | -2147483648 |
654+
| test.cpp:99:5:99:5 | i | -2147483648 |
655+
| test.cpp:106:7:106:7 | n | -32768 |
656+
| test.cpp:109:7:109:7 | n | 0 |
657+
| test.cpp:110:5:110:5 | n | 1 |
658+
| test.cpp:112:5:112:5 | n | 0 |
659+
| test.cpp:115:8:115:8 | n | 0 |
660+
| test.cpp:116:5:116:5 | n | 0 |
661+
| test.cpp:118:5:118:5 | n | 1 |
662+
| test.cpp:121:3:121:3 | n | 0 |
663+
| test.cpp:121:8:121:8 | n | 1 |
664+
| test.cpp:121:12:121:12 | n | 0 |
665+
| test.cpp:122:4:122:4 | n | 0 |
666+
| test.cpp:122:8:122:8 | n | 0 |
667+
| test.cpp:122:12:122:12 | n | 1 |

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,5 +15,5 @@
1515
| test.c:394:20:394:36 | ... ? ... : ... | 0.0 | 0.0 | 100.0 |
1616
| test.c:606:5:606:14 | ... ? ... : ... | 0.0 | 1.0 | 0.0 |
1717
| test.c:607:5:607:14 | ... ? ... : ... | 0.0 | 0.0 | 1.0 |
18-
| test.cpp:120:3:120:12 | ... ? ... : ... | 0.0 | 1.0 | 0.0 |
19-
| test.cpp:121:3:121:12 | ... ? ... : ... | 0.0 | 0.0 | 1.0 |
18+
| test.cpp:121:3:121:12 | ... ? ... : ... | 0.0 | 1.0 | 0.0 |
19+
| test.cpp:122:3:122:12 | ... ? ... : ... | 0.0 | 0.0 | 1.0 |

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,5 +15,5 @@
1515
| test.c:394:20:394:36 | ... ? ... : ... | 100.0 | 99.0 | 100.0 |
1616
| test.c:606:5:606:14 | ... ? ... : ... | 32767.0 | 32767.0 | 0.0 |
1717
| test.c:607:5:607:14 | ... ? ... : ... | 32767.0 | 0.0 | 32767.0 |
18-
| test.cpp:120:3:120:12 | ... ? ... : ... | 32767.0 | 32767.0 | 0.0 |
19-
| test.cpp:121:3:121:12 | ... ? ... : ... | 32767.0 | 0.0 | 32767.0 |
18+
| test.cpp:121:3:121:12 | ... ? ... : ... | 32767.0 | 32767.0 | 0.0 |
19+
| test.cpp:122:3:122:12 | ... ? ... : ... | 32767.0 | 0.0 | 32767.0 |

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

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -664,3 +664,18 @@ void test_mod(int s) {
664664
int s2 = s % 5;
665665
out(s2); // -4 .. 4
666666
}
667+
668+
void exit(int);
669+
void guard_with_exit(int x, int y) {
670+
if (x) {
671+
if (y != 0) {
672+
exit(0);
673+
}
674+
}
675+
out(y); // ..
676+
677+
// This test ensures that we correctly identify
678+
// that the upper bound for y is max_int when calling `out(y)`.
679+
// The RangeSsa will place guardPhy on `out(y)`, and consequently there is no
680+
// frontier phi node at out(y).
681+
}

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,7 @@ int ref_to_number(int &i, const int &ci, int &aliased) {
9595
return alias;
9696

9797
for (; i <= 12345; i++) { // test that widening works for references
98+
i = i;
9899
i;
99100
}
100101

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

Lines changed: 19 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -594,6 +594,9 @@
594594
| test.c:659:9:659:9 | u | 4294967295 |
595595
| test.c:664:12:664:12 | s | 2147483647 |
596596
| test.c:665:7:665:8 | s2 | 4 |
597+
| test.c:670:7:670:7 | x | 2147483647 |
598+
| test.c:671:9:671:9 | y | 2147483647 |
599+
| test.c:675:7:675:7 | y | 2147483647 |
597600
| test.cpp:10:7:10:7 | b | 2147483647 |
598601
| test.cpp:11:5:11:5 | x | 2147483647 |
599602
| test.cpp:13:10:13:10 | x | 2147483647 |
@@ -646,17 +649,19 @@
646649
| test.cpp:95:12:95:16 | alias | 2147483647 |
647650
| test.cpp:97:10:97:10 | i | 65535 |
648651
| test.cpp:97:22:97:22 | i | 32767 |
649-
| test.cpp:98:5:98:5 | i | 32767 |
650-
| test.cpp:105:7:105:7 | n | 32767 |
651-
| test.cpp:108:7:108:7 | n | 32767 |
652-
| test.cpp:109:5:109:5 | n | 32767 |
653-
| test.cpp:111:5:111:5 | n | 0 |
654-
| test.cpp:114:8:114:8 | n | 32767 |
655-
| test.cpp:115:5:115:5 | n | 0 |
656-
| test.cpp:117:5:117:5 | n | 32767 |
657-
| test.cpp:120:3:120:3 | n | 32767 |
658-
| test.cpp:120:8:120:8 | n | 32767 |
659-
| test.cpp:120:12:120:12 | n | 0 |
660-
| test.cpp:121:4:121:4 | n | 32767 |
661-
| test.cpp:121:8:121:8 | n | 0 |
662-
| test.cpp:121:12:121:12 | n | 32767 |
652+
| test.cpp:98:5:98:5 | i | 2147483647 |
653+
| test.cpp:98:9:98:9 | i | 32767 |
654+
| test.cpp:99:5:99:5 | i | 32767 |
655+
| test.cpp:106:7:106:7 | n | 32767 |
656+
| test.cpp:109:7:109:7 | n | 32767 |
657+
| test.cpp:110:5:110:5 | n | 32767 |
658+
| test.cpp:112:5:112:5 | n | 0 |
659+
| test.cpp:115:8:115:8 | n | 32767 |
660+
| test.cpp:116:5:116:5 | n | 0 |
661+
| test.cpp:118:5:118:5 | n | 32767 |
662+
| test.cpp:121:3:121:3 | n | 32767 |
663+
| test.cpp:121:8:121:8 | n | 32767 |
664+
| test.cpp:121:12:121:12 | n | 0 |
665+
| test.cpp:122:4:122:4 | n | 32767 |
666+
| test.cpp:122:8:122:8 | n | 0 |
667+
| test.cpp:122:12:122:12 | n | 32767 |

cpp/ql/test/query-tests/Critical/OverflowStatic/OverflowStatic.expected

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,3 +14,4 @@
1414
| test.cpp:24:27:24:27 | 4 | Potential buffer-overflow: 'buffer1' has size 3 not 4. |
1515
| test.cpp:26:27:26:27 | 4 | Potential buffer-overflow: 'buffer2' has size 3 not 4. |
1616
| test.cpp:40:22:40:27 | amount | Potential buffer-overflow: 'buffer' has size 100 not 101. |
17+
| test.cpp:55:13:55:21 | access to array | Potential buffer-overflow: counter 'i' <= 9 but 'buffer' has 5 elements. |

cpp/ql/test/query-tests/Critical/OverflowStatic/test.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,3 +46,13 @@ void f2(char *src)
4646
ptr = &(buffer[1]);
4747
memcpy(ptr, src, 100); // BAD [NOT DETECTED]
4848
}
49+
50+
void f3() {
51+
int i;
52+
char buffer[5];
53+
for (i=0; i<10; i++) {
54+
if (i < 5) {
55+
buffer[i] = 0; // GOOD [FALSE POSITIVE]
56+
}
57+
}
58+
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ int twoReasons(int a, int b) {
115115
if (a <= 0 && b > 5) {
116116
return a < b;
117117
}
118-
if (a <= 100 && b > 105) {
118+
if (a <= 100 && b > 105) { // BUG [Not detected - this clause is always false]
119119
return a > b;
120120
}
121121
return 0;

0 commit comments

Comments
 (0)