Skip to content

Commit 49807c0

Browse files
committed
C++: Understand *=.
1 parent 436b18a commit 49807c0

File tree

3 files changed

+18
-2
lines changed

3 files changed

+18
-2
lines changed

cpp/ql/lib/semmle/code/cpp/security/Overflow.qll

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,10 @@ predicate missingGuardAgainstOverflow(Operation e, VariableAccess use) {
120120
// overflow possible if large or small
121121
e instanceof MulExpr and
122122
not (guardedLesser(e, varUse(v)) and guardedGreater(e, varUse(v)))
123+
or
124+
// overflow possible if large or small
125+
e instanceof AssignMulExpr and
126+
not (guardedLesser(e, varUse(v)) and guardedGreater(e, varUse(v)))
123127
)
124128
}
125129

@@ -147,5 +151,9 @@ predicate missingGuardAgainstUnderflow(Operation e, VariableAccess use) {
147151
// underflow possible if large or small
148152
e instanceof MulExpr and
149153
not (guardedLesser(e, varUse(v)) and guardedGreater(e, varUse(v)))
154+
or
155+
// underflow possible if large or small
156+
e instanceof AssignMulExpr and
157+
not (guardedLesser(e, varUse(v)) and guardedGreater(e, varUse(v)))
150158
)
151159
}

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ edges
88
| test.c:81:23:81:26 | call to rand | test.c:83:9:83:9 | r |
99
| test.c:99:14:99:19 | call to rand | test.c:100:5:100:5 | r |
1010
| test.c:125:13:125:16 | call to rand | test.c:127:9:127:9 | r |
11+
| test.c:131:13:131:16 | call to rand | test.c:133:5:133:5 | r |
12+
| test.c:137:13:137:16 | call to rand | test.c:139:10:139:10 | r |
1113
| test.c:155:22:155:25 | call to rand | test.c:157:9:157:9 | r |
1214
| test.c:155:22:155:27 | (unsigned int)... | test.c:157:9:157:9 | r |
1315
| test.cpp:8:9:8:12 | Store | test.cpp:24:11:24:18 | call to get_rand |
@@ -55,6 +57,10 @@ nodes
5557
| test.c:100:5:100:5 | r | semmle.label | r |
5658
| test.c:125:13:125:16 | call to rand | semmle.label | call to rand |
5759
| test.c:127:9:127:9 | r | semmle.label | r |
60+
| test.c:131:13:131:16 | call to rand | semmle.label | call to rand |
61+
| test.c:133:5:133:5 | r | semmle.label | r |
62+
| test.c:137:13:137:16 | call to rand | semmle.label | call to rand |
63+
| test.c:139:10:139:10 | r | semmle.label | r |
5864
| test.c:155:22:155:25 | call to rand | semmle.label | call to rand |
5965
| test.c:155:22:155:27 | (unsigned int)... | semmle.label | (unsigned int)... |
6066
| test.c:157:9:157:9 | r | semmle.label | r |
@@ -109,6 +115,8 @@ nodes
109115
| 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 overflow. | test.c:81:23:81:26 | call to rand | Uncontrolled value |
110116
| 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 overflow. | test.c:99:14:99:19 | call to rand | Uncontrolled value |
111117
| 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 |
118+
| test.c:133:5:133:5 | r | test.c:131:13:131:16 | call to rand | test.c:133:5:133:5 | r | $@ flows to here and is used in arithmetic, potentially causing an overflow. | test.c:131:13:131:16 | call to rand | Uncontrolled value |
119+
| test.c:139:10:139:10 | r | test.c:137:13:137:16 | call to rand | test.c:139:10:139:10 | r | $@ flows to here and is used in arithmetic, potentially causing an overflow. | test.c:137:13:137:16 | call to rand | Uncontrolled value |
112120
| test.c:157:9:157:9 | r | test.c:155:22:155:25 | call to rand | test.c:157:9:157:9 | r | $@ flows to here and is used in arithmetic, potentially causing an underflow. | test.c:155:22:155:25 | call to rand | Uncontrolled value |
113121
| test.c:157:9:157:9 | r | test.c:155:22:155:27 | (unsigned int)... | test.c:157:9:157:9 | r | $@ flows to here and is used in arithmetic, potentially causing an underflow. | test.c:155:22:155:25 | call to rand | Uncontrolled value |
114122
| 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 |

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -130,13 +130,13 @@ void moreTests() {
130130
{
131131
int r = rand();
132132

133-
r *= 100; // BAD [NOT DETECTED]
133+
r *= 100; // BAD
134134
}
135135

136136
{
137137
int r = rand();
138138
int v = 100;
139-
v *= r; // BAD [NOT DETECTED]
139+
v *= r; // BAD
140140
}
141141

142142
{

0 commit comments

Comments
 (0)