From 2782b0144f937fc9c6df0a9b21bd4e1c1abbf678 Mon Sep 17 00:00:00 2001 From: Nathan Bronson Date: Wed, 16 Nov 2016 14:01:49 -0800 Subject: [PATCH] folly: fbvector: ubsan: avoid memset(nullptr, 0, 0) Summary: Constructing an empty FBVector by length results in a call to memset with a null destination, which is undefined behavior. This diff fixes it. Reviewed By: luciang, meyering, Gownta Differential Revision: D4191612 fbshipit-source-id: 3dcc091396fc33ac2230bd2d90906325131b0a3b --- folly/FBVector.h | 4 +++- folly/test/FBVectorTest.cpp | 14 ++++++++++++++ 2 files changed, 17 insertions(+), 1 deletion(-) diff --git a/folly/FBVector.h b/folly/FBVector.h index 6c6de44e..0979f883 100644 --- a/folly/FBVector.h +++ b/folly/FBVector.h @@ -430,7 +430,9 @@ private: // optimized static void S_uninitialized_fill_n(T* dest, size_type n) { if (folly::IsZeroInitializable::value) { - std::memset(dest, 0, sizeof(T) * n); + if (LIKELY(n != 0)) { + std::memset(dest, 0, sizeof(T) * n); + } } else { auto b = dest; auto e = dest + n; diff --git a/folly/test/FBVectorTest.cpp b/folly/test/FBVectorTest.cpp index 54f64efe..e4bfe89b 100644 --- a/folly/test/FBVectorTest.cpp +++ b/folly/test/FBVectorTest.cpp @@ -268,3 +268,17 @@ TEST(FBVector, shrink_to_fit_after_clear) { EXPECT_EQ(fb1.size(), 0); EXPECT_EQ(fb1.capacity(), 0); } + +TEST(FBVector, zero_len) { + fbvector fb1(0); + fbvector fb2(0, 10); + fbvector fb3(std::move(fb1)); + fbvector fb4; + fb4 = std::move(fb2); + fbvector fb5 = fb3; + fbvector fb6; + fb6 = fb4; + std::initializer_list il = {}; + fb6 = il; + fbvector fb7(fb6.begin(), fb6.end()); +} -- 2.34.1