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

            Bug ID: 90549
           Summary: missing -Wreturn-local-addr maybe returning an address
                    of a local array plus offset
           Product: gcc
           Version: 9.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: middle-end
          Assignee: unassigned at gcc dot gnu.org
          Reporter: msebor at gcc dot gnu.org
  Target Milestone: ---

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;

}

Reply via email to