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.
5 { dg-options "-O2 -Wall -Warray-bounds -ftrack-macro-expansion=0" } */
8 // Avoid including <new> to make cross-compiler testing easy.
13 typedef __SIZE_TYPE__ size_t;
15 extern const nothrow_t nothrow;
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__));
26 typedef __INT32_TYPE__ int32_t;
30 template <int N> struct S { char a[N]; };
34 #define NEW(n) new (std::nothrow) S<n>
35 #define T(T, n, i) do { \
41 void warn_nothrow_new ()
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]'" }
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]" }
64 void warn_nothrow_array_new ()
67 #define NEW(n) new (std::nothrow) char [n]
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]'" }
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]" }