public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug middle-end/104436] New: spurious -Wdangling-pointer assigning local address to a class passed by value
@ 2022-02-07 23:49 msebor at gcc dot gnu.org
  2022-02-07 23:50 ` [Bug middle-end/104436] " msebor at gcc dot gnu.org
                   ` (6 more replies)
  0 siblings, 7 replies; 8+ messages in thread
From: msebor at gcc dot gnu.org @ 2022-02-07 23:49 UTC (permalink / raw)
  To: gcc-bugs

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;

}

^ permalink raw reply	[flat|nested] 8+ messages in thread

end of thread, other threads:[~2022-03-15  0:27 UTC | newest]

Thread overview: 8+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-02-07 23:49 [Bug middle-end/104436] New: spurious -Wdangling-pointer assigning local address to a class passed by value msebor at gcc dot gnu.org
2022-02-07 23:50 ` [Bug middle-end/104436] " msebor at gcc dot gnu.org
2022-02-08  0:22 ` msebor at gcc dot gnu.org
2022-02-08  9:08 ` rguenth at gcc dot gnu.org
2022-02-08 22:00 ` [Bug middle-end/104436] [12 Regression] " msebor at gcc dot gnu.org
2022-03-09 13:18 ` rguenth at gcc dot gnu.org
2022-03-15  0:27 ` cvs-commit at gcc dot gnu.org
2022-03-15  0:27 ` msebor at gcc dot gnu.org

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).