diff --git a/llvm/include/llvm/ADT/SmallVector.h b/llvm/include/llvm/ADT/SmallVector.h --- a/llvm/include/llvm/ADT/SmallVector.h +++ b/llvm/include/llvm/ADT/SmallVector.h @@ -462,11 +462,11 @@ } void resize(size_type N, const T &NV) { - this->assertSafeToReferenceAfterResize(&NV, N); if (N < this->size()) { this->destroy_range(this->begin()+N, this->end()); this->set_size(N); } else if (N > this->size()) { + this->assertSafeToReferenceAfterResize(&NV, N); if (this->capacity() < N) this->grow(N); std::uninitialized_fill(this->end(), this->begin()+N, NV); diff --git a/llvm/unittests/ADT/SmallVectorTest.cpp b/llvm/unittests/ADT/SmallVectorTest.cpp --- a/llvm/unittests/ADT/SmallVectorTest.cpp +++ b/llvm/unittests/ADT/SmallVectorTest.cpp @@ -1051,9 +1051,13 @@ TYPED_TEST(SmallVectorReferenceInvalidationTest, Resize) { auto &V = this->V; (void)V; + int N = this->NumBuiltinElts(V); #if !defined(NDEBUG) && GTEST_HAS_DEATH_TEST - EXPECT_DEATH(V.resize(2, V.back()), this->AssertionMessage); + EXPECT_DEATH(V.resize(N + 1, V.back()), this->AssertionMessage); #endif + + // No assertion when shrinking, since the parameter isn't accessed. + V.resize(N - 1, V.back()); } TYPED_TEST(SmallVectorReferenceInvalidationTest, Append) {