[libstdc++ PATCH] Fix leak in _Rb_tree::_M_insert (stl_tree.h)

Jonathan Wakely cow@compsoc.man.ac.uk
Fri Feb 25 18:43:00 GMT 2005


On Thu, Feb 24, 2005 at 09:29:27PM -0800, Earl Chew wrote:

> Gabriel Dos Reis wrote:
> >Would there be a way to systematize these expclicit try-block without
> >efficiency loss (a la RAII or sentry)?
> 
> Yes, RAII is preferred, but I decided to keep within the spirit of what
> was already there to fix this bug rather than rewrite this and
> other sections.

No need to change anything outside that function, see patch.

jon

-- 
"Until they become conscious they will never rebel,
 and until after they have rebelled they cannot become conscious."
	- 1984, George Orwell
-------------- next part --------------
Index: include/bits/stl_tree.h
===================================================================
RCS file: /cvs/gcc/gcc/libstdc++-v3/include/bits/stl_tree.h,v
retrieving revision 1.44
diff -u -p -b -B -r1.44 stl_tree.h
--- include/bits/stl_tree.h	1 Feb 2005 13:30:30 -0000	1.44
+++ include/bits/stl_tree.h	25 Feb 2005 12:51:28 -0000
@@ -789,10 +790,33 @@ namespace std
       _Link_type __z = _M_create_node(__v);
       bool __insert_left;
 
+      typedef _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc> this_type;
+      class Rollback
+      {
+      public:
+        Rollback(this_type* obj, void (this_type::*f)(_Link_type), _Link_type arg)
+        : obj_(obj), f_(f), arg_(arg), cancelled_(false)
+        { }
+
+        ~Rollback() { if (!cancelled_) (obj_->*f_)(arg_); }
+
+        void cancel() { cancelled_ = true; }
+
+      private:
+        this_type* obj_;
+        void (this_type::*f_)(_Link_type);
+        _Link_type arg_;
+        bool cancelled_;
+      };
+
+      Rollback rollback(this, &this_type::destroy_node, __z);
+
       __insert_left = (__x != 0 || __p == _M_end()
 		       || _M_impl._M_key_compare(_KeyOfValue()(__v), 
 						 _S_key(__p)));
 
+      rollback.cancel();
+
       _Rb_tree_insert_and_rebalance(__insert_left, __z, __p,  
 				    this->_M_impl._M_header);
       ++_M_impl._M_node_count;


More information about the Gcc-patches mailing list