public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug libstdc++/54296] New: using the object in the map to erase element from the map crashes
@ 2012-08-17 13:08 plasmahh at gmx dot net
  2012-08-17 13:17 ` [Bug libstdc++/54296] " redi at gcc dot gnu.org
                   ` (11 more replies)
  0 siblings, 12 replies; 13+ messages in thread
From: plasmahh at gmx dot net @ 2012-08-17 13:08 UTC (permalink / raw)
  To: gcc-bugs

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

             Bug #: 54296
           Summary: using the object in the map to erase element from the
                    map crashes
    Classification: Unclassified
           Product: gcc
           Version: 4.8.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: libstdc++
        AssignedTo: unassigned@gcc.gnu.org
        ReportedBy: plasmahh@gmx.net


I found a crash in my program, which boils down to the following code. (Note
that this does usually not crash, but will be reported by valgrind with invalid
read after free. Also note that depending no possible internals of the bucket
hashing stuff, the value for i where it crashes might change, so you can use
the random part multiple times to figure out a new one)

#include <unordered_map>
#include <cstddef>
#include <cstdlib>
#include <cassert>
#include <ctime>
#include <iostream>

struct A
{
        int x;
};

int main ( )
{
        srand(time(0));
        std::unordered_map<int,A> map;
        map.max_load_factor(2.0);

        for (size_t i = 0; i < 50; ++i)
        {
                A a;
                a.x = i;
                map.insert({i,a});
        }

//      int i = rand() % map.size();
        int i = 47;
        std::cout << "i = " << i << "\n";

        const A& a = map[i];

        map.erase(a.x);
}
// vim: tabstop=4 shiftwidth=4 noexpandtab ft=cpp






This seems to be due to the while condition in hashtable.h:1526 accessing __k
after the _M_deallocate_node(__p) of line 1517

while (__next_bkt == __bkt && this->_M_equals(__k, __code, __next_n));

I think it is better that after the erase of the node, __k should not be
touched anymore as it migh be part of the object being erased.


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

end of thread, other threads:[~2012-09-07  9:34 UTC | newest]

Thread overview: 13+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2012-08-17 13:08 [Bug libstdc++/54296] New: using the object in the map to erase element from the map crashes plasmahh at gmx dot net
2012-08-17 13:17 ` [Bug libstdc++/54296] " redi at gcc dot gnu.org
2012-08-26 10:56 ` fdumont at gcc dot gnu.org
2012-08-26 12:31 ` paolo.carlini at oracle dot com
2012-08-26 12:38 ` paolo.carlini at oracle dot com
2012-08-26 12:41 ` paolo.carlini at oracle dot com
2012-08-26 23:07 ` paolo.carlini at oracle dot com
2012-08-26 23:16 ` paolo.carlini at oracle dot com
2012-08-26 23:21 ` paolo.carlini at oracle dot com
2012-08-27  7:59 ` fdumont at gcc dot gnu.org
2012-08-27  9:36 ` paolo.carlini at oracle dot com
2012-09-05 19:41 ` fdumont at gcc dot gnu.org
2012-09-07  9:34 ` paolo.carlini at oracle dot com

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).