diff options
author | Pedro Alves <palves@redhat.com> | 2017-04-04 23:49:27 +0100 |
---|---|---|
committer | Pedro Alves <palves@redhat.com> | 2017-04-05 01:28:06 +0100 |
commit | c053b65441eb70ac78a514fabc3431b857a30d2e (patch) | |
tree | 82f631a488fd347023fcc549d2e7c08068f43110 /gdb/common | |
parent | f3b8f8ee35bd62a2ae7d21561f5d23f00dad004f (diff) | |
download | gdb-c053b65441eb70ac78a514fabc3431b857a30d2e.zip gdb-c053b65441eb70ac78a514fabc3431b857a30d2e.tar.gz gdb-c053b65441eb70ac78a514fabc3431b857a30d2e.tar.bz2 |
Initialize gdb::optional empty payload to quiet false -Wmaybe-uninitialized warnings
Commit ecfb656c37b982 ("dwarf2read.c: Make dir_index and
file_name_index strong typedefs") added a use of gdb::optional that
triggers bogus -Wmaybe-uninitialized warnings:
GCC trunk is complaining like this:
../../binutils-gdb/gdb/dwarf2read.c: In function void read_formatted_entries(bfd*, const gdb_byte**, line_header*, const comp_unit_head*, void (*)(line_header*, const char*, dir_index, unsigned int, unsigned int)):
../../binutils-gdb/gdb/dwarf2read.c:17779:65: error: fe.file_entry::length may be used uninitialized in this function [-Werror=maybe-uninitialized]
callback (lh, fe.name, fe.d_index, fe.mod_time, fe.length);
^
../../binutils-gdb/gdb/dwarf2read.c:17779:65: error: *((void*)& fe +8) may be used uninitialized in this function [-Werror=maybe-uninitialized]
../../binutils-gdb/gdb/dwarf2read.c:17779:65: error: fe.file_entry::mod_time may be used uninitialized in this function [-Werror=maybe-uninitialized]
../../binutils-gdb/gdb/dwarf2read.c:17779:65: error: fe.file_entry::name may be used uninitialized in this function [-Werror=maybe-uninitialized]
While some older GCCs are complaining like this:
../../binutils-gdb/gdb/dwarf2read.c: In function void read_formatted_entries(bfd*, const gdb_byte**, line_header*, const comp_unit_head*, void (*)(line_header*, const char*, dir_index, unsigned int, unsigned int)):
../../binutils-gdb/gdb/dwarf2read.c:17779:65: error: uint may be used uninitialized in this function [-Werror=maybe-uninitialized]
callback (lh, fe.name, fe.d_index, fe.mod_time, fe.length);
Looking around the web, I see that boost's optional implementation
triggers this kind of issue often too. See:
http://www.boost.org/doc/libs/1_63_0/libs/optional/doc/html/boost_optional/tutorial/gotchas/false_positive_with__wmaybe_uninitialized.html
I noticed that replacing the gdb::optional uses with real C++17
std::optional uses against GCC 7/trunk makes the warnings go away.
Looking at the implementation, AFAICS, libstdc++ always initializes
its "empty" union payload member (_M_empty, which is defined as an
empty class, like ours). I.e., all payload types have this ctor:
struct _Optional_payload.....
{
constexpr _Optional_payload()
: _M_empty() {}
The constexpr makes a diference too. Without it, GCC7 still warns.
So I'm applying the same treatment to our gdb::optional.
gdb/ChangeLog:
2017-04-05 Pedro Alves <palves@redhat.com>
* common/gdb_optional.h (optional::optional): Make constexpr and
initialize m_dummy.
Diffstat (limited to 'gdb/common')
-rw-r--r-- | gdb/common/gdb_optional.h | 5 |
1 files changed, 3 insertions, 2 deletions
diff --git a/gdb/common/gdb_optional.h b/gdb/common/gdb_optional.h index fef7a73..ad1119f 100644 --- a/gdb/common/gdb_optional.h +++ b/gdb/common/gdb_optional.h @@ -34,8 +34,9 @@ class optional { public: - optional () - : m_instantiated (false) + constexpr optional () + : m_dummy (), + m_instantiated (false) { } |