Skip to content

Commit db2479c

Browse files
committed
add more tests
1 parent fc52992 commit db2479c

File tree

2 files changed

+113
-23
lines changed

2 files changed

+113
-23
lines changed
Lines changed: 28 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,28 @@
1-
| test.cpp:72:17:72:21 | (int)... | Implicit cast from unsigned long to int (truncation) |
2-
| test.cpp:73:20:73:24 | (int)... | Implicit cast from unsigned long to int (truncation) |
3-
| test.cpp:74:23:74:27 | (int)... | Implicit cast from unsigned long to int (truncation) |
4-
| test.cpp:75:28:75:32 | (int)... | Implicit cast from unsigned long to int (truncation) |
5-
| test.cpp:80:36:80:40 | (int)... | Implicit cast from unsigned long to int (truncation) |
6-
| test.cpp:88:17:88:21 | (int)... | Implicit cast from unsigned long to int (truncation) |
7-
| test.cpp:94:17:94:21 | (unsigned int)... | Implicit cast from unsigned long to unsigned int (truncation) |
8-
| test.cpp:100:17:100:21 | (unsigned int)... | Implicit cast from unsigned long to unsigned int (truncation) |
9-
| test.cpp:106:17:106:21 | (int)... | Implicit cast from long to int (truncation) |
10-
| test.cpp:114:17:114:17 | (int)... | Implicit cast from unsigned long to int (truncation) |
11-
| test.cpp:121:17:121:21 | (int)... | Implicit cast from unsigned int to int (reinterpretation) |
12-
| test.cpp:121:28:121:28 | (int)... | Implicit cast from unsigned int to int (reinterpretation) |
13-
| test.cpp:127:17:127:21 | (unsigned short)... | Implicit cast from unsigned long to unsigned short (truncation) |
14-
| test.cpp:133:17:133:21 | (int)... | Implicit cast from unsigned long to int (truncation) |
15-
| test.cpp:140:17:140:18 | (unsigned short)... | Implicit cast from unsigned long to unsigned short (truncation) |
16-
| test.cpp:146:17:146:21 | (int)... | Implicit cast from unsigned int to int (reinterpretation) |
17-
| test.cpp:151:17:151:26 | (int)... | Implicit cast from unsigned int to int (reinterpretation) |
18-
| test.cpp:157:17:157:21 | (unsigned int)... | Implicit cast from long to unsigned int (truncation) |
19-
| test.cpp:163:17:163:50 | (uint16_t)... | Implicit cast from int to unsigned short (truncation) |
20-
| test.cpp:170:24:170:24 | (unsigned int)... | Implicit cast from short to unsigned int (widening) |
21-
| test.cpp:176:18:176:20 | (int)... | Implicit cast from unsigned short to int (promotion with bitwise complement) |
22-
| test.cpp:277:12:277:47 | (unsigned int)... | Implicit cast from int to unsigned int (reinterpretation) |
23-
| test.cpp:308:35:308:37 | (int)... | Implicit cast from unsigned short to int (promotion with bitwise complement) |
1+
| test.cpp:73:17:73:21 | (int)... | Implicit cast from unsigned long to int (truncation) |
2+
| test.cpp:74:20:74:24 | (int)... | Implicit cast from unsigned long to int (truncation) |
3+
| test.cpp:75:23:75:27 | (int)... | Implicit cast from unsigned long to int (truncation) |
4+
| test.cpp:76:28:76:32 | (int)... | Implicit cast from unsigned long to int (truncation) |
5+
| test.cpp:81:36:81:40 | (int)... | Implicit cast from unsigned long to int (truncation) |
6+
| test.cpp:89:17:89:21 | (int)... | Implicit cast from unsigned long to int (truncation) |
7+
| test.cpp:95:17:95:21 | (unsigned int)... | Implicit cast from unsigned long to unsigned int (truncation) |
8+
| test.cpp:101:17:101:21 | (unsigned int)... | Implicit cast from unsigned long to unsigned int (truncation) |
9+
| test.cpp:107:17:107:21 | (int)... | Implicit cast from long to int (truncation) |
10+
| test.cpp:115:17:115:17 | (int)... | Implicit cast from unsigned long to int (truncation) |
11+
| test.cpp:122:17:122:21 | (int)... | Implicit cast from unsigned int to int (reinterpretation) |
12+
| test.cpp:122:28:122:28 | (int)... | Implicit cast from unsigned int to int (reinterpretation) |
13+
| test.cpp:128:17:128:21 | (unsigned short)... | Implicit cast from unsigned long to unsigned short (truncation) |
14+
| test.cpp:134:17:134:21 | (int)... | Implicit cast from unsigned long to int (truncation) |
15+
| test.cpp:141:17:141:18 | (unsigned short)... | Implicit cast from unsigned long to unsigned short (truncation) |
16+
| test.cpp:147:17:147:21 | (int)... | Implicit cast from unsigned int to int (reinterpretation) |
17+
| test.cpp:152:17:152:26 | (int)... | Implicit cast from unsigned int to int (reinterpretation) |
18+
| test.cpp:158:17:158:21 | (unsigned int)... | Implicit cast from long to unsigned int (truncation) |
19+
| test.cpp:164:17:164:50 | (uint16_t)... | Implicit cast from int to unsigned short (truncation) |
20+
| test.cpp:171:24:171:24 | (unsigned int)... | Implicit cast from short to unsigned int (widening) |
21+
| test.cpp:177:18:177:20 | (int)... | Implicit cast from unsigned short to int (promotion with bitwise complement) |
22+
| test.cpp:183:9:183:9 | (unsigned long)... | Implicit cast from int to unsigned long (widening) |
23+
| test.cpp:189:9:189:9 | (unsigned long long)... | Implicit cast from long long to unsigned long long (reinterpretation) |
24+
| test.cpp:195:9:195:9 | (unsigned int)... | Implicit cast from int to unsigned int (reinterpretation) |
25+
| test.cpp:204:14:204:14 | (unsigned int)... | Implicit cast from int to unsigned int (reinterpretation) |
26+
| test.cpp:213:9:213:9 | (unsigned long long)... | Implicit cast from int to unsigned long long (widening) |
27+
| test.cpp:316:12:316:47 | (unsigned int)... | Implicit cast from int to unsigned int (reinterpretation) |
28+
| test.cpp:347:35:347:37 | (int)... | Implicit cast from unsigned short to int (promotion with bitwise complement) |

