[Bug c++/49974] missing -Wreturn-local-addr for indirectly returning reference to local/temporary
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=49974 --- Comment #20 from Andrew Pinski --- (In reply to Barry Revzin from comment #19) > Another example of this: > > int get(); > > int const& f() { > int const r = get(); > return r; > } > > int const& g() { > int const& r = get(); > return r; > } > > gcc trunk warns on the incorrect use in f, but it does not currently warn on > the incorrect use in g (which is the exact same bug). clang warns on both. GCC trunk does warn on g but only with optimizations turned on.
[Bug c++/49974] missing -Wreturn-local-addr for indirectly returning reference to local/temporary
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=49974 Barry Revzin changed: What|Removed |Added CC||barry.revzin at gmail dot com --- Comment #19 from Barry Revzin --- Another example of this: int get(); int const& f() { int const r = get(); return r; } int const& g() { int const& r = get(); return r; } gcc trunk warns on the incorrect use in f, but it does not currently warn on the incorrect use in g (which is the exact same bug). clang warns on both.
[Bug c++/49974] missing -Wreturn-local-addr for indirectly returning reference to local/temporary
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=49974 --- Comment #18 from Martin Sebor --- This detection is partially implemented in GCC 12 by the -Wdnagling-pointer: $ cat pr49974.C && gcc -O -S -Wall pr49974.C struct X { }; inline const X& f(const X& r) { return r; } const X& g() { X x; return f(x); // !!! } const X& h() { return f(X()); // !!! } struct Y { Y(int& i) : r(i) { } int& r; }; Y f() { int i=0; return Y(i); } pr49974.C: In function ‘const X& g()’: pr49974.C:9:15: warning: using a dangling pointer to ‘x’ [-Wdangling-pointer=] 9 | return f(x); // !!! | ^ pr49974.C:8:7: note: ‘x’ declared here 8 | X x; | ^ pr49974.C: In function ‘const X& h()’: pr49974.C:14:17: warning: using a dangling pointer to an unnamed temporary [-Wdangling-pointer=] 14 | return f(X()); // !!! | ^ pr49974.C:14:16: note: unnamed temporary defined here 14 | return f(X()); // !!! |^ For the test case in comment #17 GCC with -O1 issues: pr49974-c17.C: In function ‘int main()’: pr49974-c17.C:11:12: warning: using a dangling pointer to an unnamed temporary [-Wdangling-pointer=] 11 | return x.i; |^ pr49974-c17.C:10:15: note: unnamed temporary defined here 10 | X&& x = f(X{}); | ^ pr49974-c17.C:11:12: warning: ‘.X::i’ is used uninitialized [-Wuninitialized] 11 | return x.i; |^ pr49974-c17.C:10:15: note: ‘’ declared here 10 | X&& x = f(X{}); | ^
[Bug c++/49974] missing -Wreturn-local-addr for indirectly returning reference to local/temporary
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=49974 --- Comment #17 from Jonathan Wakely --- 95911 boils down to this: #include struct X { int i = 0; }; X&& f(X&& x) { return std::move(x); } int main() { X&& x = f(X{}); return x.i; } With recent releases we get a -Wuninitialized warning, which isn't entirely correct, but at least suggests a problem: 49974.cc: In function 'int main()': 49974.cc:10:12: warning: '.X::i' is used uninitialized [-Wuninitialized] 10 | return x.i; |^ 49974.cc:9:15: note: '' declared here 9 | X&& x = f(X{}); | ^ (the note is new in GCC 11). With -fsanitize=address we get a nice stack-use-after-scope error: ==3088273==ERROR: AddressSanitizer: stack-use-after-scope on address 0x7fff976ed520 at pc 0x00401100 bp 0x7fff976ed4f0 sp 0x7fff976ed4e8 READ of size 4 at 0x7fff976ed520 thread T0 #0 0x4010ff in main /tmp/49974.cc:10 #1 0x7fd157d181a2 in __libc_start_main ../csu/libc-start.c:308 #2 0x40116d in _start (/tmp/a.out+0x40116d)
[Bug c++/49974] missing -Wreturn-local-addr for indirectly returning reference to local/temporary
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=49974 Jonathan Wakely changed: What|Removed |Added CC||570070308 at qq dot com --- Comment #16 from Jonathan Wakely --- *** Bug 95911 has been marked as a duplicate of this bug. ***
[Bug c++/49974] missing -Wreturn-local-addr for indirectly returning reference to local/temporary
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=49974 Martin Sebor changed: What|Removed |Added CC||msebor at gcc dot gnu.org See Also||https://gcc.gnu.org/bugzill ||a/show_bug.cgi?id=90905 --- Comment #15 from Martin Sebor --- GCC 10 issues two warnings for the first test case in comment #0: pr49974.C: In function ‘const X& g()’: pr49974.C:8:15: warning: function returns address of local variable [-Wreturn-local-addr] 8 | return f(x); // !!! | ^ pr49974.C:7:7: note: declared here 7 | X x; | ^ In function ‘const X& h()’: cc1plus: warning: function returns address of local variable [-Wreturn-local-addr] pr49974.C:7:7: note: declared here The second test case is not diagnosed (due to a similar problem as pr90905). The test cases in comment #8 are also not diagnosed.
[Bug c++/49974] missing -Wreturn-local-addr for indirectly returning reference to local/temporary
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=49974 Eric Gallager changed: What|Removed |Added Summary|missing warning for |missing -Wreturn-local-addr |indirectly returning|for indirectly returning |reference to|reference to |local/temporary |local/temporary --- Comment #14 from Eric Gallager --- (In reply to Jonathan Wakely from comment #13) > Yes, I think so. The bug is returning a reference (indirectly) bound to a > local, so I don't see a reason to have a different warning. ok, updating title