Skip to content

Commit 00f6f66

Browse files
committed
C++: Don't report underflowing multiplication.
1 parent 40f0658 commit 00f6f66

File tree

3 files changed

+8
-20
lines changed

3 files changed

+8
-20
lines changed

cpp/ql/src/Security/CWE/CWE-190/ArithmeticUncontrolled.ql

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,10 @@ private class RandS extends RandomFunction {
7474

7575
predicate missingGuard(VariableAccess va, string effect) {
7676
exists(Operation op | op.getAnOperand() = va |
77-
missingGuardAgainstUnderflow(op, va) and effect = "underflow"
77+
(
78+
missingGuardAgainstUnderflow(op, va) and effect = "underflow" and
79+
not op instanceof MulExpr // random numbers are usually non-negative, so multiplication doesn't underflow.
80+
)
7881
or
7982
missingGuardAgainstOverflow(op, va) and effect = "overflow"
8083
)

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

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,7 @@ edges
2121
| test.cpp:36:13:36:13 | get_rand3 output argument [[]] | test.cpp:36:13:36:13 | Chi |
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 |
24-
| test.cpp:78:10:78:13 | call to rand | test.cpp:84:10:84:10 | x |
2524
| test.cpp:90:10:90:13 | call to rand | test.cpp:94:10:94:10 | x |
26-
| test.cpp:90:10:90:13 | call to rand | test.cpp:97:9:97:9 | x |
27-
| test.cpp:102:10:102:13 | call to rand | test.cpp:108:10:108:10 | y |
28-
| test.cpp:116:10:116:13 | call to rand | test.cpp:124:9:124:9 | y |
2925
nodes
3026
| test.c:18:13:18:16 | call to rand | semmle.label | call to rand |
3127
| test.c:21:17:21:17 | r | semmle.label | r |
@@ -61,14 +57,8 @@ nodes
6157
| test.cpp:57:9:57:9 | x | semmle.label | x |
6258
| test.cpp:78:10:78:13 | call to rand | semmle.label | call to rand |
6359
| test.cpp:82:10:82:10 | x | semmle.label | x |
64-
| test.cpp:84:10:84:10 | x | semmle.label | x |
6560
| test.cpp:90:10:90:13 | call to rand | semmle.label | call to rand |
6661
| test.cpp:94:10:94:10 | x | semmle.label | x |
67-
| test.cpp:97:9:97:9 | x | semmle.label | x |
68-
| test.cpp:102:10:102:13 | call to rand | semmle.label | call to rand |
69-
| test.cpp:108:10:108:10 | y | semmle.label | y |
70-
| test.cpp:116:10:116:13 | call to rand | semmle.label | call to rand |
71-
| test.cpp:124:9:124:9 | y | semmle.label | y |
7262
#select
7363
| 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 |
7464
| 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 |
@@ -79,14 +69,9 @@ nodes
7969
| test.c:83:9:83:9 | r | test.c:81:23:81:26 | call to rand | test.c:83:9:83:9 | r | $@ flows to here and is used in arithmetic, potentially causing an underflow. | test.c:81:23:81:26 | call to rand | Uncontrolled value |
8070
| test.c:100:5:100:5 | r | test.c:99:14:99:19 | call to rand | test.c:100:5:100:5 | r | $@ flows to here and is used in arithmetic, potentially causing an underflow. | test.c:99:14:99:19 | call to rand | Uncontrolled value |
8171
| test.c:127:9:127:9 | r | test.c:125:13:125:16 | call to rand | test.c:127:9:127:9 | r | $@ flows to here and is used in arithmetic, potentially causing an overflow. | test.c:125:13:125:16 | call to rand | Uncontrolled value |
82-
| test.c:127:9:127:9 | r | test.c:125:13:125:16 | call to rand | test.c:127:9:127:9 | r | $@ flows to here and is used in arithmetic, potentially causing an underflow. | test.c:125:13:125:16 | call to rand | Uncontrolled value |
8372
| test.cpp:25:7:25:7 | r | test.cpp:8:9:8:12 | call to rand | test.cpp:25:7:25:7 | r | $@ flows to here and is used in arithmetic, potentially causing an overflow. | test.cpp:8:9:8:12 | call to rand | Uncontrolled value |
8473
| test.cpp:31:7:31:7 | r | test.cpp:13:10:13:13 | call to rand | test.cpp:31:7:31:7 | r | $@ flows to here and is used in arithmetic, potentially causing an overflow. | test.cpp:13:10:13:13 | call to rand | Uncontrolled value |
8574
| test.cpp:37:7:37:7 | r | test.cpp:18:9:18:12 | call to rand | test.cpp:37:7:37:7 | r | $@ flows to here and is used in arithmetic, potentially causing an overflow. | test.cpp:18:9:18:12 | call to rand | Uncontrolled value |
8675
| 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 |
8776
| 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 |
88-
| test.cpp:84:10:84:10 | x | test.cpp:78:10:78:13 | call to rand | test.cpp:84:10:84:10 | x | $@ flows to here and is used in arithmetic, potentially causing an underflow. | test.cpp:78:10:78:13 | call to rand | Uncontrolled value |
8977
| 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 |
90-
| test.cpp:97:9:97:9 | x | test.cpp:90:10:90:13 | call to rand | test.cpp:97:9:97:9 | x | $@ flows to here and is used in arithmetic, potentially causing an underflow. | test.cpp:90:10:90:13 | call to rand | Uncontrolled value |
91-
| test.cpp:108:10:108:10 | y | test.cpp:102:10:102:13 | call to rand | test.cpp:108:10:108:10 | y | $@ flows to here and is used in arithmetic, potentially causing an underflow. | test.cpp:102:10:102:13 | call to rand | Uncontrolled value |
92-
| test.cpp:124:9:124:9 | y | test.cpp:116:10:116:13 | call to rand | test.cpp:124:9:124:9 | y | $@ flows to here and is used in arithmetic, potentially causing an underflow. | test.cpp:116:10:116:13 | call to rand | Uncontrolled value |

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ int test_else_1()
8181
{
8282
return x * 10; // BAD
8383
} else {
84-
return x * 10; // GOOD (as x <= 100) [FALSE POSITIVE]
84+
return x * 10; // GOOD (as x <= 100)
8585
}
8686
}
8787

@@ -94,7 +94,7 @@ int test_else_2()
9494
return x * 10; // BAD
9595
}
9696

97-
return x * 10; // GOOD (as x <= 100) [FALSE POSITIVE]
97+
return x * 10; // GOOD (as x <= 100)
9898
}
9999

100100
int test_conditional_assignment_1()
@@ -105,7 +105,7 @@ int test_conditional_assignment_1()
105105
if (x < y)
106106
{
107107
y = x;
108-
return y * 10; // GOOD (as y <= 100) [FALSE POSITIVE]
108+
return y * 10; // GOOD (as y <= 100)
109109
} else {
110110
return y * 10; // GOOD (as y = 100)
111111
}
@@ -121,5 +121,5 @@ int test_conditional_assignment_2()
121121
y = x;
122122
}
123123

124-
return y * 10; // GOOD (as y <= 100) [FALSE POSITIVE]
124+
return y * 10; // GOOD (as y <= 100)
125125
}

0 commit comments

Comments
 (0)