From ffe26edc53bc90a14078c66e46764ea3e6a81df6 Mon Sep 17 00:00:00 2001 From: Adrien Conrath Date: Wed, 25 Nov 2015 08:34:09 -0800 Subject: [PATCH] Remove undefined behavior in goodMallocSize() Reviewed By: ot Differential Revision: D2695741 fb-gh-sync-id: d33263c1ffa5651d66f37992ce365dae157ed449 --- folly/Malloc.h | 4 ++++ folly/test/FBVectorTest.cpp | 10 ++++++++++ 2 files changed, 14 insertions(+) diff --git a/folly/Malloc.h b/folly/Malloc.h index f8398911..828c1b65 100644 --- a/folly/Malloc.h +++ b/folly/Malloc.h @@ -177,6 +177,10 @@ inline bool usingJEMalloc() noexcept { } inline size_t goodMallocSize(size_t minSize) noexcept { + if (minSize == 0) { + return 0; + } + if (!usingJEMalloc()) { // Not using jemalloc - no smarts return minSize; diff --git a/folly/test/FBVectorTest.cpp b/folly/test/FBVectorTest.cpp index 330dc4bb..ae845834 100644 --- a/folly/test/FBVectorTest.cpp +++ b/folly/test/FBVectorTest.cpp @@ -274,6 +274,16 @@ TEST(FBVector, vector_of_maps) { EXPECT_EQ(1, v[1].size()); } +TEST(FBVector, shrink_to_fit_after_clear) { + fbvector fb1; + fb1.push_back(42); + fb1.push_back(1337); + fb1.clear(); + fb1.shrink_to_fit(); + EXPECT_EQ(fb1.size(), 0); + EXPECT_EQ(fb1.capacity(), 0); +} + int main(int argc, char** argv) { testing::InitGoogleTest(&argc, argv); gflags::ParseCommandLineFlags(&argc, &argv, true); -- 2.34.1