1 // RUN: %clang_analyze_cc1 -std=c++11 %s \
2 // RUN: -analyzer-checker=core \
3 // RUN: -analyzer-checker=cplusplus.NewDelete \
4 // RUN: -analyzer-checker=cplusplus.PlacementNew \
5 // RUN: -analyzer-output=text -verify \
6 // RUN: -triple x86_64-unknown-linux-gnu
8 #include "Inputs/system-header-simulator-cxx.h"
11 short s
; // expected-note {{'s' declared without an initial value}}
12 long *lp
= ::new (&s
) long; // expected-warning{{Storage provided to placement new is only 2 bytes, whereas the allocated type requires 8 bytes}} expected-note 3 {{}}
16 namespace testArrayNew
{
18 short s
; // expected-note {{'s' declared without an initial value}}
19 char *buf
= ::new (&s
) char[8]; // expected-warning{{Storage provided to placement new is only 2 bytes, whereas the allocated type requires 8 bytes}} expected-note 3 {{}}
22 } // namespace testArrayNew
24 namespace testBufferInOtherFun
{
26 long *lp
= ::new (place
) long; // expected-warning{{Storage provided to placement new is only 2 bytes, whereas the allocated type requires 8 bytes}} expected-note 1 {{}}
30 short buf
; // expected-note {{'buf' declared without an initial value}}
31 f(&buf
); // expected-note 2 {{}}
33 } // namespace testBufferInOtherFun
35 namespace testArrayBuffer
{
37 long *lp
= ::new (place
) long; // expected-warning{{Storage provided to placement new is only 2 bytes, whereas the allocated type requires 8 bytes}} expected-note 1 {{}}
41 char buf
[2]; // expected-note {{'buf' initialized here}}
42 f(&buf
); // expected-note 2 {{}}
44 } // namespace testArrayBuffer
46 namespace testGlobalPtrAsPlace
{
50 gptr
= &gs
; // expected-note {{Value assigned to 'gptr'}}
53 f(); // expected-note 2 {{}}
54 long *lp
= ::new (gptr
) long; // expected-warning{{Storage provided to placement new is only 2 bytes, whereas the allocated type requires 8 bytes}} expected-note 1 {{}}
57 } // namespace testGlobalPtrAsPlace
59 namespace testRvalue
{
65 long *lp
= ::new (f()) long; // expected-warning{{Storage provided to placement new is only 2 bytes, whereas the allocated type requires 8 bytes}} expected-note 1 {{}}
68 } // namespace testRvalue
70 namespace testNoWarning
{
73 long *lp
= ::new (f()) long;
76 } // namespace testNoWarning
78 namespace testPtrToArrayAsPlace
{
80 //char *st = new char [8];
81 char buf
[3]; // expected-note {{'buf' initialized here}}
82 void *st
= buf
; // expected-note {{'st' initialized here}}
83 long *lp
= ::new (st
) long; // expected-warning{{Storage provided to placement new is only 3 bytes, whereas the allocated type requires 8 bytes}} expected-note 1 {{}}
86 } // namespace testPtrToArrayAsPlace
88 namespace testPtrToArrayWithOffsetAsPlace
{
90 int buf
[3]; // expected-note {{'buf' initialized here}}
91 long *lp
= ::new (buf
+ 2) long; // expected-warning{{Storage provided to placement new is only 4 bytes, whereas the allocated type requires 8 bytes}} expected-note 1 {{}}
94 } // namespace testPtrToArrayWithOffsetAsPlace
96 namespace testZeroSize
{
98 int buf
[3]; // expected-note {{'buf' initialized here}}
99 long *lp
= ::new (buf
+ 3) long; // expected-warning{{Storage provided to placement new is only 0 bytes, whereas the allocated type requires 8 bytes}} expected-note 1 {{}}
102 } // namespace testZeroSize
104 namespace testNegativeSize
{
106 int buf
[3]; // expected-note {{'buf' initialized here}}
107 long *lp
= ::new (buf
+ 4) long; // expected-warning{{Storage provided to placement new is only -4 bytes, whereas the allocated type requires 8 bytes}} expected-note 1 {{}}
110 } // namespace testNegativeSize
112 namespace testHeapAllocatedBuffer
{
114 char *buf
= new char[2]; // expected-note {{'buf' initialized here}}
115 // FIXME: The message is misleading -- we should
116 // state that a pointer to an uninitialized value
118 // expected-note@-4{{Storing uninitialized value}}
119 long *lp
= ::new (buf
) long; // expected-warning{{Storage provided to placement new is only 2 bytes, whereas the allocated type requires 8 bytes}} expected-note 1 {{}}
122 } // namespace testHeapAllocatedBuffer
124 namespace testMultiDimensionalArray
{
126 char buf
[2][3]; // expected-note {{'buf' initialized here}}
127 long *lp
= ::new (buf
) long; // expected-warning{{Storage provided to placement new is only 6 bytes, whereas the allocated type requires 8 bytes}} expected-note 1 {{}}
130 } // namespace testMultiDimensionalArray
132 namespace testMultiDimensionalArray2
{
134 char buf
[2][3]; // expected-note {{'buf' initialized here}}
135 long *lp
= ::new (buf
+ 1) long; // expected-warning{{Storage provided to placement new is only 3 bytes, whereas the allocated type requires 8 bytes}} expected-note 1 {{}}
138 } // namespace testMultiDimensionalArray2
140 namespace testMultiDimensionalArray3
{
142 char buf
[2][3]; // expected-note {{'buf' initialized here}}
143 long *lp
= ::new (&buf
[1][1]) long; // expected-warning{{Storage provided to placement new is only 2 bytes, whereas the allocated type requires 8 bytes}} expected-note 1 {{}}
146 } // namespace testMultiDimensionalArray3
148 namespace testHierarchy
{
152 struct Derived
: Base
{
157 Base b
; // expected-note {{'b' initialized here}}
158 Derived
*dp
= ::new (&b
) Derived
; // expected-warning{{Storage provided to placement new is only 2 bytes, whereas the allocated type requires 8 bytes}} expected-note 1 {{}}
161 } // namespace testHierarchy
163 namespace testArrayTypesAllocation
{
169 // bad (not enough space).
170 const unsigned N
= 32;
171 alignas(S
) unsigned char buffer1
[sizeof(S
) * N
]; // expected-note {{'buffer1' initialized here}}
172 ::new (buffer1
) S
[N
]; // expected-warning{{Storage provided to placement new is only 64 bytes, whereas the allocated array type requires more space for internal needs}} expected-note 1 {{}}
180 // maybe ok but we need to warn.
181 const unsigned N
= 32;
182 alignas(S
) unsigned char buffer2
[sizeof(S
) * N
+ sizeof(int)]; // expected-note {{'buffer2' initialized here}}
183 ::new (buffer2
) S
[N
]; // expected-warning{{68 bytes is possibly not enough for array allocation which requires 64 bytes. Current overhead requires the size of 4 bytes}} expected-note 1 {{}}
185 } // namespace testArrayTypesAllocation
187 namespace testStructAlign
{
191 } Xi
; // expected-note {{'Xi' initialized here}}
193 // bad (struct X is aligned to char).
194 ::new (&Xi
.a
) long; // expected-warning{{Storage type is aligned to 1 bytes but allocated type is aligned to 8 bytes}} expected-note 1 {{}}
204 // ok (struct X is aligned to long).
213 } Xi
; // expected-note {{'Xi' initialized here}}
215 // bad (struct X is aligned to long but field 'b' is aligned to 1 because of its offset)
216 ::new (&Xi
.b
) long; // expected-warning{{Storage type is aligned to 1 bytes but allocated type is aligned to 8 bytes}} expected-note 1 {{}}
222 struct alignas(alignof(short)) Y
{
227 } Xi
; // expected-note {{'Xi' initialized here}}
229 // bad. 'b' is aligned to short
230 ::new (&Xi
.y
.b
) long; // expected-warning{{Storage type is aligned to 2 bytes but allocated type is aligned to 8 bytes}} expected-note 1 {{}}
234 short b
[10]; // expected-note {{'b' initialized here}}
236 ::new (&b
) long; // expected-warning{{Storage type is aligned to 2 bytes but allocated type is aligned to 8 bytes}} expected-note 1 {{}}
240 short b
[10]; // expected-note {{'b' initialized here}}
242 // bad (same as previous but checks ElementRegion case)
243 ::new (&b
[0]) long; // expected-warning{{Storage type is aligned to 2 bytes but allocated type is aligned to 8 bytes}} expected-note 1 {{}}
247 alignas(alignof(long)) short b
[10];
249 // ok. aligned to long(ok). offset 4*2(ok)
254 alignas(alignof(long)) short b
[10]; // expected-note {{'b' initialized here}}
256 // ok. aligned to long(ok). offset 3*2(ok)
257 ::new (&b
[3]) long; // expected-warning{{Storage type is aligned to 6 bytes but allocated type is aligned to 8 bytes}} expected-note 1 {{}}
263 alignas(alignof(long)) char b
[20];
264 } Xi
; // expected-note {{'Xi' initialized here}}
266 // ok. aligned to long(ok). offset 8*1(ok)
267 ::new (&Xi
.b
[8]) long;
269 // bad. aligned to long(ok). offset 1*1(ok)
270 ::new (&Xi
.b
[1]) long; // expected-warning{{Storage type is aligned to 1 bytes but allocated type is aligned to 8 bytes}} expected-note 1 {{}}
277 } Xi
; // expected-note {{'Xi' initialized here}}
279 // bad (struct X is aligned to 2).
280 ::new (&Xi
.a
) long; // expected-warning{{Storage type is aligned to 2 bytes but allocated type is aligned to 8 bytes}} expected-note 1 {{}}
292 // ok (struct X is aligned to long).
297 struct alignas(alignof(long)) X
{
302 // ok (struct X is aligned to long).
313 } Xi
; // expected-note {{'Xi' initialized here}}
315 // bad. X,A are aligned to 'char'
316 ::new (&Xi
.b
[0].a
) long; // expected-warning{{Storage type is aligned to 1 bytes but allocated type is aligned to 8 bytes}} expected-note 1 {{}}
324 struct alignas(alignof(long)) X
{
328 // ok. X is aligned to 'long' and field 'a' goes with zero offset
329 ::new (&Xi
.b
[0].a
) long;
333 struct alignas(alignof(long)) Y
{
341 // ok. X is aligned to 'long' because it contains struct 'Y' which is aligned to 'long'
342 ::new (&Xi
.b
[0].a
) long;
346 struct alignas(alignof(long)) Y
{
353 } Xi
; // expected-note {{'Xi' initialized here}}
355 // bad. aligned to long(ok). offset 1(bad)
356 ::new (&Xi
.b
[0].a
) long; // expected-warning{{Storage type is aligned to 1 bytes but allocated type is aligned to 8 bytes}} expected-note 1 {{}}
360 struct alignas(alignof(long)) Y
{
369 // ok. aligned to long(ok). offset 1+7*1(ok)
370 ::new (&Xi
.b
[0].a
[7]) long;
376 alignas(alignof(long)) char a
[10];
381 } Xi
; // expected-note {{'Xi' initialized here}}
383 // ok. aligned to long(ok). offset 8*1(ok)
384 ::new (&Xi
.b
[0].a
[8]) long;
386 // bad. aligned to long(ok). offset 1(bad)
387 ::new (&Xi
.b
[0].a
[1]) long; // expected-warning{{Storage type is aligned to 1 bytes but allocated type is aligned to 8 bytes}} expected-note 1 {{}}
403 } Xi
; // expected-note {{'Xi' initialized here}}
405 // bad. all structures X,Y,Z are aligned to char
406 ::new (&Xi
.a
[1].b
[1].c
) long; // expected-warning{{Storage type is aligned to 1 bytes but allocated type is aligned to 8 bytes}} expected-note 1 {{}}
412 alignas(alignof(long)) char c
[10];
424 // ok. field 'c' is aligned to 'long'
425 ::new (&Xi
.a
[1].b
[1].c
) long;
439 struct alignas(alignof(long)) X
{
441 } Xi
; // expected-note {{'Xi' initialized here}}
443 // ok. aligned to long(ok). offset 1+7*1(ok)
444 ::new (&Xi
.a
[0].b
[0].c
[7]) long; // expected-warning{{Storage type is aligned to 1 bytes but allocated type is aligned to 8 bytes}} expected-note 1 {{}}
448 struct alignas(alignof(long)) Y
{
455 } Xi
; // expected-note {{'Xi' initialized here}}
457 // ok. aligned to long(ok). offset ok. 1(field 'a' offset) + 0*10(index '0' * first dimension size '10') + 7*1(index '7')
458 ::new (&Xi
.b
[0].a
[0][7]) long;
460 // ok. aligned to long(ok). offset ok. 1(field 'a' offset) + 1*10(index '1' * first dimension size '10') + 5*1(index '5')
461 ::new (&Xi
.b
[0].a
[1][5]) long;
463 // bad. aligned to long(ok). offset ok. 1(field 'a' offset) + 1*10(index '1' * first dimension size '10') + 6*1(index '5')
464 ::new (&Xi
.b
[0].a
[1][6]) long; // expected-warning{{Storage type is aligned to 1 bytes but allocated type is aligned to 8 bytes}} expected-note 1 {{}}
467 } // namespace testStructAlign