This is the mail archive of the
mailing list for the GCC project.
Re: Warn when returning the address of a temporary (middle-end) v2
- From: Jeff Law <law at redhat dot com>
- To: Marc Glisse <marc dot glisse at inria dot fr>, gcc-patches at gcc dot gnu dot org
- Date: Wed, 16 Jul 2014 21:45:16 -0600
- Subject: Re: Warn when returning the address of a temporary (middle-end) v2
- Authentication-results: sourceware.org; auth=none
- References: <alpine dot DEB dot 2 dot 02 dot 1406221930380 dot 20514 at stedding dot saclay dot inria dot fr>
On 06/22/14 12:20, Marc Glisse wrote:
I note you don't catch return &localvar in the isolation code -- it
looks like you just catch those which potentially flow from PHIs.
I followed the advice in this discussion:
and here is a new patch. I made an effort to isolate a path in at least
one subcase so it doesn't look too strange that the warning is in this
file. Computing the dominance info just to tweak the warning message may
be a bit excessive. I kept the same option as the front-ends, I don't
know if we want a different one, or maybe a Wmaybe-... version. There
will be cases where we get a duplicate warning from -Wtarget-lifetime in
fortran, but none in the testsuite, and I would rather have 2 warnings
than miss such broken code. The uninit-G testcase is about
initialization, not returning, so I am changing that, even if it is
unnecessary with the current version of the patch (only activated at -O2).
Bootstrap+testsuite (--enable-languages=all,obj-c++,ada,go) on
(by the way, contrib/compare_tests is confused when I use all languages,
it prints "comm: file 1 is not in sorted order" and tons of spurious
2014-06-23 Marc Glisse <firstname.lastname@example.org>
* c-typeck.c (c_finish_return): Return 0 instead of the address of
a local variable.
* typeck.c (maybe_warn_about_returning_address_of_local): Return
whether it is returning the address of a local variable.
(check_return_expr): Return 0 instead of the address of a local
* c.opt (-Wreturn-local-addr): Move to common.opt.
* common.opt (-Wreturn-local-addr): Moved from c.opt.
* gimple-ssa-isolate-paths.c: Include diagnostic-core.h.
(isolate_path): New argument to avoid inserting a trap.
(find_implicit_erroneous_behaviour): Handle returning the address
of a local variable.
(gimple_ssa_isolate_erroneous_paths): Calculate dominance info.
* c-c++-common/addrtmp.c: New file.
* c-c++-common/uninit-G.c: Adapt.
I realize you're primarily catching that in the front-ends, but can't we
have cases which aren't caught by the front end, but after optimizations
we're able to propagate &somelocal into the return statement?
It generally looks good and I'm ready to approve if the answer to the
above question is "can't happen". If it can happen, then we ought to
handle it in the isolation code as well (ought to be relatively easy).