Skip to content

Commit 3d5b198

Browse files
committed
C++: Fix a bug where 'boundedImpl' could give back multiple deltas.
1 parent c0a54e9 commit 3d5b198

File tree

5 files changed

+12
-17
lines changed

5 files changed

+12
-17
lines changed

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

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ private Instruction getABoundIn(SemBound b, IRFunction func) {
1818
* Holds if `i <= b + delta`.
1919
*/
2020
pragma[inline]
21-
private predicate boundedImpl(Instruction i, Instruction b, int delta) {
21+
private predicate boundedImplCand(Instruction i, Instruction b, int delta) {
2222
exists(SemBound bound, IRFunction func |
2323
semBounded(getSemanticExpr(i), bound, delta, true,
2424
any(SemReason reason | not reason instanceof SemTypeReason)) and
@@ -27,6 +27,15 @@ private predicate boundedImpl(Instruction i, Instruction b, int delta) {
2727
)
2828
}
2929

30+
/**
31+
* Holds if `i <= b + delta` and `delta` is the smallest integer that satisfies
32+
* this condition.
33+
*/
34+
pragma[inline]
35+
private predicate boundedImpl(Instruction i, Instruction b, int delta) {
36+
delta = min(int cand | boundedImplCand(i, b, cand))
37+
}
38+
3039
/**
3140
* Holds if `i <= b + delta`.
3241
*
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,2 @@
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: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,6 @@ 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 | ... = ... |
133132
| test.cpp:355:14:355:27 | new[] | test.cpp:356:15:356:23 | ... + ... |
134133
| test.cpp:355:14:355:27 | new[] | test.cpp:356:15:356:23 | ... + ... |
135134
| test.cpp:355:14:355:27 | new[] | test.cpp:357:24:357:30 | ... + ... |
@@ -223,7 +222,6 @@ edges
223222
| test.cpp:705:18:705:18 | q | test.cpp:706:12:706:13 | * ... |
224223
| test.cpp:711:13:711:26 | new[] | test.cpp:714:11:714:11 | q |
225224
| 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 | ... = ... |
227225
nodes
228226
| test.cpp:4:15:4:20 | call to malloc | semmle.label | call to malloc |
229227
| test.cpp:5:15:5:22 | ... + ... | semmle.label | ... + ... |
@@ -316,8 +314,6 @@ nodes
316314
| test.cpp:271:14:271:21 | ... + ... | semmle.label | ... + ... |
317315
| test.cpp:271:14:271:21 | ... + ... | semmle.label | ... + ... |
318316
| 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 | ... = ... |
321317
| test.cpp:355:14:355:27 | new[] | semmle.label | new[] |
322318
| test.cpp:356:15:356:23 | ... + ... | semmle.label | ... + ... |
323319
| test.cpp:356:15:356:23 | ... + ... | semmle.label | ... + ... |
@@ -376,8 +372,6 @@ nodes
376372
| test.cpp:706:12:706:13 | * ... | semmle.label | * ... |
377373
| test.cpp:711:13:711:26 | new[] | semmle.label | new[] |
378374
| 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 | ... = ... |
381375
subpaths
382376
#select
383377
| 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 |
@@ -399,7 +393,6 @@ subpaths
399393
| 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 |
400394
| 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 |
401395
| 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 | ... + ... | ... + ... |
403396
| 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 |
404397
| 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 |
405398
| 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 |
@@ -413,4 +406,3 @@ subpaths
413406
| 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 |
414407
| 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 |
415408
| 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,4 +1,2 @@
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: 2 additions & 2 deletions
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 [FALSE POSITIVE]
308+
xs[i+1] = test21_get(i+1); // GOOD
309309
}
310310
}
311311

@@ -722,6 +722,6 @@ void test21_simple(bool b) {
722722
int* xs = new int[n];
723723

724724
for (int i = 0; i < n; i += 2) {
725-
xs[i+1] = 0; // GOOD [FALSE POSITIVE]
725+
xs[i+1] = 0; // GOOD
726726
}
727727
}

0 commit comments

Comments
 (0)