[PATCH] Add testcase for C++ placement new and the important difference between dtor clobbers and end of scope clobbers

Jakub Jelinek jakub@redhat.com
Mon Jan 28 13:52:00 GMT 2019


On Mon, Jan 28, 2019 at 01:55:38PM +0100, Richard Biener wrote:
> So I guess in the end we're being lucky.  Somehow.  I've played with
> 
> __attribute__((noipa)) void
> qux ()
> {
>   S buf1;
>   foo ((char *)&buf1);
>   S *p = new (&buf1) (S);
>   bar (p);
>   p->~S ();
>   {
>     char buf2[128];
>     baz (buf2);
>   }
> }

I'd think the above is already invalid, by doing a placement new into
a variable with non-trivial ctor and dtor while it is still constructed,
then destruct the placement new created var in there and after a while
destruct the original variable doesn't feel right to me, but I'm not a C++
language lawyer.  I'd expect that usually either the whole var has
char/std::byte etc. array type, or the placement new is into a field inside
of some class (again char/std::byte etc. array type).
Would could be valid is:
__attribute__((noipa)) void
qux ()
{
  S buf1;
  foo (buf1.buf);
  S *p = new (buf1.buf) (S);
  bar (p);
  p->~S ();
  {
    char buf2[128];
    baz (buf2);
  }
}

but we don't really fold that into VAR_DECL lhs either, the type in the
MEM_REF isn't really S even in this case, but CLASSTYPE_AS_BASE of the S
type.

	Jakub



More information about the Gcc-patches mailing list