Skip to content

Commit c0a54e9

Browse files
committed
C++: Fix an inequality that should be strict, but wasn't.
1 parent 9aae174 commit c0a54e9

File tree

5 files changed

+28
-3
lines changed

5 files changed

+28
-3
lines changed

cpp/ql/lib/semmle/code/cpp/security/InvalidPointerDereference/AllocationToInvalidPointer.qll

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,7 @@ private module SizeBarrier {
167167
pragma[inline_late]
168168
Instruction getABarrierInstruction(int state) {
169169
exists(int delta, int k |
170-
state >= k + delta and
170+
state > k + delta and
171171
// result <= "size of allocation" + delta + k
172172
// <= "size of allocation" + state
173173
result = getABarrierInstruction0(delta, k)
@@ -195,7 +195,7 @@ private module SizeBarrier {
195195
ValidForStateFlow::flow(source, result) and
196196
hasSize(_, source, state) and
197197
ValidForStateConfig::isSink(result, delta, k) and
198-
state >= k + delta
198+
state > k + delta
199199
// so now we have:
200200
// result <= "size of allocation" + delta + k
201201
// <= "size of allocation" + state
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,6 @@
11
failures
22
testFailures
3+
| test.cpp:308:5:308:11 | PointerAdd: access to array | Unexpected result: alloc=L304 |
4+
| test.cpp:308:5:308:11 | PointerAdd: access to array | Unexpected result: alloc=L304-1 |
5+
| test.cpp:725:5:725:11 | PointerAdd: access to array | Unexpected result: alloc=L722 |
6+
| test.cpp:725:5:725:11 | PointerAdd: access to array | Unexpected result: alloc=L722-1 |

cpp/ql/test/experimental/query-tests/Security/CWE/CWE-193/pointer-deref/InvalidPointerDeref.expected

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,7 @@ edges
129129
| test.cpp:271:14:271:21 | ... + ... | test.cpp:271:14:271:21 | ... + ... |
130130
| test.cpp:271:14:271:21 | ... + ... | test.cpp:274:5:274:10 | ... = ... |
131131
| test.cpp:271:14:271:21 | ... + ... | test.cpp:274:5:274:10 | ... = ... |
132+
| test.cpp:304:15:304:26 | new[] | test.cpp:308:5:308:29 | ... = ... |
132133
| test.cpp:355:14:355:27 | new[] | test.cpp:356:15:356:23 | ... + ... |
133134
| test.cpp:355:14:355:27 | new[] | test.cpp:356:15:356:23 | ... + ... |
134135
| test.cpp:355:14:355:27 | new[] | test.cpp:357:24:357:30 | ... + ... |
@@ -222,6 +223,7 @@ edges
222223
| test.cpp:705:18:705:18 | q | test.cpp:706:12:706:13 | * ... |
223224
| test.cpp:711:13:711:26 | new[] | test.cpp:714:11:714:11 | q |
224225
| test.cpp:714:11:714:11 | q | test.cpp:705:18:705:18 | q |
226+
| test.cpp:722:13:722:22 | new[] | test.cpp:725:5:725:15 | ... = ... |
225227
nodes
226228
| test.cpp:4:15:4:20 | call to malloc | semmle.label | call to malloc |
227229
| test.cpp:5:15:5:22 | ... + ... | semmle.label | ... + ... |
@@ -314,6 +316,8 @@ nodes
314316
| test.cpp:271:14:271:21 | ... + ... | semmle.label | ... + ... |
315317
| test.cpp:271:14:271:21 | ... + ... | semmle.label | ... + ... |
316318
| test.cpp:274:5:274:10 | ... = ... | semmle.label | ... = ... |
319+
| test.cpp:304:15:304:26 | new[] | semmle.label | new[] |
320+
| test.cpp:308:5:308:29 | ... = ... | semmle.label | ... = ... |
317321
| test.cpp:355:14:355:27 | new[] | semmle.label | new[] |
318322
| test.cpp:356:15:356:23 | ... + ... | semmle.label | ... + ... |
319323
| test.cpp:356:15:356:23 | ... + ... | semmle.label | ... + ... |
@@ -372,6 +376,8 @@ nodes
372376
| test.cpp:706:12:706:13 | * ... | semmle.label | * ... |
373377
| test.cpp:711:13:711:26 | new[] | semmle.label | new[] |
374378
| test.cpp:714:11:714:11 | q | semmle.label | q |
379+
| test.cpp:722:13:722:22 | new[] | semmle.label | new[] |
380+
| test.cpp:725:5:725:15 | ... = ... | semmle.label | ... = ... |
375381
subpaths
376382
#select
377383
| test.cpp:6:14:6:15 | * ... | test.cpp:4:15:4:20 | call to malloc | test.cpp:6:14:6:15 | * ... | This read might be out of bounds, as the pointer might be equal to $@ + $@. | test.cpp:4:15:4:20 | call to malloc | call to malloc | test.cpp:5:19:5:22 | size | size |
@@ -393,6 +399,7 @@ subpaths
393399
| test.cpp:254:9:254:16 | ... = ... | test.cpp:248:24:248:30 | call to realloc | test.cpp:254:9:254:16 | ... = ... | This write might be out of bounds, as the pointer might be equal to $@ + $@. | test.cpp:248:24:248:30 | call to realloc | call to realloc | test.cpp:254:11:254:11 | i | i |
394400
| test.cpp:264:13:264:14 | * ... | test.cpp:260:13:260:24 | new[] | test.cpp:264:13:264:14 | * ... | This read might be out of bounds, as the pointer might be equal to $@ + $@. | test.cpp:260:13:260:24 | new[] | new[] | test.cpp:261:19:261:21 | len | len |
395401
| test.cpp:274:5:274:10 | ... = ... | test.cpp:270:13:270:24 | new[] | test.cpp:274:5:274:10 | ... = ... | This write might be out of bounds, as the pointer might be equal to $@ + $@. | test.cpp:270:13:270:24 | new[] | new[] | test.cpp:271:19:271:21 | len | len |
402+
| test.cpp:308:5:308:29 | ... = ... | test.cpp:304:15:304:26 | new[] | test.cpp:308:5:308:29 | ... = ... | This write might be out of bounds, as the pointer might be equal to $@ + $@. | test.cpp:304:15:304:26 | new[] | new[] | test.cpp:308:8:308:10 | ... + ... | ... + ... |
396403
| test.cpp:358:14:358:26 | * ... | test.cpp:355:14:355:27 | new[] | test.cpp:358:14:358:26 | * ... | This read might be out of bounds, as the pointer might be equal to $@ + $@ + 1. | test.cpp:355:14:355:27 | new[] | new[] | test.cpp:356:20:356:23 | size | size |
397404
| test.cpp:359:14:359:32 | * ... | test.cpp:355:14:355:27 | new[] | test.cpp:359:14:359:32 | * ... | This read might be out of bounds, as the pointer might be equal to $@ + $@ + 2. | test.cpp:355:14:355:27 | new[] | new[] | test.cpp:356:20:356:23 | size | size |
398405
| test.cpp:384:13:384:16 | * ... | test.cpp:377:14:377:27 | new[] | test.cpp:384:13:384:16 | * ... | This read might be out of bounds, as the pointer might be equal to $@ + $@. | test.cpp:377:14:377:27 | new[] | new[] | test.cpp:378:20:378:23 | size | size |
@@ -406,3 +413,4 @@ subpaths
406413
| test.cpp:647:5:647:19 | ... = ... | test.cpp:642:14:642:31 | new[] | test.cpp:647:5:647:19 | ... = ... | This write might be out of bounds, as the pointer might be equal to $@ + $@. | test.cpp:642:14:642:31 | new[] | new[] | test.cpp:647:8:647:14 | src_pos | src_pos |
407414
| test.cpp:701:15:701:16 | * ... | test.cpp:695:13:695:26 | new[] | test.cpp:701:15:701:16 | * ... | This read might be out of bounds, as the pointer might be equal to $@ + $@. | test.cpp:695:13:695:26 | new[] | new[] | test.cpp:696:19:696:22 | size | size |
408415
| test.cpp:706:12:706:13 | * ... | test.cpp:711:13:711:26 | new[] | test.cpp:706:12:706:13 | * ... | This read might be out of bounds, as the pointer might be equal to $@ + $@. | test.cpp:711:13:711:26 | new[] | new[] | test.cpp:712:19:712:22 | size | size |
416+
| test.cpp:725:5:725:15 | ... = ... | test.cpp:722:13:722:22 | new[] | test.cpp:725:5:725:15 | ... = ... | This write might be out of bounds, as the pointer might be equal to $@ + $@. | test.cpp:722:13:722:22 | new[] | new[] | test.cpp:725:8:725:10 | ... + ... | ... + ... |
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,4 @@
11
failures
22
testFailures
3+
| test.cpp:308:5:308:29 | ... = ... | Unexpected result: deref=L308 |
4+
| test.cpp:725:5:725:15 | ... = ... | Unexpected result: deref=L725 |

cpp/ql/test/experimental/query-tests/Security/CWE/CWE-193/pointer-deref/test.cpp

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -305,7 +305,7 @@ void test21() {
305305

306306
for (int i = 0; i < n; i += 2) {
307307
xs[i] = test21_get(i); // GOOD
308-
xs[i+1] = test21_get(i+1); // GOOD
308+
xs[i+1] = test21_get(i+1); // GOOD [FALSE POSITIVE]
309309
}
310310
}
311311

@@ -714,3 +714,14 @@ void test35(unsigned long size, char* q)
714714
deref(q);
715715
}
716716
}
717+
718+
void test21_simple(bool b) {
719+
int n = 0;
720+
if (b) n = 2;
721+
722+
int* xs = new int[n];
723+
724+
for (int i = 0; i < n; i += 2) {
725+
xs[i+1] = 0; // GOOD [FALSE POSITIVE]
726+
}
727+
}

0 commit comments

Comments
 (0)