public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c++/54197] New: [4.7/4.8 regression] Lifetime of reference not properly extended
@ 2012-08-07 18:54 ppluzhnikov at google dot com
  2012-08-07 19:56 ` [Bug c++/54197] " hjl.tools at gmail dot com
                   ` (8 more replies)
  0 siblings, 9 replies; 10+ messages in thread
From: ppluzhnikov at google dot com @ 2012-08-07 18:54 UTC (permalink / raw)
  To: gcc-bugs

http://gcc.gnu.org/bugzilla/show_bug.cgi?id=54197

             Bug #: 54197
           Summary: [4.7/4.8 regression] Lifetime of reference not
                    properly extended
    Classification: Unclassified
           Product: gcc
           Version: unknown
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
        AssignedTo: unassigned@gcc.gnu.org
        ReportedBy: ppluzhnikov@google.com


We've noticed this with 4.7 branch, confirmed present in current trunk (rev.
190209).

When calling a static member function returning an object (string below)
through an unnamed function pointer, and binding the result to a reference, the
lifetime of the returned temporary is not properly extended.

--- cut ---
#include <stdlib.h>
#include <string.h>
#include <stdio.h>

struct string {
  string(const char *s) {
    s_ = strdup(s);
  }
  ~string() {
    memset((void*)s_, 'a', strlen(s_));
  }
  string(const string& rhs) {
    s_ = strdup(rhs.s_);
  }
  string& operator=(const string& rhs) {
    if (&rhs != this) {
      free((void*)s_);
      s_ = strdup(rhs.s_);
    }
    return *this;
  }
  const char *c_str() const { return s_; }

  const char *s_;
};

struct Foo {
  static string foo() { return "abcd"; }
};

Foo foo_g;

struct scoped_ptr {
  Foo* operator->() const { return &foo_g; }
  Foo* get() const { return &foo_g; }
};

Foo *get() { return &foo_g; }

int main()
{
  scoped_ptr f;
  const string& ref1 = f->foo();           // BAD
  const string& ref2 = f.get()->foo();     // BAD
  const string& ref3 = get()->foo();       // BAD
  const string& ref4 = Foo::foo();         // OK
  Foo *pf = f.get();
  const string& ref5 = pf->foo();          // OK


  printf("ref1: %p (%s)\n", ref1.c_str(), ref1.c_str());
  printf("ref2: %p (%s)\n", ref2.c_str(), ref2.c_str());
  printf("ref3: %p (%s)\n", ref3.c_str(), ref3.c_str());
  printf("ref4: %p (%s)\n", ref4.c_str(), ref4.c_str());
  printf("ref5: %p (%s)\n", ref5.c_str(), ref5.c_str());
}
--- cut ---

Result from gcc-4.6:

ref1: 0x5a8030 (abcd)
ref2: 0x5a8070 (abcd)
ref3: 0x5a80b0 (abcd)
ref4: 0x5a80d0 (abcd)
ref5: 0x5a80f0 (abcd)


Result from gcc-4.7 / 4.8

ref1: 0xe18010 (aaaa)  << BUG
ref2: 0xe18030 (aaaa)  << BUG
ref3: 0xe18050 (aaaa)  << BUG
ref4: 0xe18070 (abcd)
ref5: 0xe18090 (abcd)

Changing Foo::foo to be non-static (and commenting out ref4), the bug
disappears:

ref1: 0x1eb3010 (abcd)
ref2: 0x1eb3030 (abcd)
ref3: 0x1eb3050 (abcd)
ref5: 0x1eb3070 (abcd)


Google ref: b/6946758


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

* [Bug c++/54197] [4.7/4.8 regression] Lifetime of reference not properly extended
  2012-08-07 18:54 [Bug c++/54197] New: [4.7/4.8 regression] Lifetime of reference not properly extended ppluzhnikov at google dot com
