Skip to content

Commit 3596ff7

Browse files
committed
Address review.
1 parent 1aa7a82 commit 3596ff7

File tree

3 files changed

+36
-14
lines changed

3 files changed

+36
-14
lines changed

cpp/ql/src/experimental/semmle/code/cpp/rangeanalysis/InBoundsPointerDeref.qll

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -91,11 +91,14 @@ predicate inBounds(PointerDereferenceInstruction ptrDeref) {
9191
length instanceof VNLength and
9292
offset instanceof OpOffset and
9393
b.getValueNumber() = length.(VNLength).getValueNumber() and
94+
// It holds that offset <= length + offsetBoundDelta
9495
boundedOperand(offset.(OpOffset).getOperand(), b, offsetBoundDelta, /*upper*/ true, _) and
95-
// this ensures that offset <= length holds
96-
offsetBoundDelta <= 0 and
97-
// with that we get that offset + offsetDelta < length offsetBoundDelta + lengthDelta - offsetBoundDelta
96+
// it also holds that
9897
offsetDelta < lengthDelta - offsetBoundDelta
98+
// taking both inequalities together we get
99+
// offset <= length + offsetBoundDelta
100+
// => offset + offsetDelta <= length + offsetBoundDelta + offsetDelta < length + offsetBoundDelta + lengthDelta - offsetBoundDelta
101+
// as required
99102
)
100103
)
101104
)

cpp/ql/test/experimental/library-tests/rangeanalysis/inboundsptr/InBounds.expected

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@
44
| test.cpp:27:13:27:16 | Load: access to array |
55
| test.cpp:33:5:33:12 | Store: ... = ... |
66
| test.cpp:48:5:48:16 | Store: ... = ... |
7-
| test.cpp:64:11:64:14 | Load: access to array |
8-
| test.cpp:68:5:68:12 | Store: ... = ... |
9-
| test.cpp:70:3:70:11 | Store: ... = ... |
10-
| test.cpp:74:3:74:18 | Store: ... = ... |
7+
| test.cpp:61:7:61:14 | Store: ... = ... |
8+
| test.cpp:70:7:70:14 | Store: ... = ... |
9+
| test.cpp:81:11:81:14 | Load: access to array |
10+
| test.cpp:85:5:85:12 | Store: ... = ... |
11+
| test.cpp:87:3:87:11 | Store: ... = ... |
12+
| test.cpp:91:3:91:18 | Store: ... = ... |

cpp/ql/test/experimental/library-tests/rangeanalysis/inboundsptr/test.cpp

Lines changed: 24 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -51,12 +51,29 @@ void test2(unsigned int count) {
5151
for(unsigned int i = 0; i < count; ++i) {
5252
a = (int*) malloc(sizeof(int) * count);
5353
for (int j = 0; j < count; ++j) {
54-
a[j] = 0; // in-bounds TODO not detected
54+
a[j] = 0; // in-bounds, but not detected due to RangeAnalysis shortcomings
5555
}
5656
}
57+
58+
for(unsigned int i = 0; i < 10; ++i) {
59+
a = (int*) malloc(sizeof(int) * i);
60+
for (unsigned int j = 0; j < i; ++j) {
61+
a[j] = 0; // in-bounds
62+
}
63+
}
64+
5765
}
66+
void test3(int count) {
67+
for(int i = 0; i < count; ++i) {
68+
int * a = (int*) malloc(sizeof(int) * i);
69+
for (int j = 0; j < i; ++j) {
70+
a[j] = 0; // in-bounds
71+
}
72+
}
73+
}
74+
5875

59-
void test3(unsigned long count) {
76+
void test4(unsigned long count) {
6077
if (count < 1) {
6178
return;
6279
}
@@ -74,13 +91,13 @@ void test3(unsigned long count) {
7491
a[count - 3] = 0; // in-bounds
7592
}
7693

77-
void test4(unsigned int count) {
94+
void test5(unsigned int count) {
7895
int* a = (int*) malloc(sizeof(int) * count);
7996
a[0] = 0; // unknown, call-site dependant
8097
}
8198

8299

83-
void test5(unsigned int count, bool b) {
100+
void test6(unsigned int count, bool b) {
84101
if(count < 4) {
85102
return;
86103
}
@@ -96,14 +113,14 @@ void test5(unsigned int count, bool b) {
96113
a[0] = 0; // unknown
97114
}
98115

99-
void test6(unsigned int object) {
116+
void test7(unsigned int object) {
100117
unsigned int* ptr = &object;
101118
*ptr = 0; // in-bounds, but needs ArrayLengthAnalysis improvements.
102119
}
103120

104-
void test7() {
121+
void test8() {
105122
void (*foo)(unsigned int);
106-
foo = &test6;
123+
foo = &test7;
107124
foo(4); // in-bounds, but needs ArrayLengthAnalysis improvements.
108125
}
109126

0 commit comments

Comments
 (0)