Bug 71924 - missing -Wreturn-local-addr returning alloca result
Summary: missing -Wreturn-local-addr returning alloca result
Status: RESOLVED FIXED
Alias: None
Product: gcc
Classification: Unclassified
Component: c (show other bugs)
Version: 7.0
: P3 enhancement
Target Milestone: 10.0
Assignee: Martin Sebor
URL:
Keywords: diagnostic, patch
Depends on:
Blocks: Wreturn-local-addr
  Show dependency treegraph
 
Reported: 2016-07-19 00:37 UTC by Martin Sebor
Modified: 2019-07-09 04:16 UTC (History)
0 users

See Also:
Host:
Target:
Build:
Known to work:
Known to fail:
Last reconfirmed: 2016-07-19 00:00:00


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Martin Sebor 2016-07-19 00:37:19 UTC
All three functions defined in the following example return an address of a stack object, but GCC only issues a -Wreturn-local-addr warning for two of them and not for the one where alloca was used to allocated the stack object.  The warning should be issued for all three functions.

Note that the warning comes and goes with the value of the size argument, and is not issued when the argument is not constant (the warning is, however, issued for VLAs).

Also, the note printed following the second warning is wrong (the object isn't declared) and points to the wrong location (to be useful, it should point to the line containing either the declaration of the local object or the invocation of the alloca function).

$ cat walloca.c && /build/gcc-walloca/gcc/xgcc -B /build/gcc-walloca/gcc -S -Wall -Wextra -Wpedantic walloca.c
void* foo (void)
{
  char a [4];
  return a;
}

void* bar (void)
{
  void *a = __builtin_alloca (4);
  return a;
}

void* baz (void)
{
  void *a = __builtin_alloca_with_align (4, 8);
  return a;
}

walloca.c: In function ‘foo’:
walloca.c:4:10: warning: function returns address of local variable [-Wreturn-local-addr]
   return a;
          ^
walloca.c: In function ‘baz’:
walloca.c:16:10: warning: function returns address of local variable [-Wreturn-local-addr]
   return a;
          ^
walloca.c:17:1: note: declared here
 }
 ^
Comment 1 Andrew Pinski 2016-07-19 00:43:19 UTC
I expect we also miss:
void* foo1 (void)
{
  char a [4];
  char *b = a;
  return b;
}

void* foo2 (void)
{
  char *b = (char[4]){0, 0, 0, 0};
  return b;
}

Basically we don't do any flow analysis for this warning.
Comment 2 Martin Sebor 2016-07-19 02:39:40 UTC
Returning [the address of] a compound literal, or any indirectly referenced object (such as those in comment #1), suffers from the same limitation.  Compiling the program with -O2 (but not -O1) does produce the expected warnings.  Since the effect of optimization is not documented, a "fix" might be as simple as updating the manual to mention the dependency.
Comment 3 Martin Sebor 2016-11-08 00:16:47 UTC
On second thought, the right fix is to report the problem consistently for all kinds of storage, including auto variables, alloca-ted storage, and compound literals.
Comment 4 Martin Sebor 2019-05-10 16:42:43 UTC
I'm testing a patch.
Comment 5 Martin Sebor 2019-05-22 21:35:15 UTC
Patch: https://gcc.gnu.org/ml/gcc-patches/2019-05/msg01525.html
Comment 6 Martin Sebor 2019-07-09 04:16:16 UTC
Author: msebor
Date: Tue Jul  9 04:15:42 2019
New Revision: 273261

URL: https://gcc.gnu.org/viewcvs?rev=273261&root=gcc&view=rev
Log:
PR middle-end/71924 - missing -Wreturn-local-addr returning alloca result
PR middle-end/90549 - missing -Wreturn-local-addr maybe returning an address of a local array plus offset

gcc/ChangeLog:

	PR middle-end/71924
	PR middle-end/90549
	* gimple-ssa-isolate-paths.c (isolate_path): Add attribute.  Update
	comment.
	(args_loc_t): New type.
	(args_loc_t, locmap_t): same.
	(diag_returned_locals): New function.
	(is_addr_local): Same.
	(handle_return_addr_local_phi_arg, warn_return_addr_local): Same.
	(find_implicit_erroneous_behavior): Call warn_return_addr_local_phi_arg.
	(find_explicit_erroneous_behavior): Call warn_return_addr_local.

gcc/testsuite/ChangeLog:

	PR middle-end/71924
	PR middle-end/90549
	* gcc.c-torture/execute/return-addr.c: New test.
	* gcc.dg/Wreturn-local-addr-2.c: New test.
	* gcc.dg/Wreturn-local-addr-4.c: New test.
	* gcc.dg/Wreturn-local-addr-5.c: New test.
	* gcc.dg/Wreturn-local-addr-6.c: New test.
	* gcc.dg/Wreturn-local-addr-7.c: New test.
	* gcc.dg/Wreturn-local-addr-8.c: New test.
	* gcc.dg/Wreturn-local-addr-9.c: New test.
	* gcc.dg/Wreturn-local-addr-10.c: New test.
	* gcc.dg/Walloca-4.c: Handle expected warnings.
	* gcc.dg/pr41551.c: Same.
	* gcc.dg/pr59523.c: Same.
	* gcc.dg/tree-ssa/pr88775-2.c: Same.
	* gcc.dg/tree-ssa/alias-37.c: Same.
	* gcc.dg/winline-7.c: Same.


Added:
    trunk/gcc/testsuite/gcc.c-torture/execute/return-addr.c
    trunk/gcc/testsuite/gcc.dg/Wreturn-local-addr-10.c
    trunk/gcc/testsuite/gcc.dg/Wreturn-local-addr-2.c
    trunk/gcc/testsuite/gcc.dg/Wreturn-local-addr-3.c
    trunk/gcc/testsuite/gcc.dg/Wreturn-local-addr-4.c
    trunk/gcc/testsuite/gcc.dg/Wreturn-local-addr-5.c
    trunk/gcc/testsuite/gcc.dg/Wreturn-local-addr-6.c
    trunk/gcc/testsuite/gcc.dg/Wreturn-local-addr-7.c
    trunk/gcc/testsuite/gcc.dg/Wreturn-local-addr-8.c
    trunk/gcc/testsuite/gcc.dg/Wreturn-local-addr-9.c
Modified:
    trunk/gcc/ChangeLog
    trunk/gcc/gimple-ssa-isolate-paths.c
    trunk/gcc/testsuite/ChangeLog
    trunk/gcc/testsuite/gcc.dg/Walloca-4.c
    trunk/gcc/testsuite/gcc.dg/pr41551.c
    trunk/gcc/testsuite/gcc.dg/pr59523.c
    trunk/gcc/testsuite/gcc.dg/tree-ssa/alias-37.c
    trunk/gcc/testsuite/gcc.dg/tree-ssa/pr88775-2.c
    trunk/gcc/testsuite/gcc.dg/winline-7.c
    trunk/libgcc/generic-morestack.c
Comment 7 Martin Sebor 2019-07-09 04:16:46 UTC
Patch committed in r273261.