Debian Codegraph @

gcc/testsuite/g++.dg/warn/Wplacement-new-size.C

raw
1 /* { dg-do compile } */
2 /* { dg-options "-Wplacement-new -fpermissive" } */
3
4 typedef __typeof__ (sizeof 0) size_t;
5
6 typedef int int32_t __attribute__((mode (__SI__)));
7
8 void* operator new (size_t, void *p) { return p; }
9 void* operator new[] (size_t, void *p) { return p; }
10
11 static __attribute__ ((used))char c;
12 static __attribute__ ((used))char ac1 [1];
13 static __attribute__ ((used))char ac2 [2];
14 static __attribute__ ((used))char ac3 [3];
15 static __attribute__ ((used))char ac4 [4];
16 static __attribute__ ((used))char ac5 [5];
17 static __attribute__ ((used))char ac6 [6];
18 static __attribute__ ((used))char ac7 [7];
19 static __attribute__ ((used))char ac8 [8];
20
21 static __attribute__ ((used))char ac1_1 [1][1];
22 static __attribute__ ((used))char ac1_2 [1][2];
23 static __attribute__ ((used))char ac2_1 [2][1];
24 static __attribute__ ((used))char ac2_2 [2][2];
25
26 static __attribute__ ((used))short s;
27 static __attribute__ ((used))short as1 [1];
28 static __attribute__ ((used))short as2 [2];
29
30 static __attribute__ ((used))struct SC { char c; char *pc; void *pv; } sc;
31 static __attribute__ ((used))struct SAC1 { char ac [1]; } sac1;
32 static __attribute__ ((used))struct SAC2 { char ac [2]; } sac2;
33 static __attribute__ ((used))struct SAC3 { char ac [3]; } sac3;
34 static __attribute__ ((used))struct SAC4 { char ac [4]; } sac4;
35
36 static __attribute__ ((used))struct SSC { SC sc; int32_t x; } ssc;
37 static __attribute__ ((used))struct SSAC1 { SAC1 sac; } ssac1;
38 static __attribute__ ((used))struct SSAC2 { SAC2 sac; } ssac2;
39 static __attribute__ ((used))struct SSAC3 { SAC3 sac; } ssac3;
40 static __attribute__ ((used))struct SSAC4 { SAC4 sac; } ssac4;
41
42 static __attribute__ ((used))struct SSAC4_2 { SSAC4 ssac4_2 [2]; } sssac4_2;
43
44 static __attribute__ ((used))union UAC1 { char c; char ac [1]; } uac1;
45 static __attribute__ ((used))union UAC2 { char c; char ac [2]; } uac2;
46 static __attribute__ ((used))union UAC3 { char c; char ac [3]; } uac3;
47 static __attribute__ ((used))union UAC4 { char c; char ac [4]; } uac4;
48
49 static __attribute__ ((used))SC fsc () { return SC (); }
50 static __attribute__ ((used))SAC1 fasc1 () { return SAC1 (); }
51 static __attribute__ ((used))SAC2 fasc2 () { return SAC2 (); }
52 static __attribute__ ((used))SAC3 fasc3 () { return SAC3 (); }
53 static __attribute__ ((used))SAC4 fasc4 () { return SAC4 (); }
54
55 static __attribute__ ((used))void *r;
56
57 static __attribute__ ((used))void* ptr () { return 0; }
58
59 static __attribute__ ((used))
60 void test (void *p, int32_t n)
61 {
62 {
63 void *q = p;
64 struct { void *p; } s = { p };
65
66 // Verify that none of function arguments, local or global
67 // variables, or function return values trigger the warning.
68 new (p) char;
69 new (q) char;
70 new (r) char;
71 new (s.p) char;
72 new (ptr ()) char;
73
74 new (p) char [32];
75 new (q) char [32];
76 new (r) char [32];
77 new (s.p) char [32];
78 new (ptr ()) char [32];
79
80 new (&p) char;
81 new (&q) char;
82 new (&r) char;
83
84 // Using address of objects, however, must trigger the warning.
85 new (&p) char [32]; // { dg-warning "placement" }
86 new (&q) char [32]; // { dg-warning "placement" }
87 new (&r) char [32]; // { dg-warning "placement" }
88 }
89
90 enum { N0, N1, N2, N3 };
91
92 new (&c) char;
93
94 // Warn for the common case when constructing at an offset from
95 // the beginning of an array that doesn't leave enough space for
96 // the object.
97 new (&c + 0) char; // okay
98 new (&c + n) char; // okay (n is unknown)
99 new (&c + 1) char; // { dg-warning "placement" }
100 new (&c + N0) char;
101 new (&c + N1) char; // { dg-warning "placement" }
102
103 // No warning is issued when constructing an array in space exactly
104 // its size even though strictly speaking a compiler is allowed to
105 // add a cookie to the array (gcc does not).
106 new (&c) char [1];
107 new (&c) char [sizeof c];
108 new (&c) char [n];
109 new (&c) char [1][1];
110 new (&c) char [1][1][1];
111 new (&c + N1) char [1][1][1]; // { dg-warning "placement" }
112
113 new (&c) char [2]; // { dg-warning "placement" }
114 new (&c) char [sizeof c + 1]; // { dg-warning "placement" }
115 new (&c) char [1][2]; // { dg-warning "placement" }
116 new (&c) char [2][1]; // { dg-warning "placement" }
117 new (&c) char [n][1];
118 new (&c) char [n][2]; // { dg-warning "placement" }
119 new (&c) char [3]; // { dg-warning "placement" }
120 new (&c) char [3][1]; // { dg-warning "placement" }
121 new (&c) char [1][3]; // { dg-warning "placement" }
122 new (&c) char [4][1]; // { dg-warning "placement" }
123 new (&c) char [1][4]; // { dg-warning "placement" }
124
125 // Casts must not suppress it.
126 new ((void*)&c) char [2]; // { dg-warning "placement" }
127 new ((char*)&c) char [3]; // { dg-warning "placement" }
128
129 new (static_cast<void*>(&c)) char [4]; // { dg-warning "placement" }
130 new (reinterpret_cast<char*>(&c)) char [5]; // { dg-warning "placement" }
131
132 new (&c + 0) char [2]; // { dg-warning "placement" }
133 new (&c + 0) char [3]; // { dg-warning "placement" }
134 new (&c + 0) char [4]; // { dg-warning "placement" }
135
136 new (&c + 1) char [2]; // { dg-warning "placement" }
137 new (&c + 1) char [3]; // { dg-warning "placement" }
138 new (&c + 1) char [4]; // { dg-warning "placement" }
139
140 new (&c + N0) char [1];
141 new (&c + N1) char [2]; // { dg-warning "placement" }
142
143 // Warn even though n is unknown since c is too small for char[2]
144 // regardless of the value of n.
145 new (&c + n) char [2]; // { dg-warning "placement" }
146
147 new (ac2) char [1];
148 new (ac2) char [1][1];
149 new (ac2) char [1][2];
150 new (ac2) char [2][1];
151 new (ac2) char [1][3]; // { dg-warning "placement" }
152 new (ac2) char [2][2]; // { dg-warning "placement" }
153 new (ac2) char [3][1]; // { dg-warning "placement" }
154
155 new (ac2 + N0) char [1][1];
156 new (ac2 + N0) char [1][2];
157 new (ac2 + N1) char [1][2]; // { dg-warning "placement" }
158 new (ac2 + N1) char [2][1]; // { dg-warning "placement" }
159 new (ac2 + N2) char [1][1]; // { dg-warning "placement" }
160 new (ac2 + N2) char [1][2]; // { dg-warning "placement" }
161 new (ac2 + N2) char [2][1]; // { dg-warning "placement" }
162 new (ac2 + N2) char [2][2]; // { dg-warning "placement" }
163
164 new (ac8) char [1];
165 new (ac8) char [2][2];
166 new (ac8) char [2][3];
167 new (ac8) char [2][4];
168 new (ac8) char [2][5]; // { dg-warning "placement" }
169 new (ac8) char [2][2][2];
170 new (ac8) char [2][2][3]; // { dg-warning "placement" }
171
172 new (&c) int32_t; // { dg-warning "placement" }
173
174 new (&ac1) int32_t; // { dg-warning "placement" }
175 new (&ac2) int32_t; // { dg-warning "placement" }
176 new (&ac3) int32_t; // { dg-warning "placement" }
177 new (&ac4) int32_t;
178
179 // Constructing at an address of an array element.
180 new (&ac1 [0]) int32_t; // { dg-warning "placement" }
181 new (&ac2 [0]) int32_t; // { dg-warning "placement" }
182 new (&ac3 [0]) int32_t; // { dg-warning "placement" }
183 new (&ac4 [0]) int32_t;
184
185 // ...plus or minus a constant offset.
186 new (&ac1 [0] + 0) int32_t; // { dg-warning "placement" }
187 new (&ac2 [0] + 0) int32_t; // { dg-warning "placement" }
188 new (&ac3 [0] + 0) int32_t; // { dg-warning "placement" }
189 new (&ac4 [0] + 0) int32_t;
190 new (&ac4 [1] + 0) int32_t; // { dg-warning "placement" }
191 new (&ac4 [1] - 1) int32_t;
192 new (&ac4 [2] - 1) int32_t; // { dg-warning "placement" }
193 new (&ac4 [2] - 2) int32_t;
194 new (&ac4 [3] - 1) int32_t; // { dg-warning "placement" }
195 new (&ac4 [3] - 2) int32_t; // { dg-warning "placement" }
196 new (&ac4 [3] - 3) int32_t;
197 new (&ac4 [4] - 1) int32_t; // { dg-warning "placement" }
198 new (&ac4 [4] - 2) int32_t; // { dg-warning "placement" }
199 new (&ac4 [4] - 3) int32_t; // { dg-warning "placement" }
200 new (&ac4 [4] - 4) int32_t;
201
202 new (&ac1 [0] + 1) int32_t; // { dg-warning "placement" }
203 new (&ac2 [0] + 1) int32_t; // { dg-warning "placement" }
204 new (&ac3 [0] + 1) int32_t; // { dg-warning "placement" }
205 new (&ac4 [0] + 1) int32_t; // { dg-warning "placement" }
206
207 new (&ac3 [0] + n) int32_t; // { dg-warning "placement" }
208 new (&ac4 [0] + n) int32_t; // no warning (n could be zero)
209 new (&ac4 [1] + n) int32_t; // no warning (n could be negative)
210 new (&ac4 [2] + n) int32_t; // ditto
211 new (&ac4 [3] + n) int32_t; // ditto
212 new (&ac4 [4] + n) int32_t; // ditto
213 new (&ac4 [4] - n) int32_t; // (or positive)
214
215 new (&c + 0) int32_t; // { dg-warning "placement" }
216 new (&c + 1) int32_t; // { dg-warning "placement" }
217
218 // Constructing at an offset into the address of an array.
219 new (&ac1 + 0) int32_t; // { dg-warning "placement" }
220 new (&ac1 + 1) int32_t; // { dg-warning "placement" }
221 new (&ac1 + n) int32_t; // { dg-warning "placement" }
222 new (&ac2 + 0) int32_t; // { dg-warning "placement" }
223 new (&ac2 + 1) int32_t; // { dg-warning "placement" }
224 new (&ac2 + n) int32_t; // { dg-warning "placement" }
225 new (&ac3 + 0) int32_t; // { dg-warning "placement" }
226 new (&ac3 + 1) int32_t; // { dg-warning "placement" }
227
228 // Even though n below is uknown an array of 3 bytes isn't large
229 // enugh for an int32_t.
230 new (&ac3 + n) int32_t; // { dg-warning "placement" }
231
232 new (&ac4 + 0) int32_t;
233 new (&ac4 + 1) int32_t; // { dg-warning "placement" }
234 new (&ac4 + n) int32_t; // no warning (n could be zero)
235
236 // Constructing in an array object.
237 new (ac1) int32_t; // { dg-warning "placement" }
238 new (ac2) int32_t; // { dg-warning "placement" }
239 new (ac3) int32_t; // { dg-warning "placement" }
240 new (ac4) int32_t;
241 new (ac5) int32_t;
242 new (ac5 + 0) int32_t;
243 new (ac5 + 1) int32_t;
244 new (ac5 + n) int32_t; // no warning (n could be zero)
245 new (ac5 + 2) int32_t; // { dg-warning "placement" }
246 new (ac5 + 3) int32_t; // { dg-warning "placement" }
247 new (ac5 + 4) int32_t; // { dg-warning "placement" }
248 new (ac5 + 5) int32_t; // { dg-warning "placement" }
249
250 new (ac1_1) char;
251 new (ac1_1) char[1];
252 new (ac1_1) char[n]; // no warning (n is unknown)
253 new (ac1_1) char[2]; // { dg-warning "placement" }
254 new (ac1_1) char[3]; // { dg-warning "placement" }
255
256 new (ac1_2) char;
257 new (ac1_2) char[1];
258 new (ac1_2) char[2];
259 new (ac1_2) char[3]; // { dg-warning "placement" }
260
261 new (ac2_1) char;
262 new (ac2_1) char[1];
263 new (ac2_1) char[2];
264 new (ac2_1) char[3]; // { dg-warning "placement" }
265
266 new (ac2_2) char;
267 new (ac2_2) char[1];
268 new (ac2_2) char[2];
269 new (ac2_2) char[2][2];
270
271 // Even though n below is uknown it can't meaningfully be zero
272 // (even if zero-size arrays are allowed as an extension, the size
273 // they are allocated in by placement new is zero).
274 new (ac1_1) char[n][2]; // { dg-warning "placement" }
275 new (ac2_2) char[3];
276 new (ac2_2) char[3][1];
277 new (ac2_2) char[3][2]; // { dg-warning "placement" }
278 new (ac2_2) char[4];
279 new (ac2_2) char[4][1];
280 new (ac2_2) char[4][2]; // { dg-warning "placement" }
281 new (ac2_2) char[5]; // { dg-warning "placement" }
282
283 new (&s) int32_t; // { dg-warning "placement" }
284 new (&as1) int32_t; // { dg-warning "placement" }
285 new (&as2) int32_t;
286
287 new (as1) int32_t; // { dg-warning "placement" }
288 new (as2) int32_t;
289
290 new (&sc.c) int32_t; // { dg-warning "placement" }
291 new (&sac1.ac) int32_t; // { dg-warning "placement" }
292 new (&sac2.ac) int32_t; // { dg-warning "placement" }
293 new (&sac3.ac) int32_t; // { dg-warning "placement" }
294 new (&sac4.ac) int32_t;
295
296 new (sc.pc) char;
297 new (sc.pc) int32_t;
298 new (sc.pc) int32_t[1024];
299 new (sc.pc + 0) int32_t;
300 new (sc.pc + 0) int32_t[2048];
301 new (sc.pv) int32_t;
302 new (sc.pv) char[1024];
303
304 new (sac1.ac) int32_t; // { dg-warning "placement" }
305 new (sac2.ac) int32_t; // { dg-warning "placement" }
306 new (sac3.ac) int32_t; // { dg-warning "placement" }
307 new (sac4.ac) int32_t;
308
309 new (&ssc.sc) SSC; // { dg-warning "placement" }
310 new (&ssac1.sac) int32_t; // { dg-warning "placement" }
311 new (&ssac2.sac) int32_t; // { dg-warning "placement" }
312 new (&ssac3.sac) int32_t; // { dg-warning "placement" }
313 new (&ssac4.sac) int32_t;
314
315 new (&sssac4_2) char[sizeof sssac4_2];
316 new (&sssac4_2) char[sizeof sssac4_2 + 1]; // { dg-warning "placement" }
317
318 // taking the address of a temporary is allowed with -fpermissive
319 new (&fsc ().c) int32_t; // { dg-warning "18:taking address|placement" }
320 new (&fasc1 ().ac) int32_t; // { dg-warning "20:taking address|placement" }
321 new (&fasc2 ().ac) int32_t; // { dg-warning "20:taking address|placement" }
322 new (&fasc3 ().ac) int32_t; // { dg-warning "20:taking address|placement" }
323 new (&fasc4 ().ac) int32_t; // { dg-warning "20:taking address|placement" }
324
325 new (&uac1) int32_t; // { dg-warning "placement" }
326 new (&uac2) int32_t; // { dg-warning "placement" }
327 new (&uac3) int32_t; // { dg-warning "placement" }
328 new (&uac4) int32_t;
329 new (&uac4 + 1) int32_t; // { dg-warning "placement" }
330
331 new (&uac1.c) int32_t; // { dg-warning "placement" }
332 new (&uac2.c) int32_t; // { dg-warning "placement" }
333 new (&uac3.c) int32_t; // { dg-warning "placement" }
334
335 /* The following isn't diagnosed (anymore) for consistency with
336 the middle end where members of unions are considered to extend
337 to the end of the enclosing object.
338 See gcc.dg/Wstringop-overflow-60.c for the middle end test. */
339 new (&uac4.c) int32_t;
340
341 new (&uac4.c + 1) int32_t; // { dg-warning "placement" }
342 }
343
344
345 struct S { char c [2]; };
346
347 // Verify the full text of the warning message.
348 static __attribute__ ((used))
349 void test_message (int32_t i)
350 {
351 char a [2];
352
353 // The exact sizes of both the buffer and the type are known.
354 new (a + 1) S; // { dg-warning "placement new constructing an object of type .S. and size .2. in a region of type .char \\\[2\\\]. and size .1." }
355
356 // The buffer size is known but only the size of the type whose
357 // objects are being constructed is known, not their number. While
358 // in theory it could be zero, it practice likely never will be so
359 // the potential false positive is acceptable.
360 new (a + 1) S [i]; // { dg-warning "placement new constructing an array of objects of type .S. and size .2. in a region of type .char \\\[2\\\]. and size .1." }
361
362 // The exact amount of space in the buffer isn't known, only its
363 // maximum is. The exact size of the array being created is known.
364 new (a + i) S [2]; // { dg-warning "placement new constructing an object of type .S \\\[2\\\]. and size .4. in a region of type .char \\\[2\\\]. and size at most .2." }
365 }
366
367
368 struct ClassWithMemberNew {
369 struct Object { int32_t i; } *pobj;
370 unsigned nobj;
371
372 ClassWithMemberNew ();
373 void foo ();
374
375 void* operator new (size_t, void*);
376 void* operator new[] (size_t, void*);
377 };
378
379 void ClassWithMemberNew::foo()
380 {
381 for (unsigned i = 0; i != nobj; ++i)
382 new (pobj + i) Object ();
383 }
384
385
386 struct ClassWithGlobalNew {
387 int32_t a [4];
388 ClassWithGlobalNew ();
389 };
390
391 void* operator new (size_t, ClassWithGlobalNew*);
392 void* operator new[] (size_t, ClassWithGlobalNew*);
393
394 void test_user_defined_placement_new ()
395 {
396 {
397 ClassWithMemberNew x;
398
399 // Expect no diagnostics for placement new expressions with types
400 // with their own placement operator new since the semantics of
401 // the operator aren't known.
402 new (&c) ClassWithMemberNew;
403 new (&x) ClassWithMemberNew[2];
404 }
405
406 {
407 ClassWithGlobalNew x;
408
409 new (&c) ClassWithGlobalNew; // { dg-warning "placement" }
410 new (&x) ClassWithGlobalNew[2];
411 }
412 }
413
414 extern char extbuf[];
415
416 template <class> struct TemplateClass { char c; };
417
418 // Declare a specialization but don't provide a definition.
419 template <> struct TemplateClass<void>;
420
421 // Declare an object of an explicit specialization of an unknown size.
422 extern TemplateClass<void> exttempl_void;
423
424 // Verify that no warning is issued when placement new is called with
425 // an extern buffer of unknown size (and the case is handled gracefully
426 // and doesn't cause an ICE).
427 static __attribute__ ((used))
428 void test_extern_buffer_of_unknown_size ()
429 {
430 new (extbuf) int32_t ();
431 new (extbuf) int32_t [1024];
432
433 new (&exttempl_void) int32_t ();
434 new (&exttempl_void) int32_t [1024];
435 }
436
437 extern char extbuf_size_int [sizeof (int32_t)];
438
439 extern TemplateClass<int32_t> exttempl;
440
441 // Verify that a warning is issued as expected when placement new is
442 // called with an extern buffer of known size (and the case is handled
443 // gracefully and doesn't cause an ICE).
444 static __attribute__ ((used))
445 void test_extern_buffer ()
446 {
447 new (extbuf_size_int) int32_t ();
448 new (extbuf_size_int) int32_t [1];
449
450 struct S { int32_t a [2]; };
451
452 new (extbuf_size_int) S; // { dg-warning "placement" }
453 new (extbuf_size_int) int32_t [2]; // { dg-warning "placement" }
454
455 new (&exttempl) int32_t (); // { dg-warning "placement" }
456 new (&exttempl) int32_t [1024]; // { dg-warning "placement" }
457 }