cpp/test/query-tests/security/UnsafeImplicitConversions/test.cpp

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
void *malloc(size_t);
1111
void *memset(void*, int, size_t);
1212
void free(void*);
13+
void puts(char*);
1314
#else
1415
#include <stdio.h>
1516
#include <stdlib.h>
@@ -176,6 +177,44 @@ void test17() {
176177
int val2 = (~val) >> 3;
177178
}
178179

180+
// Implicit casts in comparisons - widening
181+
void test18() {
182+
int x = -7;
183+
if (x > sizeof(int)) { puts("That's why."); }
184+
}
185+
186+
// Implicit casts in comparisons - reinterpretation
187+
void test19() {
188+
long long x = -7;
189+
if (x > sizeof(int)) { puts("That's why."); }
190+
}
191+
192+
// Implicit cast in comparison, int -> unsigned int
193+
void test20(int a) {
194+
const unsigned int b = 0xffff3502;
195+
if (a != b) { // negative a may wrap to b
196+
puts("here");
197+
return;
198+
}
199+
}
200+
201+
// Implicit cast in comparison, int -> unsigned int
202+
void test21(unsigned int a) {
203+
int b = -2;
204+
if (a != b) { // b may wrap to a
205+
puts("here");
206+
return;
207+
}
208+
}
209+
210+
// Implicit cast in comparison, int -> unsigned long long
211+
void test22(int a) {
212+
unsigned long long b = 0xfffffffff0000000;
213+
if (a != b) { // negative a may wrap to b
214+
puts("here");
215+
return;
216+
}
217+
}
179218

180219
/*
181220
* Tests for False Positives
@@ -326,6 +365,43 @@ void test_fp15() {
326365
unsigned short c = (unsigned int)a & b;
327366
}
328367

368+
void test_fp16(unsigned short a, unsigned short b) {
369+
if ( (a-5) < 0) { // promotion to int, possibly unexpected but we are not reporting such issues
370+
puts("called");
371+
}
372+
373+
b = b - 5;
374+
if (b < 0) { // no unexpected promotion
375+
puts("not called");
376+
}
377+
}
378+
379+
// Safe implicit cast in comparison, int -> unsigned int
380+
void test_fp17(int a) {
381+
unsigned int b = 0xcafe;
382+
if (a != b) { // negative a cannot wrap to c
383+
puts("here");
384+
return;
385+
}
386+
}
387+
388+
// Safe implicit cast in comparison, unsigned int -> long long
389+
void test_fp18(unsigned int a) {
390+
long long b = -13;
391+
if (a != b) { // large a cannot wrap to b
392+
puts("here");
393+
return;
394+
}
395+
}
396+
397+
// Safe implicit cast in comparison, int -> unsigned long long
398+
void test_fp19(int a) {
399+
unsigned long long b = 0xffffff1f80000000;
400+
if (a != b) { // large a cannot wrap to b
401+
puts("here");
402+
return;
403+
}
404+
}
329405

330406

331407
int main(int argc, char **argv) {
@@ -349,6 +425,11 @@ int main(int argc, char **argv) {
349425
test15();
350426
test16();
351427
test17();
428+
test18();
429+
test19();
430+
test20((int)argc);
431+
test21((unsigned int)argc);
432+
test22((int)argc);
352433

353434
test_fp1(large);
354435
test_fp2();
@@ -368,6 +449,10 @@ int main(int argc, char **argv) {
368449
test_fp13();
369450
test_fp14();
370451
test_fp15();
452+
test_fp16((unsigned short)argc, (unsigned short)argv[0]);
453+
test_fp17((int)argc);
454+
test_fp18((unsigned int)argc);
455+
test_fp19((int)argc);
371456

372457
return 0;
373458
}

0 commit comments

Comments
 (0)