[PATCH] D93781: ADT: Fix reference invalidation in SmallVector::resize
Duncan P. N. Exon Smith via Phabricator via llvm-commits
llvm-commits at lists.llvm.org
Wed Dec 23 14:05:43 PST 2020
dexonsmith created this revision.
dexonsmith added a reviewer: dblaikie.
Herald added a subscriber: ributzka.
dexonsmith requested review of this revision.
Herald added a project: LLVM.
For small enough, trivially copyable `T`, take the parameter by-value in
`SmallVector::resize`. Otherwise, when growing, update the arugment
appropriately.
Depends on https://reviews.llvm.org/D93780.
Split out from https://reviews.llvm.org/D91837.
Repository:
rG LLVM Github Monorepo
https://reviews.llvm.org/D93781
Files:
llvm/include/llvm/ADT/SmallVector.h
llvm/unittests/ADT/SmallVectorTest.cpp
Index: llvm/unittests/ADT/SmallVectorTest.cpp
===================================================================
--- llvm/unittests/ADT/SmallVectorTest.cpp
+++ llvm/unittests/ADT/SmallVectorTest.cpp
@@ -1102,12 +1102,14 @@
auto &V = this->V;
(void)V;
int N = this->NumBuiltinElts(V);
-#if !defined(NDEBUG) && GTEST_HAS_DEATH_TEST
- EXPECT_DEATH(V.resize(N + 1, V.back()), this->AssertionMessage);
-#endif
+ V.resize(N + 1, V.back());
+ EXPECT_EQ(N, V.back());
- // No assertion when shrinking, since the parameter isn't accessed.
- V.resize(N - 1, V.back());
+ // Resize to add enough elements that V will grow again. If reference
+ // invalidation breaks in the future, sanitizers should be able to catch a
+ // use-after-free here.
+ V.resize(V.capacity() + 1, V.front());
+ EXPECT_EQ(1, V.back());
}
TYPED_TEST(SmallVectorReferenceInvalidationTest, Append) {
Index: llvm/include/llvm/ADT/SmallVector.h
===================================================================
--- llvm/include/llvm/ADT/SmallVector.h
+++ llvm/include/llvm/ADT/SmallVector.h
@@ -559,7 +559,7 @@
/// Like resize, but \ref T is POD, the new values won't be initialized.
void resize_for_overwrite(size_type N) { resizeImpl<true>(N); }
- void resize(size_type N, const T &NV) {
+ void resize(size_type N, ValueParamT NV) {
if (N == this->size())
return;
@@ -569,11 +569,8 @@
return;
}
- this->assertSafeToReferenceAfterResize(&NV, N);
- if (this->capacity() < N)
- this->grow(N);
- std::uninitialized_fill(this->end(), this->begin() + N, NV);
- this->set_size(N);
+ // N > this->size(). Defer to append.
+ this->append(N - this->size(), NV);
}
void reserve(size_type N) {
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D93781.313614.patch
Type: text/x-patch
Size: 1741 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20201223/46fe674f/attachment.bin>
More information about the llvm-commits
mailing list