This is the mail archive of the
gcc@gcc.gnu.org
mailing list for the GCC project.
Re: Warning on move and dereference of unique_ptr in the same expression
- From: Marek Polacek <polacek at redhat dot com>
- To: Allan Sandfeld Jensen <linux at carewolf dot com>
- Cc: GCC Development <gcc at gcc dot gnu dot org>
- Date: Mon, 3 Feb 2020 15:47:13 -0500
- Subject: Re: Warning on move and dereference of unique_ptr in the same expression
- References: <4534615.31r3eYUQgx@twilight>
On Mon, Feb 03, 2020 at 09:26:40PM +0100, Allan Sandfeld Jensen wrote:
> Hello gcc
>
> I have now twice hit obscure bugs in Chromium that crashed on some compilers
> but not on others, and didn't produce any warnings on any compiler. I would
> like to know if this code is as undefined as I think it is, and if it would
> make sense to have gcc warn about it.
>
> Both cases basically has this form:
>
> std::unique_ptr<A> a;
>
> a->b->callMethod(something, bind(callback, std::move(a)));
>
> This crashed with MSVC and gcc 5, but not with newer gcc or with clang.
You mean the application itself, not the compiler, presumably.
> When it crashes it is because the arguments and the move therein have been
> evaluated before a->b is resolved.
>
> I assume this is undefined behavior? So why isn't the warning for using and
> modifying in the same expression triggered?
This should be defined in C++17, with P0145 in particular:
http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2016/p0145r3.pdf
which says that the expression that names the function is sequenced before every
argument expression and every default argument.
Marek