[libcxx] r179760 - After years of telling people: 'If you ever find any of my code that self-move-assigns, send me a bug report.' Somebody finally took me up on it. vector::erase(begin(), begin()) does a self-move-assign of every element in the vector, leaving all of those elements in an unspecified state. I checked the other containers for this same bug and did not find it. Added test case.
Howard Hinnant
hhinnant at apple.com
Thu Apr 18 08:02:57 PDT 2013
Author: hhinnant
Date: Thu Apr 18 10:02:57 2013
New Revision: 179760
URL: http://llvm.org/viewvc/llvm-project?rev=179760&view=rev
Log:
After years of telling people: 'If you ever find any of my code that self-move-assigns, send me a bug report.' Somebody finally took me up on it. vector::erase(begin(), begin()) does a self-move-assign of every element in the vector, leaving all of those elements in an unspecified state. I checked the other containers for this same bug and did not find it. Added test case.
Modified:
libcxx/trunk/include/vector
libcxx/trunk/test/containers/sequences/vector/vector.modifiers/erase_iter_iter.pass.cpp
Modified: libcxx/trunk/include/vector
URL: http://llvm.org/viewvc/llvm-project/libcxx/trunk/include/vector?rev=179760&r1=179759&r2=179760&view=diff
==============================================================================
--- libcxx/trunk/include/vector (original)
+++ libcxx/trunk/include/vector Thu Apr 18 10:02:57 2013
@@ -1615,7 +1615,8 @@ vector<_Tp, _Allocator>::erase(const_ite
_LIBCPP_ASSERT(__first <= __last, "vector::erase(first, last) called with invalid range");
pointer __p = this->__begin_ + (__first - begin());
iterator __r = __make_iter(__p);
- this->__destruct_at_end(_VSTD::move(__p + (__last - __first), this->__end_, __p));
+ if (__first != __last)
+ this->__destruct_at_end(_VSTD::move(__p + (__last - __first), this->__end_, __p));
return __r;
}
Modified: libcxx/trunk/test/containers/sequences/vector/vector.modifiers/erase_iter_iter.pass.cpp
URL: http://llvm.org/viewvc/llvm-project/libcxx/trunk/test/containers/sequences/vector/vector.modifiers/erase_iter_iter.pass.cpp?rev=179760&r1=179759&r2=179760&view=diff
==============================================================================
--- libcxx/trunk/test/containers/sequences/vector/vector.modifiers/erase_iter_iter.pass.cpp (original)
+++ libcxx/trunk/test/containers/sequences/vector/vector.modifiers/erase_iter_iter.pass.cpp Thu Apr 18 10:02:57 2013
@@ -47,4 +47,11 @@ int main()
assert(distance(l1.cbegin(), l1.cend()) == 0);
assert(i == l1.begin());
}
+ {
+ std::vector<std::vector<int> > outer(2, std::vector<int>(1));
+ outer.erase(outer.begin(), outer.begin());
+ assert(outer.size() == 2);
+ assert(outer[0].size() == 1);
+ assert(outer[1].size() == 1);
+ }
}
More information about the cfe-commits
mailing list