public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug libstdc++/17948] New: iterator increment bug in _Rb_tree::erase(it,it)
@ 2004-10-12 16:29 gcc-bugzilla at gcc dot gnu dot org
  2004-10-12 16:33 ` [Bug libstdc++/17948] " pinskia at gcc dot gnu dot org
                   ` (4 more replies)
  0 siblings, 5 replies; 6+ messages in thread
From: gcc-bugzilla at gcc dot gnu dot org @ 2004-10-12 16:29 UTC (permalink / raw)
  To: gcc-bugs



I expect the following program to produce the following output:

a 2
b 1 1

i.e, two elements go into the set, we delete one, and one remains.

However, when i compile this with the current cvs head, the program
actually does the following:

$ g++ -g -o x x.cc
$ ./x
a 2
b 0 1
$

i.e., two elements go in, take away one, and none are left.
Someone's arithmetic is bad...

It looks like the problem is in this function in stl_tree.h:


  template<typename _Key, typename _Val, typename _KeyOfValue,
           typename _Compare, typename _Alloc>
    void
    _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::
    erase(iterator __first, iterator __last)
    {
      if (__first == begin() && __last == end())
	clear();
      else
	for (; __first != __last; ++__first)
	  erase(__first);
    }

Note that in the loop, the iterator __first is referenced in the
increment _after_ it has been erased from the container --- which
is undefined behavior.

Below is an attempt to fix this.  With this patch, the test behaves
as expected.

Environment:
System: Linux karma 2.6.8.1 #20 Mon Sep 13 23:48:47 EDT 2004 i686 i686 i386 GNU/Linux
Architecture: i686

	<machine, os, target, libraries (multiple lines)>
host: i686-pc-linux-gnu
build: i686-pc-linux-gnu
target: i686-pc-linux-gnu
configured with: /home/sss/gcc/gcc/configure --prefix=/usr/local/gcc --enable-threads=posix --enable-long-long --enable-languages=c,c++,f95

How-To-Repeat:

-------------------------------------------
#include <set>

void test2 ()
{
  std::set<int> s;
  
  s.insert(2);
  s.insert(3);

  printf ("a %d\n", s.size());
  int x = s.erase (3);
  printf ("b %d %d\n", s.size(), x);
}


int main ()
{
  test2 ();
  return 0;
}
-------------------------------------------
------- Additional Comments From snyder at fnal dot gov  2004-10-12 16:29 -------
Fix:

2004-10-11  scott snyder  <snyder@fnal.gov>

	* include/bits/stl_tree.h (erase(iterator,iterator)): Fix iterator
	loop.

--- libstdc++-v3/include/bits/stl_tree.h-orig	2004-10-11 23:17:30.096014584 -0400
+++ libstdc++-v3/include/bits/stl_tree.h	2004-10-11 23:19:39.688313560 -0400
@@ -1087,8 +1087,12 @@
       if (__first == begin() && __last == end())
 	clear();
       else
-	for (; __first != __last; ++__first)
+	for (iterator __next = __first; __first != __last; __first = __next)
+        {
+	  ++__next;
 	  erase(__first);
+	}
+       
     }
 
   template<typename _Key, typename _Val, typename _KeyOfValue,

-- 
           Summary: iterator increment bug in _Rb_tree::erase(it,it)
           Product: gcc
           Version: 0.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: libstdc++
        AssignedTo: unassigned at gcc dot gnu dot org
        ReportedBy: snyder at fnal dot gov
                CC: gcc-bugs at gcc dot gnu dot org
 GCC build triplet: i686-pc-linux-gnu
  GCC host triplet: i686-pc-linux-gnu
GCC target triplet: i686-pc-linux-gnu


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


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

* [Bug libstdc++/17948] iterator increment bug in _Rb_tree::erase(it,it)
  2004-10-12 16:29 [Bug libstdc++/17948] New: iterator increment bug in _Rb_tree::erase(it,it) gcc-bugzilla at gcc dot gnu dot org
@ 2004-10-12 16:33 ` pinskia at gcc dot gnu dot org
  2004-10-12 16:59 ` pcarlini at suse dot de
                   ` (3 subsequent siblings)
  4 siblings, 0 replies; 6+ messages in thread
From: pinskia at gcc dot gnu dot org @ 2004-10-12 16:33 UTC (permalink / raw)
  To: gcc-bugs



-- 
           What    |Removed                     |Added
----------------------------------------------------------------------------
            Version|0.0                         |4.0.0


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


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

* [Bug libstdc++/17948] iterator increment bug in _Rb_tree::erase(it,it)
  2004-10-12 16:29 [Bug libstdc++/17948] New: iterator increment bug in _Rb_tree::erase(it,it) gcc-bugzilla at gcc dot gnu dot org
  2004-10-12 16:33 ` [Bug libstdc++/17948] " pinskia at gcc dot gnu dot org
