public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c++/81420] When a reference is bound to a member in the base of a temporary, lifetime of the temporary is not extended
       [not found] <bug-81420-4@http.gcc.gnu.org/bugzilla/>
@ 2022-07-06 13:14 ` ion.lupascu at barclays dot com
  2023-08-11 20:35 ` jason at gcc dot gnu.org
  1 sibling, 0 replies; 2+ messages in thread
From: ion.lupascu at barclays dot com @ 2022-07-06 13:14 UTC (permalink / raw)
  To: gcc-bugs

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

Ion Lupascu <ion.lupascu at barclays dot com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |ion.lupascu at barclays dot com

--- Comment #7 from Ion Lupascu <ion.lupascu at barclays dot com> ---
Not really, The issue still persists

Check this example:
#include <iostream>
#include <limits>
#include <optional>

std::optional<double> getValue(){
    return 3.14;
}

int main(){
    const double &v = *getValue();
    std::cout<<"v:=" << v  << std::endl;
    if (v == 0.0) {
        __builtin_abort();
    }
    return 0;
}

compiled with GCC trunk (v13) and the following flags: -O3 -std=c++17 -Werror
-Wall -Wextra

returns:
Program returned: 139
v:=0

link to play: https://godbolt.org/z/fPhxqe1h3

clang behaves correctly till v 10, it extends the lifetime, in v10 it catches
with dangling-gsl check. when adding a more complex value rather than double
clang doesn't catch it but it extends the lifetime. e.g.:

#include <iostream>
#include <limits>
#include <optional>

namespace
{

struct Data{
    double v;
};

} // namespace

std::optional<Data> getValue(){
    return Data{3.14};
}

int main(){
    const double &v = getValue()->v;
    std::cout<<"v:=" << v  << std::endl;
    if (v == 0.0) {
        __builtin_abort();
    }
    return 0;
}

link to play : https://godbolt.org/z/WoeGqhn7E

where GCC catches with the following error:
<source>: In function 'int main()':
<source>:20:25: error: using a dangling pointer to an unnamed temporary
[-Werror=dangling-pointer=]
   20 |     std::cout<<"v:=" << v  << std::endl;
      |                         ^
<source>:19:31: note: unnamed temporary defined here
   19 |     const double &v = getValue()->v;
      |                       ~~~~~~~~^~
cc1plus: all warnings being treated as errors
ASM generation compiler returned: 1


GCC must first fix the temporary values lifetime when bounded by reference.

^ permalink raw reply	[flat|nested] 2+ messages in thread

* [Bug c++/81420] When a reference is bound to a member in the base of a temporary, lifetime of the temporary is not extended
       [not found] <bug-81420-4@http.gcc.gnu.org/bugzilla/>
  2022-07-06 13:14 ` [Bug c++/81420] When a reference is bound to a member in the base of a temporary, lifetime of the temporary is not extended ion.lupascu at barclays dot com
@ 2023-08-11 20:35 ` jason at gcc dot gnu.org
  1 sibling, 0 replies; 2+ messages in thread
From: jason at gcc dot gnu.org @ 2023-08-11 20:35 UTC (permalink / raw)
  To: gcc-bugs

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

Jason Merrill <jason at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|ASSIGNED                    |RESOLVED
         Resolution|---                         |FIXED
   Target Milestone|---                         |8.2

--- Comment #8 from Jason Merrill <jason at gcc dot gnu.org> ---
(In reply to Ion Lupascu from comment #7)
> Not really, The issue still persists
> 
> std::optional<double> getValue(){
...
>     const double &v = *getValue();
...
>     const double &v = getValue()->v;

Neither of these bind the reference to a prvalue; std::optional operator*
returns a reference, and operator-> returns a pointer, so in both cases we're
binding the reference to an xvalue, and GCC is correct not to extend the
temporary.

Fixed in 8.2.

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2023-08-11 20:35 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
     [not found] <bug-81420-4@http.gcc.gnu.org/bugzilla/>
2022-07-06 13:14 ` [Bug c++/81420] When a reference is bound to a member in the base of a temporary, lifetime of the temporary is not extended ion.lupascu at barclays dot com
2023-08-11 20:35 ` jason at gcc dot gnu.org

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).