Bug 90549 - missing -Wreturn-local-addr maybe returning an address of a local array plus offset
Summary: missing -Wreturn-local-addr maybe returning an address of a local array plus ...
Status: RESOLVED FIXED
Alias: None
Product: gcc
Classification: Unclassified
Component: middle-end (show other bugs)
Version: 9.0
: P3 normal
Target Milestone: 10.0
Assignee: Martin Sebor
URL:
Keywords: diagnostic, patch
Depends on:
Blocks: Wreturn-local-addr
  Show dependency treegraph
 
Reported: 2019-05-20 16:47 UTC by Martin Sebor
Modified: 2019-07-09 04:19 UTC (History)
0 users

See Also:
Host:
Target:
Build:
Known to work:
Known to fail:
Last reconfirmed: 2019-05-21 00:00:00


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Martin Sebor 2019-05-20 16:47:04 UTC
While testing the enhancement to -Wreturn-local-addr in PR 71924 I noticed that of the following two cases only the first one is diagnosed but the second equivalent case is not.

$ cat a.c && gcc -O2 -S -Wall -fdump-tree-isolate-paths=/dev/stdout a.c
int a[2];

void* f (int i)
{
  int b[2];
  int *p = i ? &a[1] : &b[1];
  return p;        // -Wreturn-local-addr (good)
}

void* g (int i)
{
  int b[2];

  int *p = i ? a : b;
  return p + 1;    // missing -Wreturn-local-addr
}

;; Function f (f, funcdef_no=0, decl_uid=1907, cgraph_uid=1, symbol_order=1)

a.c: In function ‘f’:
a.c:7:10: warning: function may return address of local variable [-Wreturn-local-addr]
    7 |   return p;        // -Wreturn-local-addr (good)
      |          ^
a.c:5:7: note: declared here
    5 |   int b[2];
      |       ^

SSA replacement table
N_i -> { O_1 ... O_j } means that N_i replaces O_1, ..., O_j

iftmp.0_5 -> { iftmp.0_1 }
.MEM_6 -> { .MEM_4 }
Incremental SSA update started at block: 2
Number of blocks in CFG: 6
Number of blocks to update: 2 ( 33%)


Removing basic block 3
f (int i)
{
  int b[2];
  int * iftmp.0_1;
  int * iftmp.0_5;

  <bb 2> [local count: 1073741824]:
  if (i_2(D) == 0)
    goto <bb 4>; [0.00%]
  else
    goto <bb 3>; [100.00%]

  <bb 3> [local count: 536870912]:
  # iftmp.0_1 = PHI <&a[1](2)>
  b ={v} {CLOBBER};
  return iftmp.0_1;

  <bb 4> [count: 0]:
  # iftmp.0_5 = PHI <&b[1](2)>
  b ={v} {CLOBBER};
  return 0B;

}



;; Function g (g, funcdef_no=1, decl_uid=1912, cgraph_uid=2, symbol_order=2)

g (int i)
{
  int b[2];
  int * iftmp.1_1;
  void * _3;

  <bb 2> [local count: 1073741824]:
  if (i_2(D) == 0)
    goto <bb 4>; [50.00%]
  else
    goto <bb 3>; [50.00%]

  <bb 3> [local count: 536870913]:

  <bb 4> [local count: 1073741824]:
  # iftmp.1_1 = PHI <&b(2), &a(3)>
  _3 = iftmp.1_1 + 4;
  b ={v} {CLOBBER};
  return _3;

}
Comment 1 Eric Gallager 2019-05-21 00:29:53 UTC
I think there's enough bugs with -Wreturn-local-addr for it to have its own meta-bug now...
Comment 2 Martin Sebor 2019-05-21 02:22:16 UTC
Agreed.  Please go ahead abd create one.

I'm working on a combined patch for this and PR 71924.
Comment 3 Eric Gallager 2019-05-21 13:28:57 UTC
(In reply to Martin Sebor from comment #2)
> Agreed.  Please go ahead abd create one.
> 
> I'm working on a combined patch for this and PR 71924.

OK, I created bug 90556
Comment 4 Martin Sebor 2019-05-22 21:35:52 UTC
Patch: https://gcc.gnu.org/ml/gcc-patches/2019-05/msg01525.html
Comment 5 Martin Sebor 2019-07-09 04:16:17 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 6 Martin Sebor 2019-07-09 04:19:04 UTC
Fixed via r273261.  Both functions in the test case are now diagnosed:

pr90549.c: In function ‘f’:
pr90549.c:7:10: warning: function may return address of local variable [-Wreturn-local-addr]
    7 |   return p;        // -Wreturn-local-addr (good)
      |          ^
pr90549.c:5:7: note: declared here
    5 |   int b[2];
      |       ^
pr90549.c: In function ‘g’:
pr90549.c:15:12: warning: function may return address of local variable [-Wreturn-local-addr]
   15 |   return p + 1;    // missing -Wreturn-local-addr
      |          ~~^~~
pr90549.c:12:7: note: declared here
   12 |   int b[2];
      |       ^