Skip to content

Commit 59ff3f3

Browse files
committed
C++: Add test cases exploring issues and potential issues with the query (especially related to simple range analysis).
1 parent e8d7925 commit 59ff3f3

File tree

2 files changed

+209
-1
lines changed

2 files changed

+209
-1
lines changed

cpp/ql/test/query-tests/Security/CWE/CWE-191/UnsignedDifferenceExpressionComparedZero/UnsignedDifferenceExpressionComparedZero.expected

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,21 @@
88
| test.cpp:62:5:62:13 | ... > ... | Unsigned subtraction can never be negative. |
99
| test.cpp:69:5:69:13 | ... > ... | Unsigned subtraction can never be negative. |
1010
| test.cpp:75:8:75:16 | ... > ... | Unsigned subtraction can never be negative. |
11+
| test.cpp:83:6:83:14 | ... > ... | Unsigned subtraction can never be negative. |
12+
| test.cpp:92:6:92:14 | ... > ... | Unsigned subtraction can never be negative. |
13+
| test.cpp:101:6:101:14 | ... > ... | Unsigned subtraction can never be negative. |
14+
| test.cpp:110:6:110:14 | ... > ... | Unsigned subtraction can never be negative. |
15+
| test.cpp:119:6:119:14 | ... > ... | Unsigned subtraction can never be negative. |
16+
| test.cpp:128:6:128:14 | ... > ... | Unsigned subtraction can never be negative. |
17+
| test.cpp:137:6:137:14 | ... > ... | Unsigned subtraction can never be negative. |
18+
| test.cpp:146:7:146:15 | ... > ... | Unsigned subtraction can never be negative. |
19+
| test.cpp:152:7:152:15 | ... > ... | Unsigned subtraction can never be negative. |
20+
| test.cpp:156:7:156:15 | ... > ... | Unsigned subtraction can never be negative. |
21+
| test.cpp:169:6:169:14 | ... > ... | Unsigned subtraction can never be negative. |
22+
| test.cpp:182:6:182:14 | ... > ... | Unsigned subtraction can never be negative. |
23+
| test.cpp:195:6:195:14 | ... > ... | Unsigned subtraction can never be negative. |
24+
| test.cpp:208:6:208:14 | ... > ... | Unsigned subtraction can never be negative. |
25+
| test.cpp:219:7:219:15 | ... > ... | Unsigned subtraction can never be negative. |
26+
| test.cpp:226:8:226:16 | ... > ... | Unsigned subtraction can never be negative. |
27+
| test.cpp:252:10:252:18 | ... > ... | Unsigned subtraction can never be negative. |
28+
| test.cpp:266:10:266:24 | ... > ... | Unsigned subtraction can never be negative. |

cpp/ql/test/query-tests/Security/CWE/CWE-191/UnsignedDifferenceExpressionComparedZero/test.cpp

