https://gcc.gnu.org/bugzilla/show_bug.cgi?id=84851

            Bug ID: 84851
           Summary: missing -Wclass-memaccess for a memcpy in a copy ctor
                    with a non-trivial member
           Product: gcc
           Version: 8.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: msebor at gcc dot gnu.org
  Target Milestone: ---

-Wclass-memaccess excempts ctors and dtors of non-polymorphic classes from
checking for raw memory accesses, but the warning does trigger for such
accesses in classes with a virtual table to help detect virtual table
corruption.  However, the exemption is too broad and allows for the corruption
of virtual tables or subobjects with non-trivial members.

$ cat u.C && gcc -S -Wall -Wextra u.C
struct A
{
  const int &r;

  A ();

  A (const A&);

  virtual ~A ();
};

struct B: A
{
  B (const B&);
};

B::B (const B &b)
{
  __builtin_memcpy (this, &b, sizeof b);   // -Wclass-memaccess (good)
}

struct C
{
  A a;

  C (const C&);
};

C::C (const C &c)
{
  __builtin_memcpy (this, &c, sizeof c);   // missing -Wclass-memaccess
}
u.C: In copy constructor ‘B::B(const B&)’:
u.C:17:1: warning: base class ‘struct A’ should be explicitly initialized in
the copy constructor [-Wextra]
 B::B (const B &b)
 ^
u.C:19:39: warning: ‘void* __builtin_memcpy(void*, const void*, long unsigned
int)’ writing to an object of type ‘struct B’ with no trivial copy-assignment;
use copy-initialization instead [-Wclass-memaccess]
   __builtin_memcpy (this, &b, sizeof b);   // -Wclass-memaccess (good)
                                       ^
u.C:12:8: note: ‘struct B’ declared here
 struct B: A
        ^

Reply via email to