@ 2012-08-07 19:56 ` hjl.tools at gmail dot com
  2012-08-08  7:54 ` rguenth at gcc dot gnu.org
                   ` (7 subsequent siblings)
  8 siblings, 0 replies; 10+ messages in thread
From: hjl.tools at gmail dot com @ 2012-08-07 19:56 UTC (permalink / raw)
  To: gcc-bugs

http://gcc.gnu.org/bugzilla/show_bug.cgi?id=54197

H.J. Lu <hjl.tools at gmail dot com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|UNCONFIRMED                 |NEW
   Last reconfirmed|                            |2012-08-07
                 CC|                            |jason at redhat dot com
     Ever Confirmed|0                           |1

--- Comment #1 from H.J. Lu <hjl.tools at gmail dot com> 2012-08-07 19:55:37 UTC ---
It is caused by revision 180944:

http://gcc.gnu.org/ml/gcc-cvs/2011-11/msg00230.html


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

* [Bug c++/54197] [4.7/4.8 regression] Lifetime of reference not properly extended
  2012-08-07 18:54 [Bug c++/54197] New: [4.7/4.8 regression] Lifetime of reference not properly extended ppluzhnikov at google dot com
  2012-08-07 19:56 ` [Bug c++/54197] " hjl.tools at gmail dot com
@ 2012-08-08  7:54 ` rguenth at gcc dot gnu.org
  2012-08-13 18:04 ` aaw at gcc dot gnu.org
                   ` (6 subsequent siblings)
  8 siblings, 0 replies; 10+ messages in thread
From: rguenth at gcc dot gnu.org @ 2012-08-08  7:54 UTC (permalink / raw)
  To: gcc-bugs

http://gcc.gnu.org/bugzilla/show_bug.cgi?id=54197

Richard Guenther <rguenth at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
   Target Milestone|---                         |4.7.2


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

* [Bug c++/54197] [4.7/4.8 regression] Lifetime of reference not properly extended
  2012-08-07 18:54 [Bug c++/54197] New: [4.7/4.8 regression] Lifetime of reference not properly extended ppluzhnikov at google dot com
  2012-08-07 19:56 ` [Bug c++/54197] " hjl.tools at gmail dot com
  2012-08-08  7:54 ` rguenth at gcc dot gnu.org
@ 2012-08-13 18:04 ` aaw at gcc dot gnu.org
  2012-08-13 18:37 ` redi at gcc dot gnu.org
                   ` (5 subsequent siblings)
  8 siblings, 0 replies; 10+ messages in thread
From: aaw at gcc dot gnu.org @ 2012-08-13 18:04 UTC (permalink / raw)
  To: gcc-bugs

http://gcc.gnu.org/bugzilla/show_bug.cgi?id=54197

Ollie Wild <aaw at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |aaw at gcc dot gnu.org
         AssignedTo|unassigned at gcc dot       |aaw at gcc dot gnu.org
                   |gnu.org                     |

--- Comment #2 from Ollie Wild <aaw at gcc dot gnu.org> 2012-08-13 18:04:21 UTC ---
The issue is that these cause a COMPOUND_EXPR to be passed to
extend_ref_init_temps_1.

I have a patch which replaces the second operand of the COMPOUND_EXPR with
another call to extend_ref_init_temps_1.  Testing now.  Will send out for
review shortly.


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

* [Bug c++/54197] [4.7/4.8 regression] Lifetime of reference not properly extended
  2012-08-07 18:54 [Bug c++/54197] New: [4.7/4.8 regression] Lifetime of reference not properly extended ppluzhnikov at google dot com
                   ` (2 preceding siblings ...)
  2012-08-13 18:04 ` aaw at gcc dot gnu.org
@ 2012-08-13 18:37 ` redi at gcc dot gnu.org
  2012-08-16 18:44 ` aaw at gcc dot gnu.org
                   ` (4 subsequent siblings)
  8 siblings, 0 replies; 10+ messages in thread
From: redi at gcc dot gnu.org @ 2012-08-13 18:37 UTC (permalink / raw)
  To: gcc-bugs

http://gcc.gnu.org/bugzilla/show_bug.cgi?id=54197

Jonathan Wakely <redi at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|NEW                         |ASSIGNED


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

* [Bug c++/54197] [4.7/4.8 regression] Lifetime of reference not properly extended
  2012-08-07 18:54 [Bug c++/54197] New: [4.7/4.8 regression] Lifetime of reference not properly extended ppluzhnikov at google dot com
                   ` (3 preceding siblings ...)
  2012-08-13 18:37 ` redi at gcc dot gnu.org
@ 2012-08-16 18:44 ` aaw at gcc dot gnu.org
  2012-08-16 18:47 ` aaw at gcc dot gnu.org
                   ` (3 subsequent siblings)
  8 siblings, 0 replies; 10+ messages in thread
