From: Adrien Conrath Date: Wed, 25 Nov 2015 16:34:09 +0000 (-0800) Subject: Remove undefined behavior in goodMallocSize() X-Git-Tag: deprecate-dynamic-initializer~231 X-Git-Url: http://demsky.eecs.uci.edu/git/?a=commitdiff_plain;h=ffe26edc53bc90a14078c66e46764ea3e6a81df6;p=folly.git Remove undefined behavior in goodMallocSize() Reviewed By: ot Differential Revision: D2695741 fb-gh-sync-id: d33263c1ffa5651d66f37992ce365dae157ed449 --- 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);