[Bug c++/49974] missing -Wreturn-local-addr for indirectly returning reference to local/temporary
msebor at gcc dot gnu.org
gcc-bugzilla@gcc.gnu.org
Sun Jan 16 00:37:45 GMT 2022
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=49974
--- Comment #18 from Martin Sebor <msebor at gcc dot gnu.org> ---
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: ‘<anonymous>.X::i’ is used uninitialized
[-Wuninitialized]
11 | return x.i;
| ^
pr49974-c17.C:10:15: note: ‘<anonymous>’ declared here
10 | X&& x = f(X{});
| ^
More information about the Gcc-bugs
mailing list