From: aaw at gcc dot gnu.org @ 2012-08-16 18:44 UTC (permalink / raw)
  To: gcc-bugs

http://gcc.gnu.org/bugzilla/show_bug.cgi?id=54197

--- Comment #3 from Ollie Wild <aaw at gcc dot gnu.org> 2012-08-16 18:44:01 UTC ---
Author: aaw
Date: Thu Aug 16 18:43:52 2012
New Revision: 190450

URL: http://gcc.gnu.org/viewcvs?root=gcc&view=rev&rev=190450
Log:
Fix some cases where lifetimes of temporaries bound to expressions are not
properly extended (Google ref b/6946758).

2012-08-16  Ollie Wild  <aaw@google.com>

    PR c++/54197
    * gcc/cp/call.c (extend_ref_init_temps_1): Handle COMPOUND_EXPR trees.
    * gcc/testsuite/g++.dg/init/lifetime3.C: New test.

Added:
    branches/google/gcc-4_7/gcc/testsuite/g++.dg/init/lifetime3.C
Modified:
    branches/google/gcc-4_7/gcc/cp/ChangeLog.google-4_7
    branches/google/gcc-4_7/gcc/cp/call.c
    branches/google/gcc-4_7/gcc/testsuite/ChangeLog.google-4_7


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

* [Bug c++/54197] [4.7/4.8 regression] Lifetime of reference not properly extended
  2012-08-07 18:54 [Bug c++/54197] New: [4.7/4.8 regression] Lifetime of reference not properly extended ppluzhnikov at google dot com
                   ` (4 preceding siblings ...)
  2012-08-16 18:44 ` aaw at gcc dot gnu.org
@ 2012-08-16 18:47 ` aaw at gcc dot gnu.org
  2012-08-31 15:48 ` aaw at gcc dot gnu.org
                   ` (2 subsequent siblings)
  8 siblings, 0 replies; 10+ messages in thread
From: aaw at gcc dot gnu.org @ 2012-08-16 18:47 UTC (permalink / raw)
  To: gcc-bugs

http://gcc.gnu.org/bugzilla/show_bug.cgi?id=54197

--- Comment #4 from Ollie Wild <aaw at gcc dot gnu.org> 2012-08-16 18:46:42 UTC ---
Fix submitted to the google/gcc-4_7 branch.

Still waiting on maintainer approval for the trunk and gcc-4_7-branches.


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

* [Bug c++/54197] [4.7/4.8 regression] Lifetime of reference not properly extended
  2012-08-07 18:54 [Bug c++/54197] New: [4.7/4.8 regression] Lifetime of reference not properly extended ppluzhnikov at google dot com
                   ` (5 preceding siblings ...)
  2012-08-16 18:47 ` aaw at gcc dot gnu.org
@ 2012-08-31 15:48 ` aaw at gcc dot gnu.org
  2012-08-31 17:17 ` aaw at gcc dot gnu.org
  2012-08-31 17:24 ` aaw at gcc dot gnu.org
  8 siblings, 0 replies; 10+ messages in thread
From: aaw at gcc dot gnu.org @ 2012-08-31 15:48 UTC (permalink / raw)
  To: gcc-bugs

http://gcc.gnu.org/bugzilla/show_bug.cgi?id=54197

--- Comment #5 from Ollie Wild <aaw at gcc dot gnu.org> 2012-08-31 15:47:37 UTC ---
Author: aaw
Date: Fri Aug 31 15:47:29 2012
New Revision: 190834

