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

            Bug ID: 93644
           Summary: -Wreturn-local-addr July regression: new
                    false-positive warning
           Product: gcc
           Version: 10.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: other
          Assignee: unassigned at gcc dot gnu.org
          Reporter: jim at meyering dot net
  Target Milestone: ---

Created attachment 47808
  --> https://gcc.gnu.org/bugzilla/attachment.cgi?id=47808&action=edit
derived from gnulib's lib/careadlinkat.c

Using -Wreturn-local-addr can now evoke this false positive:

$ gcc -c -O2 -Wreturn-local-addr k.c
k.c: In function 'careadlinkat':
cc1: warning: function may return address of local variable
[-Wreturn-local-addr]
k.c:24:8: note: declared here
   24 |   char stack_buf[1024];
      |        ^~~~~~~~~

I was able to easily narrow down that this behavior changed on gcc/git master
built from:
2019-07-19 (no warning) to
2019-07-23 (warning emitted).
--

I was surprised not to be able to convince the compiler that the code is ok OR
even to suppress the warning.
Per discussion in the thread at
https://lists.gnu.org/r/coreutils/2020-02/msg00006.html, I first tried this:

>> Would an `assure (buf != stack_buf)` before the `return buf`
>> indicate that constraint to gcc with minimal runtime overhead?

I then tried to suppress that warning in the affected file by adding these
lines:

/* Without this pragma, gcc 10.0.1 20200205 reports that
   the "function may return address of local variable".  */
# pragma GCC diagnostic ignored "-Wreturn-local-addr"

But, surprisingly, even with that pragma, the warning was still emitted.
I ended up having to disable this normally-high-signal warning for all of
coreutils/lib.

The attached file is derived from gnulib's lib/careadlinkat.c.

Reply via email to