public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c/110267] New: Bogus warning "function may return address of local variable"
@ 2023-06-15 15:39 nullplanwichmann at web dot de
  2023-06-15 15:49 ` [Bug c/110267] " pinskia at gcc dot gnu.org
  0 siblings, 1 reply; 2+ messages in thread
From: nullplanwichmann at web dot de @ 2023-06-15 15:39 UTC (permalink / raw)
  To: gcc-bugs

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

            Bug ID: 110267
           Summary: Bogus warning "function may return address of local
                    variable"
           Product: gcc
           Version: 12.2.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c
          Assignee: unassigned at gcc dot gnu.org
          Reporter: nullplanwichmann at web dot de
  Target Milestone: ---
              Host: x86_64-pc-linux-gnu
            Target: x86_64-pc-linux-gnu
             Build: x86_64-pc-linux-gnu

Minimal test case:
extern char *strdup(const char *);
extern int fill(char *);
char *do_something(char *buf)
{
  char tmp[10];
  if (!buf)
    buf = tmp;
  int rv = fill(buf);
  if (rv < 0) return 0;
  if (buf[0] != '/')
    return 0;
  return buf == tmp? strdup(buf) : buf;
}

Compile with -O2 -Wall. All GCC versions I have tried (10.2.1-6 from an old
Debian installation, 12.2.0 from current Debian stable, and a development
snapshot I compiled from git this morning) warn:

tmp.c: In function ‘do_something’:
tmp.c:13:1: warning: function may return address of local variable
[-Wreturn-local-addr]
   13 | }
      | ^
tmp.c:5:8: note: declared here
    5 |   char tmp[10];


The warning is of course wrong: Whenever buf == tmp, the function returns the
strdup() of tmp, not tmp itself. I was resigned to assume that gcc cannot
identify that this is the case, but I noticed that if the if-selection in lines
10-11 is removed, then the warning disappears. The warning only occurs when
optimizing at level s, 2, or 3.

Additionally, the old gcc version 10.2.1-6 mangled the warning output, it would
write out:

cc1: warning: function may return address of local variable
[-Wreturn-local-addr]
tmp.c:5:8: note: declared here
    5 |   char tmp[10];

As if that was some internal warning. That seems to be fixed in the newer
versions, though.

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

* [Bug c/110267] Bogus warning "function may return address of local variable"
  2023-06-15 15:39 [Bug c/110267] New: Bogus warning "function may return address of local variable" nullplanwichmann at web dot de
@ 2023-06-15 15:49 ` pinskia at gcc dot gnu.org
  0 siblings, 0 replies; 2+ messages in thread
From: pinskia at gcc dot gnu.org @ 2023-06-15 15:49 UTC (permalink / raw)
  To: gcc-bugs

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

Andrew Pinski <pinskia at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|UNCONFIRMED                 |RESOLVED
         Resolution|---                         |DUPLICATE

--- Comment #1 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
Dup of bug 93644.

*** This bug has been marked as a duplicate of bug 93644 ***

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

end of thread, other threads:[~2023-06-15 15:49 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-06-15 15:39 [Bug c/110267] New: Bogus warning "function may return address of local variable" nullplanwichmann at web dot de
2023-06-15 15:49 ` [Bug c/110267] " pinskia 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).