This is the mail archive of the libstdc++@sourceware.cygnus.com mailing list for the libstdc++ project. See the libstdc++ home page for more information.


[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index] [Subject Index] [Author Index] [Thread Index]

auto_ptr::reset patch



The auto_ptr::reset member function does not check the equality of the
function parameter and the owned pointer:

  void reset(_Tp* __p = 0) __STL_NOTHROW {
    delete _M_ptr;
    _M_ptr = __p;
  }

That is wrong, it can cause errors if __p == _M_ptr != 0.

The standard requirements for the operator= are:

          auto_ptr& operator=(auto_ptr& a) throw();

     Requires:  The expression delete get() is well formed.
     Effects:  reset(a.release()).
     Returns:  *this.

Ryszard Kabatek




1999-01-29  Ryszard Kabatek  <kabatek@chemie.uni-halle.de>

        * stl/bits/std_memory.h:  Modify auto_ptr::reset to reflect the
standard.



*** stl/bits/std_memory.h	Fri Jan  1 04:14:56 1999
--- std_memory.h	Fri Jan 29 11:41:33 1999
*************** public:
*** 38,55 ****
    template <class _Tp1> auto_ptr(auto_ptr<_Tp1>& __a) __STL_NOTHROW
      : _M_ptr(__a.release()) {}
    auto_ptr& operator=(auto_ptr& __a) __STL_NOTHROW {
!     if (&__a != this) {
!       delete _M_ptr;
!       _M_ptr = __a.release();
!     }
      return *this;
    }
    template <class _Tp1>
    auto_ptr& operator=(auto_ptr<_Tp1>& __a) __STL_NOTHROW {
!     if (__a.get() != this->get()) {
!       delete _M_ptr;
!       _M_ptr = __a.release();
!     }
      return *this;
    }
    ~auto_ptr() __STL_NOTHROW { delete _M_ptr; }
--- 38,49 ----
    template <class _Tp1> auto_ptr(auto_ptr<_Tp1>& __a) __STL_NOTHROW
      : _M_ptr(__a.release()) {}
    auto_ptr& operator=(auto_ptr& __a) __STL_NOTHROW {
!     reset(__a.release());
      return *this;
    }
    template <class _Tp1>
    auto_ptr& operator=(auto_ptr<_Tp1>& __a) __STL_NOTHROW {
!     reset(__a.release());
      return *this;
    }
    ~auto_ptr() __STL_NOTHROW { delete _M_ptr; }
*************** public:
*** 69,76 ****
      return __tmp;
    }
    void reset(_Tp* __p = 0) __STL_NOTHROW {
!     delete _M_ptr;
!     _M_ptr = __p;
    }
  
    // According to the C++ standard, these conversions are required. 
Most
--- 63,72 ----
      return __tmp;
    }
    void reset(_Tp* __p = 0) __STL_NOTHROW {
!     if (__p != _M_ptr) {
!       delete _M_ptr;
!       _M_ptr = __p;
!     }
    }
  
    // According to the C++ standard, these conversions are required. 
Most