public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug libstdc++/52967] New: Segmentation fault on std::vector destruction
@ 2012-04-12 23:54 karlicoss at gmail dot com
  2012-04-13  0:15 ` [Bug c++/52967] " pinskia at gcc dot gnu.org
                   ` (3 more replies)
  0 siblings, 4 replies; 5+ messages in thread
From: karlicoss at gmail dot com @ 2012-04-12 23:54 UTC (permalink / raw)
  To: gcc-bugs

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

             Bug #: 52967
           Summary: Segmentation fault on std::vector destruction
    Classification: Unclassified
           Product: gcc
           Version: unknown
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: libstdc++
        AssignedTo: unassigned@gcc.gnu.org
        ReportedBy: karlicoss@gmail.com


GCC version:
4.6.1 (Ubuntu/Linaro 4.6.1-9ubuntu3) 

Environment:
Ubuntu 11.10 amd64

I've also reproduced it at:
Ubuntu 11.10 amd64, gcc version 4.4.6
Windows 7 x64, mingw version 4.6.2. 
Gentoo x86, gcc-4.5.3-r2
Arch Linux x64, gcc version 4.7.0 


How to repeat:
main.cpp:
#include <vector>

struct Foo
{
    double a, b, c, d;
};

std::vector<Foo> v;
int steps = 3;

double run()
{
    if (steps == 0)
        return 1.0;

    steps--;
    v.push_back(Foo());

    v[0].a = run();

    //double tmp = run();
    //nodes[0].a = tmp;

    return 1.0;
}

int main()
{
    run();
    return 0;
}


$ g++ main.cpp -o main && ./main
results in segmentation fault. The backtrace shows the problem is in vector
destructor.


This one is very weird, for example:
1)
replacing 
nodes[0].a = run()
with
double tmp = run();
nodes[0].a = tmp;
does not give segfault anymore.

2)
Reducing the Foo structure size (for example, using only three doubles instead
of four or using two doubles and two ints) does not give segfault.

3)
Decreasing the steps variable does not result in segfault.

4)
Returning 0.0 in the run function does not result in segfault (any non-zero
value still does).

5)
Setting the initial size for v sometimes results in segfault, sometime does.


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

* [Bug c++/52967] Segmentation fault on std::vector destruction
  2012-04-12 23:54 [Bug libstdc++/52967] New: Segmentation fault on std::vector destruction karlicoss at gmail dot com
@ 2012-04-13  0:15 ` pinskia at gcc dot gnu.org
  2012-04-13  8:26 ` karlicoss at gmail dot com
                   ` (2 subsequent siblings)
  3 siblings, 0 replies; 5+ messages in thread
From: pinskia at gcc dot gnu.org @ 2012-04-13  0:15 UTC (permalink / raw)
  To: gcc-bugs

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

Andrew Pinski <pinskia at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
          Component|libstdc++                   |c++

--- Comment #1 from Andrew Pinski <pinskia at gcc dot gnu.org> 2012-04-13 00:15:05 UTC ---
I don't know if this is not undefined code.
>    v[0].a = run();

Is this:
double &a = v[0].a;
a = run();
Or:
double tmp = run();
v[0].a = tmp;

I think both are correct because of the way the C++ standard defines =.


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

* [Bug c++/52967] Segmentation fault on std::vector destruction
  2012-04-12 23:54 [Bug libstdc++/52967] New: Segmentation fault on std::vector destruction karlicoss at gmail dot com
  2012-04-13  0:15 ` [Bug c++/52967] " pinskia at gcc dot gnu.org
@ 2012-04-13  8:26 ` karlicoss at gmail dot com
  2012-04-13  8:44 ` karlicoss at gmail dot com
  2012-04-13  9:52 ` redi at gcc dot gnu.org
  3 siblings, 0 replies; 5+ messages in thread
From: karlicoss at gmail dot com @ 2012-04-13  8:26 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #2 from Dmitry Gerasimov <karlicoss at gmail dot com> 2012-04-13 08:26:08 UTC ---
(In reply to comment #1)
> I don't know if this is not undefined code.
> >    v[0].a = run();
> 
> Is this:
> double &a = v[0].a;
> a = run();
> Or:
> double tmp = run();
> v[0].a = tmp;
> 
> I think both are correct because of the way the C++ standard defines =.

Hm,
> double &a = v[0].a;
> a = run();
does result in segfault. Should it?


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

* [Bug c++/52967] Segmentation fault on std::vector destruction
  2012-04-12 23:54 [Bug libstdc++/52967] New: Segmentation fault on std::vector destruction karlicoss at gmail dot com
  2012-04-13  0:15 ` [Bug c++/52967] " pinskia at gcc dot gnu.org
  2012-04-13  8:26 ` karlicoss at gmail dot com
@ 2012-04-13  8:44 ` karlicoss at gmail dot com
  2012-04-13  9:52 ` redi at gcc dot gnu.org
  3 siblings, 0 replies; 5+ messages in thread
From: karlicoss at gmail dot com @ 2012-04-13  8:44 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #3 from Dmitry Gerasimov <karlicoss at gmail dot com> 2012-04-13 08:44:10 UTC ---
(In reply to comment #1)
> I don't know if this is not undefined code.
> >    v[0].a = run();
> 
> Is this:
> double &a = v[0].a;
> a = run();
> Or:
> double tmp = run();
> v[0].a = tmp;
> 
> I think both are correct because of the way the C++ standard defines =.

Ok, I got this.
If v[0].a = run(); is equivalent to double &a = v[0].a; a = run();, we:
1. calculate the address of a;
2. recurse into run
3. push_back, causing vector to increase its capacity and reallocate its
memory, which makes a to point to free memory.
I guess I should mark bug as Invalid?


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

* [Bug c++/52967] Segmentation fault on std::vector destruction
  2012-04-12 23:54 [Bug libstdc++/52967] New: Segmentation fault on std::vector destruction karlicoss at gmail dot com
                   ` (2 preceding siblings ...)
  2012-04-13  8:44 ` karlicoss at gmail dot com
@ 2012-04-13  9:52 ` redi at gcc dot gnu.org
  3 siblings, 0 replies; 5+ messages in thread
From: redi at gcc dot gnu.org @ 2012-04-13  9:52 UTC (permalink / raw)
  To: gcc-bugs

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

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

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|UNCONFIRMED                 |RESOLVED
         Resolution|                            |INVALID

--- Comment #4 from Jonathan Wakely <redi at gcc dot gnu.org> 2012-04-13 09:52:03 UTC ---
Yes, this is invalid.  If you want to do this then you need to call reserve on
the vector so that push_back doesn't cause reallocation (or decide if the code
shouldn't just be rewritten differently!)


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

end of thread, other threads:[~2012-04-13  9:52 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2012-04-12 23:54 [Bug libstdc++/52967] New: Segmentation fault on std::vector destruction karlicoss at gmail dot com
2012-04-13  0:15 ` [Bug c++/52967] " pinskia at gcc dot gnu.org
2012-04-13  8:26 ` karlicoss at gmail dot com
2012-04-13  8:44 ` karlicoss at gmail dot com
2012-04-13  9:52 ` redi 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).