This is the mail archive of the
gdb-patches@sourceware.org
mailing list for the GDB project.
Re: Oh dear. I regret to inform you that commit 'RAII-fy make_cleanup_restore_current_thread & friends' might be unfortunate
- From: Pedro Alves <palves at redhat dot com>
- To: Simon Marchi <simon dot marchi at polymtl dot ca>
- Cc: gdb-patches at sourceware dot org
- Date: Thu, 4 May 2017 22:49:02 +0100
- Subject: Re: Oh dear. I regret to inform you that commit 'RAII-fy make_cleanup_restore_current_thread & friends' might be unfortunate
- Authentication-results: sourceware.org; auth=none
- Authentication-results: ext-mx04.extmail.prod.ext.phx2.redhat.com; dmarc=none (p=none dis=none) header.from=redhat.com
- Authentication-results: ext-mx04.extmail.prod.ext.phx2.redhat.com; spf=pass smtp.mailfrom=palves at redhat dot com
- Dkim-filter: OpenDKIM Filter v2.11.0 mx1.redhat.com 08F038048E
- Dmarc-filter: OpenDMARC Filter v1.3.2 mx1.redhat.com 08F038048E
- References: <E1d6JGc-0000zB-OE@kwanyin.sergiodj.net> <34792dd0-088c-a1d1-9125-70c8585c21bd@redhat.com> <6bf88edee0fb17451d44b85bb00fb0d0@polymtl.ca> <79511435-a3a8-48f9-2e16-bca8adb1909d@redhat.com> <1fd4bf31-b0cd-3746-36d0-5f9f9e9dce4c@redhat.com>
On 05/04/2017 07:28 PM, Pedro Alves wrote:
> On 05/04/2017 07:22 PM, Pedro Alves wrote:
>
>> AFAICS so far, this is a false positive.
>>
>> Not sure what to do. I wouldn't want to force-memset
>> the optional's storage to work around it, which would be
>> a pessimization to quiet a warning. From above, we see that
>> that wouldn't work when we later start using std::optional.
>>
>> There's a bug open about this (for boost::optional, but most
>> probably the exact same):
>> https://gcc.gnu.org/bugzilla/show_bug.cgi?id=78044
>
> I've tried the reproducer there with the obvious change to use
> std::optional and that does not warn. So this is a different,
> though related issue.
>
> Or maybe there's really something wrong with the gdb code
> that is escaping me.
OK, here's smallest, self-contained reproducer I managed to
come up with:
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//#include <optional>
#include <new>
template<typename T>
struct optional
{
optional () : m_dummy () {}
~optional () { m_item.~T (); }
void emplace () { new (&m_item) T (); }
union
{
int m_dummy;
T m_item;
};
};
template <typename T>
using Optional = optional<T>; // warns
//using Optional = std::optional<T>; // warns too
extern int get ();
extern void set (int);
struct A
{
A () : m (get ()) {}
~A () { set (m); }
int m;
};
struct B
{
B ();
~B ();
};
void func ()
{
Optional<A> maybe_a;
Optional<B> maybe_b;
maybe_a.emplace ();
maybe_b.emplace ();
}
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
$ /opt/gcc/bin/g++ optional.cc -g3 -O2 -Wmaybe-uninitialized -std=gnu++17 -c
optional.cc: In function ‘void func()’:
optional.cc:28:15: warning:
‘maybe_a.optional<A>::<anonymous>.optional<A>::<unnamed union>::m_dummy’
may be used uninitialized in this function [-Wmaybe-uninitialized]
~A () { set (m); }
~~~~^~~
optional.cc:41:15: note:
‘maybe_a.optional<A>::<anonymous>.optional<A>::<unnamed union>::m_dummy’
was declared here
Optional<A> maybe_a;
^~~~~~~
Looks like a compiler bug to me.
Thanks,
Pedro Alves