URL: http://gcc.gnu.org/viewcvs?root=gcc&view=rev&rev=190834
Log:
2012-08-31  Ollie Wild  <aaw@google.com>

    PR c++/54197
    * gcc/cp/call.c (extend_ref_init_temps_1): Handle COMPOUND_EXPR trees.
    * gcc/testsuite/g++.dg/init/lifetime3.C: New test.

Added:
    trunk/gcc/testsuite/g++.dg/init/lifetime3.C
Modified:
    trunk/gcc/cp/ChangeLog
    trunk/gcc/cp/call.c
    trunk/gcc/testsuite/ChangeLog


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

* [Bug c++/54197] [4.7/4.8 regression] Lifetime of reference not properly extended
  2012-08-07 18:54 [Bug c++/54197] New: [4.7/4.8 regression] Lifetime of reference not properly extended ppluzhnikov at google dot com
                   ` (6 preceding siblings ...)
  2012-08-31 15:48 ` aaw at gcc dot gnu.org
@ 2012-08-31 17:17 ` aaw at gcc dot gnu.org
  2012-08-31 17:24 ` aaw at gcc dot gnu.org
  8 siblings, 0 replies; 10+ messages in thread
From: aaw at gcc dot gnu.org @ 2012-08-31 17:17 UTC (permalink / raw)
  To: gcc-bugs

http://gcc.gnu.org/bugzilla/show_bug.cgi?id=54197

--- Comment #6 from Ollie Wild <aaw at gcc dot gnu.org> 2012-08-31 17:16:47 UTC ---
Author: aaw
Date: Fri Aug 31 17:16:39 2012
New Revision: 190839

URL: http://gcc.gnu.org/viewcvs?root=gcc&view=rev&rev=190839
Log:
Backport from mainline
2012-08-31  Ollie Wild  <aaw@google.com>

    PR c++/54197
    * gcc/cp/call.c (extend_ref_init_temps_1): Handle COMPOUND_EXPR trees.
    * gcc/testsuite/g++.dg/init/lifetime3.C: New test.

Added:
    branches/gcc-4_7-branch/gcc/testsuite/g++.dg/init/lifetime3.C
Modified:
    branches/gcc-4_7-branch/gcc/cp/ChangeLog
    branches/gcc-4_7-branch/gcc/cp/call.c
    branches/gcc-4_7-branch/gcc/testsuite/ChangeLog


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

* [Bug c++/54197] [4.7/4.8 regression] Lifetime of reference not properly extended
  2012-08-07 18:54 [Bug c++/54197] New: [4.7/4.8 regression] Lifetime of reference not properly extended ppluzhnikov at google dot com
                   ` (7 preceding siblings ...)
  2012-08-31 17:17 ` aaw at gcc dot gnu.org
@ 2012-08-31 17:24 ` aaw at gcc dot gnu.org
  8 siblings, 0 replies; 10+ messages in thread
From: aaw at gcc dot gnu.org @ 2012-08-31 17:24 UTC (permalink / raw)
  To: gcc-bugs

http://gcc.gnu.org/bugzilla/show_bug.cgi?id=54197

Ollie Wild <aaw at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|ASSIGNED                    |RESOLVED
         Resolution|                            |FIXED

--- Comment #7 from Ollie Wild <aaw at gcc dot gnu.org> 2012-08-31 17:24:07 UTC ---
Fixed for 4.7.2+.


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

end of thread, other threads:[~2012-08-31 17:24 UTC | newest]

Thread overview: 10+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2012-08-07 18:54 [Bug c++/54197] New: [4.7/4.8 regression] Lifetime of reference not properly extended ppluzhnikov at google dot com
2012-08-07 19:56 ` [Bug c++/54197] " hjl.tools at gmail dot com
2012-08-08  7:54 ` rguenth at gcc dot gnu.org
2012-08-13 18:04 ` aaw at gcc dot gnu.org
2012-08-13 18:37 ` redi at gcc dot gnu.org
2012-08-16 18:44 ` aaw at gcc dot gnu.org
2012-08-16 18:47 ` aaw at gcc dot gnu.org
2012-08-31 15:48 ` aaw at gcc dot gnu.org
2012-08-31 17:17 ` aaw at gcc dot gnu.org
2012-08-31 17:24 ` aaw 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).