| 1 |
/* PR c++/97201 - ICE in -Warray-bounds writing to result of operator new(0) |
| 2 |
Verify that out-of-bounds accesses to memory returned by the new expression |
| 3 |
are diagnosed. |
| 4 |
{ dg-do compile } |
| 5 |
{ dg-options "-O2 -Wall -Warray-bounds -ftrack-macro-expansion=0" } */ |
| 6 |
|
| 7 |
typedef __INT32_TYPE__ int32_t; |
| 8 |
|
| 9 |
template <int N> struct S { char a[N]; }; |
| 10 |
|
| 11 |
void sink (void*); |
| 12 |
|
| 13 |
#define NEW(n) new S<n> |
| 14 |
#define T(T, n, i) do { \ |
| 15 |
T *p = (T*)NEW (n); \ |
| 16 |
p[i] = 0; \ |
| 17 |
sink (p); \ |
| 18 |
} while (0) |
| 19 |
|
| 20 |
void warn_new () |
| 21 |
{ |
| 22 |
T (int32_t, 0, 0); // { dg-warning "array subscript 0 is outside array bounds of 'int32_t \\\[0]'" } |
| 23 |
// { dg-message "object of size \\d allocated by '\[^\n\r]*operator new\[^\n\r]*'" "note" { target *-*-* } .-1 } |
| 24 |
T (int32_t, 1, 0); // { dg-warning "array subscript 'int32_t {aka (long )?int}\\\[0]' is partly outside array bounds of 'unsigned char \\\[1]'" } |
| 25 |
T (int32_t, 2, 0); // { dg-warning "array subscript 'int32_t {aka (long )?int}\\\[0]' is partly outside array bounds of 'unsigned char \\\[2]'" } |
| 26 |
T (int32_t, 3, 0); // { dg-warning "array subscript 'int32_t {aka (long )?int}\\\[0]' is partly outside array bounds of 'unsigned char \\\[3]'" } |
| 27 |
|
| 28 |
T (int32_t, 4, 0); |
| 29 |
|
| 30 |
T (int32_t, 0, 1); // { dg-warning "array subscript 1 is outside array bounds of 'int32_t \\\[0]'" } |
| 31 |
T (int32_t, 1, 1); // { dg-warning "array subscript 1 is outside array bounds " } |
| 32 |
T (int32_t, 2, 1); // { dg-warning "array subscript 1 is outside array bounds " } |
| 33 |
T (int32_t, 3, 1); // { dg-warning "array subscript 1 is outside array bounds " } |
| 34 |
T (int32_t, 4, 1); // { dg-warning "array subscript 1 is outside array bounds " } |
| 35 |
T (int32_t, 5, 1); // { dg-warning "array subscript 'int32_t {aka (long )?int}\\\[1]' is partly outside array bounds of 'unsigned char \\\[5]" } |
| 36 |
T (int32_t, 6, 1); // { dg-warning "array subscript 'int32_t {aka (long )?int}\\\[1]' is partly outside array bounds of 'unsigned char \\\[6]" } |
| 37 |
T (int32_t, 7, 1); // { dg-warning "array subscript 'int32_t {aka (long )?int}\\\[1]' is partly outside array bounds of 'unsigned char \\\[7]" } |
| 38 |
|
| 39 |
T (int32_t, 8, 1); |
| 40 |
} |
| 41 |
|
| 42 |
|
| 43 |
void warn_array_new () |
| 44 |
{ |
| 45 |
#undef NEW |
| 46 |
#define NEW(n) new char [n] |
| 47 |
|
| 48 |
T (int32_t, 0, 0); // { dg-warning "array subscript 0 is outside array bounds of 'int32_t \\\[0]'" } |
| 49 |
// { dg-message "object of size \\d allocated by '\[^\n\r]*operator new\[^\n\r]*'" "note" { target *-*-* } .-1 } |
| 50 |
T (int32_t, 1, 0); // { dg-warning "array subscript 'int32_t {aka (long )?int}\\\[0]' is partly outside array bounds of 'unsigned char \\\[1]'" } |
| 51 |
T (int32_t, 2, 0); // { dg-warning "array subscript 'int32_t {aka (long )?int}\\\[0]' is partly outside array bounds of 'unsigned char \\\[2]'" } |
| 52 |
T (int32_t, 3, 0); // { dg-warning "array subscript 'int32_t {aka (long )?int}\\\[0]' is partly outside array bounds of 'unsigned char \\\[3]'" } |
| 53 |
|
| 54 |
T (int32_t, 4, 0); |
| 55 |
|
| 56 |
T (int32_t, 0, 1); // { dg-warning "array subscript 1 is outside array bounds of 'int32_t \\\[0]'" } |
| 57 |
T (int32_t, 1, 1); // { dg-warning "array subscript 1 is outside array bounds " } |
| 58 |
T (int32_t, 2, 1); // { dg-warning "array subscript 1 is outside array bounds " } |
| 59 |
T (int32_t, 3, 1); // { dg-warning "array subscript 1 is outside array bounds " } |
| 60 |
T (int32_t, 4, 1); // { dg-warning "array subscript 1 is outside array bounds " } |
| 61 |
T (int32_t, 5, 1); // { dg-warning "array subscript 'int32_t {aka (long )?int}\\\[1]' is partly outside array bounds of 'unsigned char \\\[5]" } |
| 62 |
T (int32_t, 6, 1); // { dg-warning "array subscript 'int32_t {aka (long )?int}\\\[1]' is partly outside array bounds of 'unsigned char \\\[6]" } |
| 63 |
T (int32_t, 7, 1); // { dg-warning "array subscript 'int32_t {aka (long )?int}\\\[1]' is partly outside array bounds of 'unsigned char \\\[7]" } |
| 64 |
|
| 65 |
T (int32_t, 8, 1); |
| 66 |
} |