Lines changed: 191 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,4 +74,194 @@ void test(unsigned x, unsigned y, bool unknown) {
7474
y += n; // NOTE: `n` is at most `x - y` at this point.
7575
if (x - y > 0) {} // GOOD [FALSE POSITIVE]
7676
}
77-
}
77+
}
78+
79+
void test2() {
80+
unsigned int a = getAnInt();
81+
unsigned int b = a;
82+
83+
if (a - b > 0) { // GOOD (as a = b) [FALSE POSITIVE]
84+
// ...
85+
}
86+
}
87+
88+
void test3() {
89+
unsigned int a = getAnInt();
90+
unsigned int b = a - 1;
91+
92+
if (a - b > 0) { // GOOD (as a >= b) [FALSE POSITIVE]
93+
// ...
94+
}
95+
}
96+
97+
void test4() {
98+
unsigned int a = getAnInt();
99+
unsigned int b = a + 1;
100+
101+
if (a - b > 0) { // BAD
102+
// ...
103+
}
104+
}
105+
106+
void test5() {
107+
unsigned int b = getAnInt();
108+
unsigned int a = b;
109+
110+
if (a - b > 0) { // GOOD (as a = b) [FALSE POSITIVE]
111+
// ...
112+
}
113+
}
114+
115+
void test6() {
116+
unsigned int b = getAnInt();
117+
unsigned int a = b + 1;
118+
119+
if (a - b > 0) { // GOOD (as a >= b) [FALSE POSITIVE]
120+
// ...
121+
}
122+
}
123+
124+
void test7() {
125+
unsigned int b = getAnInt();
126+
unsigned int a = b - 1;
127+
128+
if (a - b > 0) { // BAD
129+
// ...
130+
}
131+
}
132+
133+
void test8() {
134+
unsigned int a = getAnInt();
135+
unsigned int b = getAnInt();
136+
137+
if (a - b > 0) { // BAD
138+
// ...
139+
}
140+
141+
if (a >= b) { // GOOD
142+
if (a - b > 0) { // GOOD (as a >= b)
143+
// ...
144+
}
145+
} else {
146+
if (a - b > 0) { // BAD
147+
// ...
148+
}
149+
}
150+
151+
if (b >= a) { // GOOD
152+
if (a - b > 0) { // BAD
153+
// ...
154+
}
155+
} else {
156+
if (a - b > 0) { // GOOD (as a > b) [FALSE POSITIVE]
157+
// ...
158+
}
159+
}
160+
161+
while (a >= b) { // GOOD
162+
if (a - b > 0) { // GOOD (as a >= b)
163+
// ...
164+
}
165+
}
166+
167+
if (a < b) return;
168+
169+
if (a - b > 0) { // GOOD (as a >= b) [FALSE POSITIVE]
170+
// ...
171+
}
172+
}
173+
174+
void test9() {
175+
unsigned int a = getAnInt();
176+
unsigned int b = getAnInt();
177+
178+
if (a < b) {
179+
b = 0;
180+
}
181+
182+
if (a - b > 0) { // GOOD (as a >= b) [FALSE POSITIVE]
183+
// ...
184+
}
185+
}
186+
187+
void test10() {
188+
unsigned int a = getAnInt();
189+
unsigned int b = getAnInt();
190+
191+
if (a < b) {
192+
a = b;
193+
}
194+
195+
if (a - b > 0) { // GOOD (as a >= b) [FALSE POSITIVE]
196+
// ...
197+
}
198+
}
199+
200+
void test11() {
201+
unsigned int a = getAnInt();
202+
unsigned int b = getAnInt();
203+
204+
if (a < b) return;
205+
206+
b = getAnInt();
207+
208+
if (a - b > 0) { // BAD
209+
// ...
210+
}
211+
}
212+
213+
void test12() {
214+
unsigned int a = getAnInt();
215+
unsigned int b = getAnInt();
216+
unsigned int c;
217+
218+
if ((b <= c) && (c <= a)) {
219+
if (a - b > 0) { // GOOD (as b <= a) [FALSE POSITIVE]
220+
// ...
221+
}
222+
}
223+
224+
if (b <= c) {
225+
if (c <= a) {
226+
if (a - b > 0) { // GOOD (as b <= a) [FALSE POSITIVE]
227+
// ...
228+
}
229+
}
230+
}
231+
}
232+
233+
int test13() {
234+
unsigned int a = getAnInt();
235+
unsigned int b = getAnInt();
236+
237+
if (b != 0) {
238+
return 0;
239+
}
240+
241+
return (a - b > 0); // GOOD (as b = 0)
242+
}
243+
244+
int test14() {
245+
unsigned int a = getAnInt();
246+
unsigned int b = getAnInt();
247+
248+
if (!b) {
249+
return 0;
250+
}
251+
252+
return (a - b > 0); // GOOD (as b = 0) [FALSE POSITIVE]
253+
}
254+
255+
struct Numbers
256+
{
257+
unsigned int a, b;
258+
};
259+
260+
int test15(Numbers *n) {
261+
262+
if (!n) {
263+
return 0;
264+
}
265+
266+
return (n->a - n->b > 0); // BAD
267+
}

0 commit comments

Comments
 (0)