]> git.ipfire.org Git - thirdparty/gcc.git/blame - gcc/testsuite/g++.dg/warn/Warray-bounds-13.C
Correct handling of variable offset minus constant in -Warray-bounds [PR100137]
[thirdparty/gcc.git] / gcc / testsuite / g++.dg / warn / Warray-bounds-13.C
CommitLineData
1be51a3a
MS
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#include <new>
8
9typedef __INT32_TYPE__ int32_t;
10
11void sink (void*);
12
13template <int N> struct S { char a[N]; };
14
15void sink (void*);
16
17#define NEW(n) new (std::nothrow) S<n>
18#define T(T, n, i) do { \
19 T *p = (T*)NEW (n); \
20 p[i] = 0; \
21 sink (p); \
22 } while (0)
23
24void warn_nothrow_new ()
25{
26 T (int32_t, 0, 0); // { dg-warning "array subscript 0 is outside array bounds of 'int32_t \\\[0]'" }
a1108556 27 // { dg-message "object of size \\d allocated by '\[^\n\r]*operator new\[^\n\r]*'" "note" { target *-*-* } .-1 }
8e99b5ba
HPN
28 T (int32_t, 1, 0); // { dg-warning "array subscript 'int32_t {aka (long )?int}\\\[0]' is partly outside array bounds of 'unsigned char \\\[1]'" }
29 T (int32_t, 2, 0); // { dg-warning "array subscript 'int32_t {aka (long )?int}\\\[0]' is partly outside array bounds of 'unsigned char \\\[2]'" }
30 T (int32_t, 3, 0); // { dg-warning "array subscript 'int32_t {aka (long )?int}\\\[0]' is partly outside array bounds of 'unsigned char \\\[3]'" }
1be51a3a
MS
31
32 T (int32_t, 4, 0);
33
34 T (int32_t, 0, 1); // { dg-warning "array subscript 1 is outside array bounds of 'int32_t \\\[0]'" }
35 T (int32_t, 1, 1); // { dg-warning "array subscript 1 is outside array bounds " }
36 T (int32_t, 2, 1); // { dg-warning "array subscript 1 is outside array bounds " }
37 T (int32_t, 3, 1); // { dg-warning "array subscript 1 is outside array bounds " }
38 T (int32_t, 4, 1); // { dg-warning "array subscript 1 is outside array bounds " }
8e99b5ba
HPN
39 T (int32_t, 5, 1); // { dg-warning "array subscript 'int32_t {aka (long )?int}\\\[1]' is partly outside array bounds of 'unsigned char \\\[5]" }
40 T (int32_t, 6, 1); // { dg-warning "array subscript 'int32_t {aka (long )?int}\\\[1]' is partly outside array bounds of 'unsigned char \\\[6]" }
41 T (int32_t, 7, 1); // { dg-warning "array subscript 'int32_t {aka (long )?int}\\\[1]' is partly outside array bounds of 'unsigned char \\\[7]" }
1be51a3a
MS
42
43 T (int32_t, 8, 1);
44}
45
46
47void warn_nothrow_array_new ()
48{
49#undef NEW
50#define NEW(n) new (std::nothrow) char [n]
51
52 T (int32_t, 0, 0); // { dg-warning "array subscript 0 is outside array bounds of 'int32_t \\\[0]'" }
a1108556 53 // { dg-message "object of size \\d allocated by '\[^\n\r]*operator new\[^\n\r]*'" "note" { target *-*-* } .-1 }
8e99b5ba
HPN
54 T (int32_t, 1, 0); // { dg-warning "array subscript 'int32_t {aka (long )?int}\\\[0]' is partly outside array bounds of 'unsigned char \\\[1]'" }
55 T (int32_t, 2, 0); // { dg-warning "array subscript 'int32_t {aka (long )?int}\\\[0]' is partly outside array bounds of 'unsigned char \\\[2]'" }
56 T (int32_t, 3, 0); // { dg-warning "array subscript 'int32_t {aka (long )?int}\\\[0]' is partly outside array bounds of 'unsigned char \\\[3]'" }
1be51a3a
MS
57
58 T (int32_t, 4, 0);
59
60 T (int32_t, 0, 1); // { dg-warning "array subscript 1 is outside array bounds of 'int32_t \\\[0]'" }
61 T (int32_t, 1, 1); // { dg-warning "array subscript 1 is outside array bounds " }
62 T (int32_t, 2, 1); // { dg-warning "array subscript 1 is outside array bounds " }
63 T (int32_t, 3, 1); // { dg-warning "array subscript 1 is outside array bounds " }
64 T (int32_t, 4, 1); // { dg-warning "array subscript 1 is outside array bounds " }
8e99b5ba
HPN
65 T (int32_t, 5, 1); // { dg-warning "array subscript 'int32_t {aka (long )?int}\\\[1]' is partly outside array bounds of 'unsigned char \\\[5]" }
66 T (int32_t, 6, 1); // { dg-warning "array subscript 'int32_t {aka (long )?int}\\\[1]' is partly outside array bounds of 'unsigned char \\\[6]" }
67 T (int32_t, 7, 1); // { dg-warning "array subscript 'int32_t {aka (long )?int}\\\[1]' is partly outside array bounds of 'unsigned char \\\[7]" }
1be51a3a
MS
68
69 T (int32_t, 8, 1);
70}