[cfe-dev] libc++ std::map self-assignment bug

Howard Hinnant hhinnant at apple.com
Thu Nov 28 19:17:27 PST 2013


On Nov 28, 2013, at 9:58 PM, Karen Shaeffer <shaeffer at neuralscape.com> wrote:

> On Fri, Nov 29, 2013 at 02:19:44AM +0100, Kal wrote:
>> Hi Howard, etc,
>> 
>> libc++3.4 (rc1/trunk) std::map doesn't support self-assignment for std <
>> c++11. The relevant code is:
>> 
>>    _LIBCPP_INLINE_VISIBILITY
>>    map& operator=(const map& __m)
>>        {
>> #if __cplusplus >= 201103L
>>            __tree_ = __m.__tree_;
>> #else
>>            __tree_.clear();
>>            __tree_.value_comp() = __m.__tree_.value_comp();
>>            __tree_.__copy_assign_alloc(__m.__tree_);
>>            insert(__m.begin(), __m.end());
>> #endif
>>            return *this;
>>        }
>> 
>> Maybe should be like:
>> 
>>    _LIBCPP_INLINE_VISIBILITY
>>    map& operator=(const map& __m)
>>        {
>> #if __cplusplus >= 201103L
>>            __tree_ = __m.__tree_;
>> #else
>>            if (this != &__m) {
>>                __tree_.clear();
>>                __tree_.value_comp() = __m.__tree_.value_comp();
>>                __tree_.__copy_assign_alloc(__m.__tree_);
>>                insert(__m.begin(), __m.end());
>>            }
>> #endif
>>            return *this;
>>        }
>> 
>> I see the same issue with unordered_map& operator=(const unordered_map&
>> __u).
>> 
>> Thanks!
>> Kal
> --- end quoted text ---
> 
> Hi Kal,
> I don't speak for Howard or anyone else. But my understanding is the stl library
> generally doesn't perform checks like that with the goal of best possible performance.
> I see a lot of code in stdlibc++ just like that.

Actually this does look like a bug to me (for  __cplusplus < 201103L).  I think Kal has the correct fix.  A post-condition of copy assignment is that both lhs and rhs should be equivalent to the previous value of rhs.  For move assignment this is not the case.  But this is copy assignment.

Howard




More information about the cfe-dev mailing list