V752. Creating an object with placement new requires a buffer of large size.
The analyzer detected an attempt to create an object using 'placement new' while the size of the allocated storage is not large enough to store this object. This issue will result in using additional memory outside the allocated block and may cause a crash or incorrect program behavior.
Consider the following example:
struct T { float x, y, z, q; };
char buf[12];
T *p = new (buf) T;
In this code, the programmer is trying to store an object of size 16 bytes in the 'buf' buffer of size 12 bytes. When using this object, the memory outside the buffer bounds will be changed. The result of such change is unpredictable.
To fix this error, we need to adjust the buffer size or make sure that the offset from the beginning of the buffer is specified correctly.
Fixed code:
struct T { float x, y, z, q; };
char buf[sizeof(T)];
T *p = new (buf) T;
This diagnostic is classified as:
|