Skip to content

Commit 01d3257

Browse files
committed
C++: Add test cases involving casts.
1 parent 24d7446 commit 01d3257

File tree

2 files changed

+34
-0
lines changed

2 files changed

+34
-0
lines changed

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,3 +11,6 @@
1111
| 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 | ... & ... | ... & ... |
1212
| 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 | ... >> ... | ... >> ... |
1313
| 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 | ... >> ... | ... >> ... |
14+
| test.c:128:15:128:21 | ... < ... | Comparison between $@ of type unsigned char and $@ of wider type unsigned int. | test.c:121:16:121:17 | uc | uc | test.c:123:19:123:20 | sz | sz |
15+
| test.c:139:15:139:21 | ... < ... | Comparison between $@ of type unsigned char and $@ of wider type unsigned int. | test.c:121:16:121:17 | uc | uc | test.c:123:19:123:20 | sz | sz |
16+
| test.c:146:15:146:21 | ... < ... | Comparison between $@ of type unsigned char and $@ of wider type unsigned int. | test.c:121:16:121:17 | uc | uc | test.c:123:19:123:20 | sz | sz |

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

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,3 +114,34 @@ void test12() {
114114
x = get_a_uint();
115115
for (c = 0; c < ((x & 0xFF000000) >> 24); c++) {} // GOOD
116116
}
117+
118+
int get_an_int();
119+
120+
void test13() {
121+
unsigned char uc;
122+
int sx, sy;
123+
unsigned ux, uy, sz;
124+
125+
ux = get_a_uint();
126+
uy = get_a_uint();
127+
sz = ux & uy;
128+
for (uc = 0; uc < sz; uc++) {} // BAD
129+
130+
ux = get_a_uint();
131+
uy = get_a_uint();
132+
if (ux > 128) {ux = 128;}
133+
sz = ux & uy;
134+
for (uc = 0; uc < sz; uc++) {} // GOOD
135+
136+
sx = get_an_int();
137+
sy = get_an_int();
138+
sz = (unsigned)sx & (unsigned)sy;
139+
for (uc = 0; uc < sz; uc++) {} // BAD
140+
141+
sx = get_an_int();
142+
sy = get_an_int();
143+
if (sx < 0) {sx = 0;}
144+
if (sx > 128) {sx = 128;}
145+
sz = (unsigned)sx & (unsigned)sy;
146+
for (uc = 0; uc < sz; uc++) {} // GOOD [FALSE POSITIVE]
147+
}

0 commit comments

Comments
 (0)