https://gcc.gnu.org/bugzilla/show_bug.cgi?id=99367
Bug ID: 99367 Summary: missing warning on constructing/destroying class objects in insufficient space Product: gcc Version: 11.0 Status: UNCONFIRMED Severity: normal Priority: P3 Component: middle-end Assignee: unassigned at gcc dot gnu.org Reporter: msebor at gcc dot gnu.org Target Milestone: --- Except in the most trivial cases GCC fails to diagnose invalid attempts to invoke nontrivial constructors or destructors of classes in insufficient space. They should be easily detectable in the middle end. $ cat u.C && gcc -O2 -S -Wall u.C void* operator new (__SIZE_TYPE__, void *p) { return p; } struct A { A (); ~A (); int i; }; void f0 (void*); char a[sizeof (A)]; void f1 () { A *q = new (a + 1) A (); // -Wplacement-new (good) f0 (q); } void g () { char *p = a + 1; A *q = new (p) A (); // missing warning f0 (q); } void f2 () { A *q = (A*)(a + 1); q->~A (); // missing warning } u.C: In function ‘void f1()’: u.C:11:17: warning: placement new constructing an object of type ‘A’ and size ‘4’ in a region of type ‘char [4]’ and size ‘3’ [-Wplacement-new=] 11 | A *q = new (a + 1) A (); // -Wplacement-new (good) | ~~^~~ u.C:7:6: note: at offset 1 from ‘a’ declared here 7 | char a[sizeof (A)]; | ^