Skip to content

Commit 7391327

Browse files
authored
[NFC][analyzer] OOB test consolidation I: no-outofbounds.c (llvm#126539)
Before commit 6e17ed9 the test file `no-outofbounds.c` tested the behavior of the old alpha checker `alpha.security.ArrayBound` (V1); then that commit converted it into a test for the checker `security.ArrayBound` which was previously called `alpha.security.ArrayBoundV2`. This commit removes this small separate test file and adds some tests to the "native" test files of `security.ArrayBound` to ensure that the same functionality is still tested. This is intended as the first commit in a series that reorganizes the tests of `security.ArrayBound` to system that's easier to understand and maintain.
1 parent 560149b commit 7391327

File tree

3 files changed

+29
-34
lines changed

3 files changed

+29
-34
lines changed

clang/test/Analysis/no-outofbounds.c

Lines changed: 0 additions & 32 deletions
This file was deleted.

clang/test/Analysis/out-of-bounds-diagnostics.c

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -231,7 +231,16 @@ int arrayOfStructsArrow(void) {
231231
// expected-note@-2 {{Access of 'itemArray' at index 35, while it holds only 20 'struct item' elements}}
232232
}
233233

234+
char convertedScalar(long long var) {
235+
char *p = ((char*)&var);
236+
(void) p[3]; // no-warning
237+
return p[13];
238+
// expected-warning@-1 {{Out of bound access to memory after the end of 'var'}}
239+
// expected-note@-2 {{Access of 'var' at index 13, while it holds only 8 'char' elements}}
240+
}
241+
234242
short convertedArray(void) {
243+
(void) ((short*)TenElements)[17]; // no-warning
235244
return ((short*)TenElements)[47];
236245
// expected-warning@-1 {{Out of bound access to memory after the end of 'TenElements'}}
237246
// expected-note@-2 {{Access of 'TenElements' at index 47, while it holds only 20 'short' elements}}
@@ -268,23 +277,41 @@ int intFromStringDivisible(void) {
268277

269278
typedef __typeof(sizeof(int)) size_t;
270279
void *malloc(size_t size);
280+
void free(void *mem);
271281

272282
int *mallocRegion(void) {
273283
int *mem = (int*)malloc(2*sizeof(int));
274284

285+
mem[1] = 48; // no-warning
286+
275287
mem[3] = -2;
276288
// expected-warning@-1 {{Out of bound access to memory after the end of the heap area}}
277289
// expected-note@-2 {{Access of the heap area at index 3, while it holds only 2 'int' elements}}
278290
return mem;
279291
}
280292

293+
typedef struct { size_t len; int data[0]; } vec_t;
294+
295+
void mallocFlexibleArray(void) {
296+
vec_t *v = malloc(sizeof(vec_t) + 10 * sizeof(int));
297+
v->len = 10;
298+
v->data[1] = 5; // no-warning
299+
v->data[11] = 99;
300+
// TODO: Here ideally we would expect
301+
// {{Out of bound access to memory after the end of the heap area}}
302+
// {{Access of the heap area at index 11, while it holds only 10 'int' elements}}
303+
// but the analyzer cannot (yet) deduce the size of the flexible array member
304+
// from the size of the whole allocated area.
305+
free(v);
306+
}
307+
281308
int *custom_calloc(size_t a, size_t b) {
282309
size_t res;
283310

284311
return __builtin_mul_overflow(a, b, &res) ? 0 : malloc(res);
285312
}
286313

287-
int *mallocRegionOverflow(void) {
314+
int *mallocMulOverflow(void) {
288315
int *mem = (int*)custom_calloc(10, sizeof(int));
289316

290317
mem[20] = 10;

clang/test/Analysis/out-of-bounds.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,7 @@ void test_assume_after_access(unsigned long x) {
153153
int *get_symbolic(void);
154154
void test_underflow_symbolic(void) {
155155
int *buf = get_symbolic();
156-
buf[-1] = 0; // no-warning;
156+
buf[-1] = 0; // no-warning
157157
}
158158

159159
// But warn if we understand the internal memory layout of a symbolic region.

0 commit comments

Comments
 (0)