Discussion:
gold won't build with current gcc
Alan Modra
2018-12-05 01:46:31 UTC
Permalink
.../symtab.h: In constructor 'gold::Symbol::Symbol()':
.../symtab.h:917:33: error: 'void* memset(void*, int, size_t)' clearing an object of type 'class gold::Symbol' with no trivial copy-assignment; use assignment or value-initialization instead [-Werror=class-memaccess]
917 | { memset(this, 0, sizeof *this); }

From gcc/doc/invoke.texi:

@item -Wclass-memaccess @r{(C++ and Objective-C++ only)}
Warn when the destination of a call to a raw memory function such as
@code{memset} or @code{memcpy} is an object of class type, and when writing
into such an object might bypass the class non-trivial or deleted constructor
or copy assignment, violate const-correctness or encapsulation, or corrupt
virtual table pointers. Modifying the representation of such objects may
violate invariants maintained by member functions of the class. For example,
the call to @code{memset} below is undefined because it modifies a non-trivial
class object and is, therefore, diagnosed. The safe way to either initialize
or clear the storage of objects of such types is by using the appropriate
constructor or assignment operator, if one is available.
@smallexample
std::string str = "abc";
memset (&str, 0, sizeof str);
@end smallexample
The @option{-Wclass-memaccess} option is enabled by @option{-Wall}.
Explicitly casting the pointer to the class object to @code{void *} or
to a type that can be safely accessed by the raw memory function suppresses
the warning.

So we can fix this as follows. OK?

* symtab.h (Symbol::Symbol): Avoid -Wclass-memaccess warning.

diff --git a/gold/symtab.h b/gold/symtab.h
index 089e156b45..16a244855d 100644
--- a/gold/symtab.h
+++ b/gold/symtab.h
@@ -914,7 +914,7 @@ class Symbol
// Instances of this class should always be created at a specific
// size.
Symbol()
- { memset(this, 0, sizeof *this); }
+ { memset(static_cast<void*>(this), 0, sizeof *this); }

// Initialize the general fields.
void
--
Alan Modra
Australia Development Lab, IBM
Cary Coutant
2018-12-05 02:16:03 UTC
Permalink
Post by Alan Modra
So we can fix this as follows. OK?
* symtab.h (Symbol::Symbol): Avoid -Wclass-memaccess warning.
diff --git a/gold/symtab.h b/gold/symtab.h
index 089e156b45..16a244855d 100644
--- a/gold/symtab.h
+++ b/gold/symtab.h
@@ -914,7 +914,7 @@ class Symbol
// Instances of this class should always be created at a specific
// size.
Symbol()
- { memset(this, 0, sizeof *this); }
+ { memset(static_cast<void*>(this), 0, sizeof *this); }
Sure, looks good to me.

I wonder if we can just get rid of the memset altogether -- it seems
to be there as a guard against carelessness and random garbage in
unused variant fields, as the various Symbol::init_xxx and
Sized_symbol::init_xxx methods ought to initialize everything
explicitly, and we just end up setting most fields to 0 twice.
Actually, it's probably more efficient to keep the memset and remove
all the explicit assignments that set fields to 0 or false. It's just
not very C++-ish.

-cary

Loading...