[llvm] r210426 - Ensure SmallVector::insert doesn't overwrite the last element in the range with the already-moved-from value
David Blaikie
dblaikie at gmail.com
Sun Jun 8 09:00:04 PDT 2014
Author: dblaikie
Date: Sun Jun 8 11:00:02 2014
New Revision: 210426
URL: http://llvm.org/viewvc/llvm-project?rev=210426&view=rev
Log:
Ensure SmallVector::insert doesn't overwrite the last element in the range with the already-moved-from value
This would cause the last element in a range to be in a moved-from state
after an insert at a non-end position, losing that value entirely in the
process.
Side note: move_backward is subtle. It copies [A, B) to C-1 and down.
(the fact that it decrements both the second and third iterators before
the first movement is the subtle part... kind of surprising, anyway)
Modified:
llvm/trunk/include/llvm/ADT/SmallVector.h
llvm/trunk/unittests/ADT/SmallVectorTest.cpp
Modified: llvm/trunk/include/llvm/ADT/SmallVector.h
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/ADT/SmallVector.h?rev=210426&r1=210425&r2=210426&view=diff
==============================================================================
--- llvm/trunk/include/llvm/ADT/SmallVector.h (original)
+++ llvm/trunk/include/llvm/ADT/SmallVector.h Sun Jun 8 11:00:02 2014
@@ -488,9 +488,9 @@ public:
}
::new ((void*) this->end()) T(::std::move(this->back()));
- this->setEnd(this->end()+1);
// Push everything else over.
this->move_backward(I, this->end()-1, this->end());
+ this->setEnd(this->end()+1);
// If we just moved the element we're inserting, be sure to update
// the reference.
Modified: llvm/trunk/unittests/ADT/SmallVectorTest.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/unittests/ADT/SmallVectorTest.cpp?rev=210426&r1=210425&r2=210426&view=diff
==============================================================================
--- llvm/trunk/unittests/ADT/SmallVectorTest.cpp (original)
+++ llvm/trunk/unittests/ADT/SmallVectorTest.cpp Sun Jun 8 11:00:02 2014
@@ -531,4 +531,26 @@ TEST(SmallVectorCustomTest, NoAssignTest
EXPECT_EQ(42, vec.pop_back_val().x);
}
+struct MovedFrom {
+ bool hasValue;
+ MovedFrom() : hasValue(true) {
+ }
+ MovedFrom(MovedFrom&& m) : hasValue(m.hasValue) {
+ m.hasValue = false;
+ }
+ MovedFrom &operator=(MovedFrom&& m) {
+ hasValue = m.hasValue;
+ m.hasValue = false;
+ return *this;
+ }
+};
+
+TEST(SmallVectorTest, MidInsert) {
+ SmallVector<MovedFrom, 3> v;
+ v.push_back(MovedFrom());
+ v.insert(v.begin(), MovedFrom());
+ for (MovedFrom &m : v)
+ EXPECT_TRUE(m.hasValue);
+}
+
}
More information about the llvm-commits
mailing list