Skip to content

Commit 2acbdec

Browse files
committed
C++: Add test cases.
1 parent b8acd70 commit 2acbdec

File tree

2 files changed

+53
-1
lines changed

2 files changed

+53
-1
lines changed

cpp/ql/test/query-tests/Security/CWE/CWE-190/semmle/ComparisonWithWiderType/ComparisonWithWiderType.expected

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,17 @@
22
| test.c:9:14:9:18 | ... > ... | Comparison between $@ of type char and $@ of wider type int. | test.c:8:7:8:7 | c | c | test.c:7:17:7:17 | x | x |
33
| test.c:14:14:14:18 | ... < ... | Comparison between $@ of type short and $@ of wider type int. | test.c:13:8:13:8 | s | s | test.c:12:17:12:17 | x | x |
44
| test.c:65:14:65:18 | ... < ... | Comparison between $@ of type short and $@ of wider type int. | test.c:64:8:64:8 | s | s | test.c:63:17:63:17 | x | x |
5+
| test.c:87:14:87:18 | ... < ... | Comparison between $@ of type unsigned char and $@ of wider type unsigned int. | test.c:83:16:83:16 | c | c | test.c:84:15:84:15 | x | x |
6+
| test.c:91:14:91:23 | ... < ... | Comparison between $@ of type unsigned char and $@ of wider type int. | test.c:83:16:83:16 | c | c | test.c:91:18:91:23 | 65280 | 65280 |
7+
| test.c:93:14:93:25 | ... < ... | Comparison between $@ of type unsigned char and $@ of wider type int. | test.c:83:16:83:16 | c | c | test.c:93:18:93:25 | 16711680 | 16711680 |
8+
| test.c:95:14:95:27 | ... < ... | Comparison between $@ of type unsigned char and $@ of wider type unsigned int. | test.c:83:16:83:16 | c | c | test.c:95:18:95:27 | 4278190080 | 4278190080 |
9+
| test.c:97:14:97:27 | ... < ... | Comparison between $@ of type unsigned char and $@ of wider type unsigned int. | test.c:83:16:83:16 | c | c | test.c:97:19:97:26 | ... & ... | ... & ... |
10+
| test.c:99:14:99:29 | ... < ... | Comparison between $@ of type unsigned char and $@ of wider type unsigned int. | test.c:83:16:83:16 | c | c | test.c:99:19:99:28 | ... & ... | ... & ... |
11+
| test.c:101:14:101:31 | ... < ... | Comparison between $@ of type unsigned char and $@ of wider type unsigned int. | test.c:83:16:83:16 | c | c | test.c:101:19:101:30 | ... & ... | ... & ... |
12+
| test.c:103:14:103:33 | ... < ... | Comparison between $@ of type unsigned char and $@ of wider type unsigned int. | test.c:83:16:83:16 | c | c | test.c:103:19:103:32 | ... & ... | ... & ... |
13+
| test.c:105:14:105:25 | ... < ... | Comparison between $@ of type unsigned char and $@ of wider type unsigned int. | test.c:83:16:83:16 | c | c | test.c:105:19:105:24 | ... >> ... | ... >> ... |
14+
| test.c:107:14:107:26 | ... < ... | Comparison between $@ of type unsigned char and $@ of wider type unsigned int. | test.c:83:16:83:16 | c | c | test.c:107:19:107:25 | ... >> ... | ... >> ... |
15+
| test.c:109:14:109:26 | ... < ... | Comparison between $@ of type unsigned char and $@ of wider type unsigned int. | test.c:83:16:83:16 | c | c | test.c:109:19:109:25 | ... >> ... | ... >> ... |
16+
| test.c:111:14:111:36 | ... < ... | Comparison between $@ of type unsigned char and $@ of wider type unsigned int. | test.c:83:16:83:16 | c | c | test.c:111:19:111:35 | ... >> ... | ... >> ... |
17+
| test.c:113:14:113:39 | ... < ... | Comparison between $@ of type unsigned char and $@ of wider type unsigned int. | test.c:83:16:83:16 | c | c | test.c:113:19:113:38 | ... >> ... | ... >> ... |
18+
| test.c:115:14:115:41 | ... < ... | Comparison between $@ of type unsigned char and $@ of wider type unsigned int. | test.c:83:16:83:16 | c | c | test.c:115:19:115:40 | ... >> ... | ... >> ... |

cpp/ql/test/query-tests/Security/CWE/CWE-190/semmle/ComparisonWithWiderType/test.c

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,4 +75,42 @@ extern const int const256;
7575
void test11() {
7676
short s;
7777
for(s = 0; s < const256; ++s) {}
78-
}
78+
}
79+
80+
unsigned int get_a_uint();
81+
82+
void test12() {
83+
unsigned char c;
84+
unsigned int x;
85+
86+
x = get_a_uint();
87+
for (c = 0; c < x; c++) {} // BAD
88+
x = get_a_uint();
89+
for (c = 0; c < 0xFF; c++) {} // GOOD
90+
x = get_a_uint();
91+
for (c = 0; c < 0xFF00; c++) {} // BAD
92+
x = get_a_uint();
93+
for (c = 0; c < 0xFF0000; c++) {} // BAD
94+
x = get_a_uint();
95+
for (c = 0; c < 0xFF000000; c++) {} // BAD
96+
x = get_a_uint();
97+
for (c = 0; c < (x & 0xFF); c++) {} // GOOD [FALSE POSITIVE]
98+
x = get_a_uint();
99+
for (c = 0; c < (x & 0xFF00); c++) {} // BAD
100+
x = get_a_uint();
101+
for (c = 0; c < (x & 0xFF0000); c++) {} // BAD
102+
x = get_a_uint();
103+
for (c = 0; c < (x & 0xFF000000); c++) {} // BAD
104+
x = get_a_uint();
105+
for (c = 0; c < (x >> 8); c++) {} // BAD
106+
x = get_a_uint();
107+
for (c = 0; c < (x >> 16); c++) {} // BAD
108+
x = get_a_uint();
109+
for (c = 0; c < (x >> 24); c++) {} // GOOD (assuming 32-bit ints) [FALSE POSITIVE]
110+
x = get_a_uint();
111+
for (c = 0; c < ((x & 0xFF00) >> 8); c++) {} // GOOD [FALSE POSITIVE]
112+
x = get_a_uint();
113+
for (c = 0; c < ((x & 0xFF0000) >> 16); c++) {} // GOOD [FALSE POSITIVE]
114+
x = get_a_uint();
115+
for (c = 0; c < ((x & 0xFF000000) >> 24); c++) {} // GOOD [FALSE POSITIVE]
116+
}

0 commit comments

Comments
 (0)