From c78e8cf45e441501431aeaf29a342e07c0628fb8 Mon Sep 17 00:00:00 2001 From: Christian Kamm Date: Mon, 25 Mar 2013 09:54:26 +0100 Subject: [PATCH] Fix bug in reserve() and shrink_to_fit(). Summary: impl_.e_ += newB - impl_.b_; fails when the difference between newB and impl_.b_ isn't a multiple of sizeof(T). Test Plan: . Reviewed By: oyamauchi@fb.com FB internal diff: D774754 --- folly/FBVector.h | 4 ++-- folly/test/FBVectorTest.cpp | 11 +++++++++++ 2 files changed, 13 insertions(+), 2 deletions(-) diff --git a/folly/FBVector.h b/folly/FBVector.h index fbae7e2c..9e593c60 100644 --- a/folly/FBVector.h +++ b/folly/FBVector.h @@ -1095,7 +1095,7 @@ public: if (impl_.b_) M_deallocate(impl_.b_, impl_.z_ - impl_.b_); impl_.z_ = newB + newCap; - impl_.e_ += newB - impl_.b_; // speed hax + impl_.e_ = newB + (impl_.e_ - impl_.b_); impl_.b_ = newB; } @@ -1128,7 +1128,7 @@ public: if (impl_.b_) M_deallocate(impl_.b_, impl_.z_ - impl_.b_); impl_.z_ = newB + newCap; - impl_.e_ += newB - impl_.b_; // speed hax + impl_.e_ = newB + (impl_.e_ - impl_.b_); impl_.b_ = newB; } } diff --git a/folly/test/FBVectorTest.cpp b/folly/test/FBVectorTest.cpp index 68380279..a8a1b2ac 100644 --- a/folly/test/FBVectorTest.cpp +++ b/folly/test/FBVectorTest.cpp @@ -245,6 +245,17 @@ TEST(FBVector, move_iterator) { EXPECT_EQ(fbvi3, base); } +TEST(FBVector, reserve_consistency) { + struct S { int64_t a, b, c, d; }; + + fbvector fb1; + for (size_t i = 0; i < 1000; ++i) { + fb1.reserve(1); + EXPECT_EQ(fb1.size(), 0); + fb1.shrink_to_fit(); + } +} + int main(int argc, char** argv) { testing::InitGoogleTest(&argc, argv); google::ParseCommandLineFlags(&argc, &argv, true); -- 2.34.1