@ 2004-10-12 16:59 ` pcarlini at suse dot de
  2004-10-12 17:22 ` pcarlini at suse dot de
                   ` (2 subsequent siblings)
  4 siblings, 0 replies; 6+ messages in thread
From: pcarlini at suse dot de @ 2004-10-12 16:59 UTC (permalink / raw)
  To: gcc-bugs


------- Additional Comments From pcarlini at suse dot de  2004-10-12 16:57 -------
I'm guilty of this, sorry, your patch looks correct. Will deal with this later
today.


-- 
           What    |Removed                     |Added
----------------------------------------------------------------------------
         AssignedTo|unassigned at gcc dot gnu   |pcarlini at suse dot de
                   |dot org                     |
             Status|UNCONFIRMED                 |ASSIGNED


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


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

* [Bug libstdc++/17948] iterator increment bug in _Rb_tree::erase(it,it)
  2004-10-12 16:29 [Bug libstdc++/17948] New: iterator increment bug in _Rb_tree::erase(it,it) gcc-bugzilla at gcc dot gnu dot org
  2004-10-12 16:33 ` [Bug libstdc++/17948] " pinskia at gcc dot gnu dot org
  2004-10-12 16:59 ` pcarlini at suse dot de
@ 2004-10-12 17:22 ` pcarlini at suse dot de
  2004-10-13  0:11 ` cvs-commit at gcc dot gnu dot org
  2004-10-13  0:12 ` pcarlini at suse dot de
  4 siblings, 0 replies; 6+ messages in thread
From: pcarlini at suse dot de @ 2004-10-12 17:22 UTC (permalink / raw)
  To: gcc-bugs


------- Additional Comments From pcarlini at suse dot de  2004-10-12 17:17 -------
Everything considered, if we have to use an additional iterator next, probably is
better just reverting my stupid change and return to:

  while (__first != __last)
    erase(__first++);




-- 


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


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

* [Bug libstdc++/17948] iterator increment bug in _Rb_tree::erase(it,it)
  2004-10-12 16:29 [Bug libstdc++/17948] New: iterator increment bug in _Rb_tree::erase(it,it) gcc-bugzilla at gcc dot gnu dot org
                   ` (2 preceding siblings ...)
  2004-10-12 17:22 ` pcarlini at suse dot de
@ 2004-10-13  0:11 ` cvs-commit at gcc dot gnu dot org
  2004-10-13  0:12 ` pcarlini at suse dot de
  4 siblings, 0 replies; 6+ messages in thread
From: cvs-commit at gcc dot gnu dot org @ 2004-10-13  0:11 UTC (permalink / raw)
  To: gcc-bugs


------- Additional Comments From cvs-commit at gcc dot gnu dot org  2004-10-13 00:11 -------
Subject: Bug 17948

CVSROOT:	/cvs/gcc
Module name:	gcc
Changes by:	paolo@gcc.gnu.org	2004-10-13 00:11:14

Modified files:
	libstdc++-v3   : ChangeLog 
	libstdc++-v3/include/bits: stl_tree.h 
Added files:
	libstdc++-v3/testsuite/23_containers/set/modifiers: 17948.cc 

Log message:
	2004-10-12  Paolo Carlini  <pcarlini@suse.de>
	
	PR libstdc++/17948
	* include/bits/stl_tree.h (erase(iterator, iterator)): Revert
	wrong commit of 2004-10-07.
	
	2004-10-12  Scott Snyder  <snyder@fnal.gov>
	
	PR libstdc++/17948
	* testsuite/23_containers/set/modifiers/17948.cc: New.

Patches:
http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/libstdc++-v3/ChangeLog.diff?cvsroot=gcc&r1=1.2700&r2=1.2701
http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/libstdc++-v3/include/bits/stl_tree.h.diff?cvsroot=gcc&r1=1.39&r2=1.40
http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/libstdc++-v3/testsuite/23_containers/set/modifiers/17948.cc.diff?cvsroot=gcc&r1=NONE&r2=1.1



-- 


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


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

* [Bug libstdc++/17948] iterator increment bug in _Rb_tree::erase(it,it)
  2004-10-12 16:29 [Bug libstdc++/17948] New: iterator increment bug in _Rb_tree::erase(it,it) gcc-bugzilla at gcc dot gnu dot org
                   ` (3 preceding siblings ...)
  2004-10-13  0:11 ` cvs-commit at gcc dot gnu dot org
@ 2004-10-13  0:12 ` pcarlini at suse dot de
  4 siblings, 0 replies; 6+ messages in thread
From: pcarlini at suse dot de @ 2004-10-13  0:12 UTC (permalink / raw)
  To: gcc-bugs


------- Additional Comments From pcarlini at suse dot de  2004-10-13 00:12 -------
Fixed.

-- 
           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|ASSIGNED                    |RESOLVED
         Resolution|                            |FIXED
   Target Milestone|---                         |4.0.0


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


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

end of thread, other threads:[~2004-10-13  0:12 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2004-10-12 16:29 [Bug libstdc++/17948] New: iterator increment bug in _Rb_tree::erase(it,it) gcc-bugzilla at gcc dot gnu dot org
2004-10-12 16:33 ` [Bug libstdc++/17948] " pinskia at gcc dot gnu dot org
2004-10-12 16:59 ` pcarlini at suse dot de
2004-10-12 17:22 ` pcarlini at suse dot de
2004-10-13  0:11 ` cvs-commit at gcc dot gnu dot org
2004-10-13  0:12 ` pcarlini at suse dot de

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