| 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 nothrow form |
| 3 |
of the new expression are diagnosed. |
| 4 |
{ dg-do compile } |
| 5 |
{ dg-options "-O2 -Wall -Warray-bounds -ftrack-macro-expansion=0" } */ |
| 6 |
|
| 7 |
#if 0 |
| 8 |
// Avoid including <new> to make cross-compiler testing easy. |
| 9 |
// #include <new> |
| 10 |
#else |
| 11 |
namespace std { |
| 12 |
|
| 13 |
typedef __SIZE_TYPE__ size_t; |
| 14 |
struct nothrow_t { }; |
| 15 |
extern const nothrow_t nothrow; |
| 16 |
|
| 17 |
} |
| 18 |
|
| 19 |
void* operator new (std::size_t, const std::nothrow_t &) throw () |
| 20 |
__attribute__ ((__alloc_size__ (1), __malloc__)); |
| 21 |
void* operator new[] (std::size_t, const std::nothrow_t &) throw () |
| 22 |
__attribute__ ((__alloc_size__ (1), __malloc__)); |
| 23 |
|
| 24 |
#endif |
| 25 |
|
| 26 |
typedef __INT32_TYPE__ int32_t; |
| 27 |
|
| 28 |
void sink (void*); |
| 29 |
|
| 30 |
template <int N> struct S { char a[N]; }; |
| 31 |
|
| 32 |
void sink (void*); |
| 33 |
|
| 34 |
#define NEW(n) new (std::nothrow) S<n> |
| 35 |
#define T(T, n, i) do { \ |
| 36 |
T *p = (T*)NEW (n); \ |
| 37 |
p[i] = 0; \ |
| 38 |
sink (p); \ |
| 39 |
} while (0) |
| 40 |
|
| 41 |
void warn_nothrow_new () |
| 42 |
{ |
| 43 |
T (int32_t, 0, 0); // { dg-warning "array subscript 0 is outside array bounds of 'int32_t \\\[0]'" } |
| 44 |
// { dg-message "object of size \\d allocated by '\[^\n\r]*operator new\[^\n\r]*'" "note" { target *-*-* } .-1 } |
| 45 |
T (int32_t, 1, 0); // { dg-warning "array subscript 'int32_t {aka (long )?int}\\\[0]' is partly outside array bounds of 'unsigned char \\\[1]'" } |
| 46 |
T (int32_t, 2, 0); // { dg-warning "array subscript 'int32_t {aka (long )?int}\\\[0]' is partly outside array bounds of 'unsigned char \\\[2]'" } |
| 47 |
T (int32_t, 3, 0); // { dg-warning "array subscript 'int32_t {aka (long )?int}\\\[0]' is partly outside array bounds of 'unsigned char \\\[3]'" } |
| 48 |
|
| 49 |
T (int32_t, 4, 0); |
| 50 |
|
| 51 |
T (int32_t, 0, 1); // { dg-warning "array subscript 1 is outside array bounds of 'int32_t \\\[0]'" } |
| 52 |
T (int32_t, 1, 1); // { dg-warning "array subscript 1 is outside array bounds " } |
| 53 |
T (int32_t, 2, 1); // { dg-warning "array subscript 1 is outside array bounds " } |
| 54 |
T (int32_t, 3, 1); // { dg-warning "array subscript 1 is outside array bounds " } |
| 55 |
T (int32_t, 4, 1); // { dg-warning "array subscript 1 is outside array bounds " } |
| 56 |
T (int32_t, 5, 1); // { dg-warning "array subscript 'int32_t {aka (long )?int}\\\[1]' is partly outside array bounds of 'unsigned char \\\[5]" } |
| 57 |
T (int32_t, 6, 1); // { dg-warning "array subscript 'int32_t {aka (long )?int}\\\[1]' is partly outside array bounds of 'unsigned char \\\[6]" } |
| 58 |
T (int32_t, 7, 1); // { dg-warning "array subscript 'int32_t {aka (long )?int}\\\[1]' is partly outside array bounds of 'unsigned char \\\[7]" } |
| 59 |
|
| 60 |
T (int32_t, 8, 1); |
| 61 |
} |
| 62 |
|
| 63 |
|
| 64 |
void warn_nothrow_array_new () |
| 65 |
{ |
| 66 |
#undef NEW |
| 67 |
#define NEW(n) new (std::nothrow) char [n] |
| 68 |
|
| 69 |
T (int32_t, 0, 0); // { dg-warning "array subscript 0 is outside array bounds of 'int32_t \\\[0]'" } |
| 70 |
// { dg-message "object of size \\d allocated by '\[^\n\r]*operator new\[^\n\r]*'" "note" { target *-*-* } .-1 } |
| 71 |
T (int32_t, 1, 0); // { dg-warning "array subscript 'int32_t {aka (long )?int}\\\[0]' is partly outside array bounds of 'unsigned char \\\[1]'" } |
| 72 |
T (int32_t, 2, 0); // { dg-warning "array subscript 'int32_t {aka (long )?int}\\\[0]' is partly outside array bounds of 'unsigned char \\\[2]'" } |
| 73 |
T (int32_t, 3, 0); // { dg-warning "array subscript 'int32_t {aka (long )?int}\\\[0]' is partly outside array bounds of 'unsigned char \\\[3]'" } |
| 74 |
|
| 75 |
T (int32_t, 4, 0); |
| 76 |
|
| 77 |
T (int32_t, 0, 1); // { dg-warning "array subscript 1 is outside array bounds of 'int32_t \\\[0]'" } |
| 78 |
T (int32_t, 1, 1); // { dg-warning "array subscript 1 is outside array bounds " } |
| 79 |
T (int32_t, 2, 1); // { dg-warning "array subscript 1 is outside array bounds " } |
| 80 |
T (int32_t, 3, 1); // { dg-warning "array subscript 1 is outside array bounds " } |
| 81 |
T (int32_t, 4, 1); // { dg-warning "array subscript 1 is outside array bounds " } |
| 82 |
T (int32_t, 5, 1); // { dg-warning "array subscript 'int32_t {aka (long )?int}\\\[1]' is partly outside array bounds of 'unsigned char \\\[5]" } |
| 83 |
T (int32_t, 6, 1); // { dg-warning "array subscript 'int32_t {aka (long )?int}\\\[1]' is partly outside array bounds of 'unsigned char \\\[6]" } |
| 84 |
T (int32_t, 7, 1); // { dg-warning "array subscript 'int32_t {aka (long )?int}\\\[1]' is partly outside array bounds of 'unsigned char \\\[7]" } |
| 85 |
|
| 86 |
T (int32_t, 8, 1); |
| 87 |
} |