Bug 94312 - missing -Wreturn-local-addr on returning local address via memchr or memset
Summary: missing -Wreturn-local-addr on returning local address via memchr or memset
Alias: None
Product: gcc
Classification: Unclassified
Component: middle-end (show other bugs)
Version: 10.0
: P3 normal
Target Milestone: ---
Assignee: Not yet assigned to anyone
Keywords: diagnostic
Depends on:
Blocks: Wreturn-local-addr
  Show dependency treegraph
Reported: 2020-03-24 20:20 UTC by Martin Sebor
Modified: 2020-03-24 20:34 UTC (History)
0 users

See Also:
Known to work:
Known to fail:
Last reconfirmed:


Note You need to log in before you can comment on or make changes to this bug.
Description Martin Sebor 2020-03-24 20:20:50 UTC
-Wreturn-local-addr diagnoses only the first function below.  It misses the same problem in the second function.  The root cause is a missing label for BUILT_IN_MEMCHR in is_addr_local in gimple-ssa-isolate-paths.c (it was accidentally omitted in r273261).

$ cat x.c && gcc -O2 -S -Wall x.c
void* f (char x)
  char a[] = { x, x + 1 };
  return __builtin_strchr (a, 0);   // -Wreturn-local-addr (okay)

void* g (char x)
  char a[] = { x, x + 1 };
  return __builtin_memchr (a, 0, sizeof a);   // missing -Wreturn-local-addr
x.c: In function ‘f’:
x.c:4:10: warning: function returns address of local variable [-Wreturn-local-addr]
    4 |   return __builtin_strchr (a, 0);   // -Wreturn-local-addr (okay)
      |          ^~~~~~~~~~~~~~~~~~~~~~~
x.c:3:8: note: declared here
    3 |   char a[] = { x, x + 1 };
      |        ^
Comment 1 Martin Sebor 2020-03-24 20:34:45 UTC
Ditto for memset and strpbrk.  The warning should be reviewed for coverage.  Rather than handling just built-ins a more general and more broadly usable solution is to add a new function attribute to specify that a function returns either the same value as one of its arguments (unchanged, such as memset), or a pointer into the same array as one of its arguments (such as memchr).