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

Martin Sebor <msebor at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
           Keywords|                            |diagnostic
             Status|UNCONFIRMED                 |NEW
   Last reconfirmed|                            |2018-03-13
                 CC|                            |msebor at gcc dot gnu.org
            Summary|new GCC version prints      |-Wclass-memaccess on a
                   |warning on memcpy call (no  |memcpy in a copy assignment
                   |nontrivial bases or         |operator with no nontrivial
                   |members)                    |bases or members
     Ever confirmed|0                           |1

--- Comment #1 from Martin Sebor <msebor at gcc dot gnu.org> ---
The warning is designed to allow raw memory access in ctors and dtors of
non-trivial classes with no non-trivial bases or members, but the exemption
doesn't extend to assignment operators of such classes.  Some such types
(including squid's TrafficMode class) use memcpy in the special functions as a
shortcut to copy multiple trivial members without having to explicitly
enumerate them.  Since this is safe it makes sense to exempt the copy
assignment operator as well.  Confirmed with the test case below.

$ cat pr84850.C && gcc -S  -Wall pr84850.C
struct S
{
  bool a, b;
  S ();
  S (const S&);
  ~S ();
  void operator= (const S&);
};

S::S ()
{
  __builtin_memset (this, 0, sizeof *this);
}

S::S (const S &s)
{
  __builtin_memcpy (this, &s, sizeof s);
}

S::~S ()
{
  __builtin_memset (this, 0, sizeof *this);
}

void S::operator= (const S &s)
{
  __builtin_memcpy (this, &s, sizeof s);
}

pr84850.C: In member function ‘void S::operator=(const S&)’:
pr84850.C:27:39: warning: ‘void* __builtin_memcpy(void*, const void*, long
unsigned int)’ writing to an object of type ‘struct S’ with no trivial
copy-assignment; use copy-assignment or copy-initialization instead
[-Wclass-memaccess]
   __builtin_memcpy (this, &s, sizeof s);
                                       ^
pr84850.C:1:8: note: ‘struct S’ declared here
 struct S
        ^

Reply via email to