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

            Bug ID: 104436
           Summary: spurious -Wdangling-pointer assigning local address to
                    a class passed by value
           Product: gcc
           Version: 12.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: ---

The test case below, reduced from
https://bugzilla.redhat.com/show_bug.cgi?id=2051740, shows that
-Wdangling-pointer can be made to trigger by assigning an object of a C++ class
with a user-defined copy ctor that stores a pointer to a local in a function
that takes an argument of the class by value.  GCC transforms the by-value
argument of the function to a by-reference, which is one of the use cases the
warning is designed to detect.

$ cat rhbz2051740.C && gcc -O1 -S -Wall -fdump-tree-optimized=/dev/stdout
rhbz2051740.C
struct S
{
  S () = default;
  S (const S &s): p (s.p) { }

  void *p;
};

void foo (S s)
{
  int a[3];
  S t; t.p = a;
  s = t;
}
rhbz2051740.C: In function ‘void foo(S)’:
rhbz2051740.C:13:5: warning: storing the address of local variable ‘a’ in
‘*s.S::p’ [-Wdangling-pointer=]
   13 |   s = t;
      |   ~~^~~
rhbz2051740.C:11:7: note: ‘a’ declared here
   11 |   int a[3];
      |       ^
rhbz2051740.C:11:7: note: ‘s’ declared here

;; Function foo (_Z3foo1S, funcdef_no=3, decl_uid=2396, cgraph_uid=4,
symbol_order=3)

void foo (struct S & s)
{
  int a[3];

  <bb 2> [local count: 1073741824]:
  s_2(D)->p = &a;
  a ={v} {CLOBBER};
  return;

}

Reply via email to