Debian Codegraph @

gcc/testsuite/g++.dg/warn/Warray-bounds-11.C

raw
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 nothrow operator
3 new() 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 #define OP_NEW(n) operator new (n, std::nothrow)
31 #define T(T, n, i) do { \
32 T *p = (T*) OP_NEW (n); \
33 p[i] = 0; \
34 sink (p); \
35 } while (0)
36
37 void warn_op_new ()
38 {
39 T (int32_t, 0, 0); // { dg-warning "array subscript 0 is outside array bounds of 'int32_t \\\[0]'" }
40 // { dg-message "object of size \\d allocated by '\[^\n\r]*operator new\[^\n\r]*'" "note" { target *-*-* } .-1 }
41 T (int32_t, 1, 0); // { dg-warning "array subscript 'int32_t {aka (long )?int}\\\[0]' is partly outside array bounds of 'unsigned char \\\[1]'" }
42 T (int32_t, 2, 0); // { dg-warning "array subscript 'int32_t {aka (long )?int}\\\[0]' is partly outside array bounds of 'unsigned char \\\[2]'" }
43 T (int32_t, 3, 0); // { dg-warning "array subscript 'int32_t {aka (long )?int}\\\[0]' is partly outside array bounds of 'unsigned char \\\[3]'" }
44
45 T (int32_t, 4, 0);
46
47 T (int32_t, 0, 1); // { dg-warning "array subscript 1 is outside array bounds of 'int32_t \\\[0]'" }
48 T (int32_t, 1, 1); // { dg-warning "array subscript 1 is outside array bounds " }
49 T (int32_t, 2, 1); // { dg-warning "array subscript 1 is outside array bounds " }
50 T (int32_t, 3, 1); // { dg-warning "array subscript 1 is outside array bounds " }
51 T (int32_t, 4, 1); // { dg-warning "array subscript 1 is outside array bounds " }
52 T (int32_t, 5, 1); // { dg-warning "array subscript 'int32_t {aka (long )?int}\\\[1]' is partly outside array bounds of 'unsigned char \\\[5]" }
53 T (int32_t, 6, 1); // { dg-warning "array subscript 'int32_t {aka (long )?int}\\\[1]' is partly outside array bounds of 'unsigned char \\\[6]" }
54 T (int32_t, 7, 1); // { dg-warning "array subscript 'int32_t {aka (long )?int}\\\[1]' is partly outside array bounds of 'unsigned char \\\[7]" }
55
56 T (int32_t, 8, 1);
57 }
58
59
60 void warn_op_array_new ()
61 {
62 #undef OP_NEW
63 #define OP_NEW(n) operator new[] (n, std::nothrow)
64
65 T (int32_t, 0, 0); // { dg-warning "array subscript 0 is outside array bounds of 'int32_t \\\[0]'" }
66 // { dg-message "object of size \\d allocated by '\[^\n\r]*operator new\[^\n\r]*'" "note" { target *-*-* } .-1 }
67 T (int32_t, 1, 0); // { dg-warning "array subscript 'int32_t {aka (long )?int}\\\[0]' is partly outside array bounds of 'unsigned char \\\[1]'" }
68 T (int32_t, 2, 0); // { dg-warning "array subscript 'int32_t {aka (long )?int}\\\[0]' is partly outside array bounds of 'unsigned char \\\[2]'" }
69 T (int32_t, 3, 0); // { dg-warning "array subscript 'int32_t {aka (long )?int}\\\[0]' is partly outside array bounds of 'unsigned char \\\[3]'" }
70
71 T (int32_t, 4, 0);
72
73 T (int32_t, 0, 1); // { dg-warning "array subscript 1 is outside array bounds of 'int32_t \\\[0]'" }
74 T (int32_t, 1, 1); // { dg-warning "array subscript 1 is outside array bounds " }
75 T (int32_t, 2, 1); // { dg-warning "array subscript 1 is outside array bounds " }
76 T (int32_t, 3, 1); // { dg-warning "array subscript 1 is outside array bounds " }
77 T (int32_t, 4, 1); // { dg-warning "array subscript 1 is outside array bounds " }
78 T (int32_t, 5, 1); // { dg-warning "array subscript 'int32_t {aka (long )?int}\\\[1]' is partly outside array bounds of 'unsigned char \\\[5]" }
79 T (int32_t, 6, 1); // { dg-warning "array subscript 'int32_t {aka (long )?int}\\\[1]' is partly outside array bounds of 'unsigned char \\\[6]" }
80 T (int32_t, 7, 1); // { dg-warning "array subscript 'int32_t {aka (long )?int}\\\[1]' is partly outside array bounds of 'unsigned char \\\[7]" }
81
82 T (int32_t, 8, 1);
83 }