Skip to content

Commit 9e04112

Browse files
committed
C++: Add some more test cases.
1 parent 5ac6b38 commit 9e04112

File tree

2 files changed

+49
-0
lines changed

2 files changed

+49
-0
lines changed

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

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,9 @@ edges
2222
| test.cpp:54:10:54:13 | call to rand | test.cpp:57:9:57:9 | x |
2323
| test.cpp:78:10:78:13 | call to rand | test.cpp:82:10:82:10 | x |
2424
| test.cpp:90:10:90:13 | call to rand | test.cpp:94:10:94:10 | x |
25+
| test.cpp:129:10:129:13 | call to rand | test.cpp:132:10:132:10 | b |
26+
| test.cpp:147:11:147:14 | call to rand | test.cpp:149:11:149:16 | (int)... |
27+
| test.cpp:147:11:147:14 | call to rand | test.cpp:149:16:149:16 | y |
2528
nodes
2629
| test.c:18:13:18:16 | call to rand | semmle.label | call to rand |
2730
| test.c:21:17:21:17 | r | semmle.label | r |
@@ -59,6 +62,11 @@ nodes
5962
| test.cpp:82:10:82:10 | x | semmle.label | x |
6063
| test.cpp:90:10:90:13 | call to rand | semmle.label | call to rand |
6164
| test.cpp:94:10:94:10 | x | semmle.label | x |
65+
| test.cpp:129:10:129:13 | call to rand | semmle.label | call to rand |
66+
| test.cpp:132:10:132:10 | b | semmle.label | b |
67+
| test.cpp:147:11:147:14 | call to rand | semmle.label | call to rand |
68+
| test.cpp:149:11:149:16 | (int)... | semmle.label | (int)... |
69+
| test.cpp:149:16:149:16 | y | semmle.label | y |
6270
#select
6371
| test.c:21:17:21:17 | r | test.c:18:13:18:16 | call to rand | test.c:21:17:21:17 | r | $@ flows to here and is used in arithmetic, potentially causing an overflow. | test.c:18:13:18:16 | call to rand | Uncontrolled value |
6472
| test.c:35:5:35:5 | r | test.c:34:13:34:18 | call to rand | test.c:35:5:35:5 | r | $@ flows to here and is used in arithmetic, potentially causing an overflow. | test.c:34:13:34:18 | call to rand | Uncontrolled value |
@@ -75,3 +83,6 @@ nodes
7583
| test.cpp:57:9:57:9 | x | test.cpp:54:10:54:13 | call to rand | test.cpp:57:9:57:9 | x | $@ flows to here and is used in arithmetic, potentially causing an underflow. | test.cpp:54:10:54:13 | call to rand | Uncontrolled value |
7684
| test.cpp:82:10:82:10 | x | test.cpp:78:10:78:13 | call to rand | test.cpp:82:10:82:10 | x | $@ flows to here and is used in arithmetic, potentially causing an overflow. | test.cpp:78:10:78:13 | call to rand | Uncontrolled value |
7785
| test.cpp:94:10:94:10 | x | test.cpp:90:10:90:13 | call to rand | test.cpp:94:10:94:10 | x | $@ flows to here and is used in arithmetic, potentially causing an overflow. | test.cpp:90:10:90:13 | call to rand | Uncontrolled value |
86+
| test.cpp:132:10:132:10 | b | test.cpp:129:10:129:13 | call to rand | test.cpp:132:10:132:10 | b | $@ flows to here and is used in arithmetic, potentially causing an overflow. | test.cpp:129:10:129:13 | call to rand | Uncontrolled value |
87+
| test.cpp:149:11:149:16 | (int)... | test.cpp:147:11:147:14 | call to rand | test.cpp:149:11:149:16 | (int)... | $@ flows to here and is used in arithmetic, potentially causing an overflow. | test.cpp:147:11:147:14 | call to rand | Uncontrolled value |
88+
| test.cpp:149:16:149:16 | y | test.cpp:147:11:147:14 | call to rand | test.cpp:149:16:149:16 | y | $@ flows to here and is used in arithmetic, potentially causing an overflow. | test.cpp:147:11:147:14 | call to rand | Uncontrolled value |

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

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,3 +123,41 @@ int test_conditional_assignment_2()
123123

124124
return y * 10; // GOOD (as y <= 100)
125125
}
126+
127+
int test_underflow()
128+
{
129+
int x = rand();
130+
int a = -x; // GOOD
131+
int b = 10 - x; // GOOD
132+
int c = b * 2; // BAD
133+
}
134+
135+
int test_cast()
136+
{
137+
int x = rand();
138+
short a = x; // BAD [NOT DETECTED]
139+
short b = -x; // BAD [NOT DETECTED]
140+
long long c = x; // GOOD
141+
long long d = -x; // GOOD
142+
}
143+
144+
void test_float()
145+
{
146+
{
147+
int x = rand();
148+
float y = x; // GOOD
149+
int z = (int)y * 5; // BAD
150+
}
151+
152+
{
153+
int x = rand();
154+
float y = x * 5.0f; // GOOD
155+
int z = y; // BAD [NOT DETECTED]
156+
}
157+
158+
{
159+
int x = rand();
160+
float y = x / 10.0f; // GOOD
161+
int z = (int)y * 5; // GOOD
162+
}
163+
}

0 commit comments

Comments
 (0)