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

            Bug ID: 84656
           Summary: -Wclass-memaccess spuriously warns in code paths that
                    cannot access nontrivial types
           Product: gcc
           Version: 8.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: evan at eklitzke dot org
  Target Milestone: ---

$ gcc --version
gcc (GCC) 8.0.1 20180218 (Red Hat 8.0.1-0.14)
Copyright (C) 2018 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.


$ cat bug.cc 
#include <cstring>
#include <type_traits>

struct nontrivial_t {
  int x;
  nontrivial_t() :x(1) {}
};
static_assert(!std::is_trivial<nontrivial_t>::value);

template <typename T>
void magic(T *val) {
  if (std::is_trivial<T>::value) {
    ::memset(val, 0, sizeof(T));
  } else {
    *val = T();
  }
}

void bug() {
  nontrivial_t b;
  magic<nontrivial_t>(&b);
}


$ gcc -Wclass-memaccess -c bug.cc 
bug.cc: In instantiation of ‘void magic(T*) [with T = nontrivial_t]’:
bug.cc:21:25:   required from here
bug.cc:13:13: warning: ‘void* memset(void*, int, size_t)’ clearing an object of
non-trivial type ‘struct nontrivial_t’; use assignment or value-initialization
instead [-Wclass-memaccess]
     ::memset(val, 0, sizeof(T));
     ~~~~~~~~^~~~~~~~~~~~~~~~~~~
bug.cc:4:8: note: ‘struct nontrivial_t’ declared here
 struct nontrivial_t {
        ^~~~~~~~~~~~

Reply via email to