Commit 2782b014 authored by Nathan Bronson's avatar Nathan Bronson Committed by Facebook Github Bot

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
parent 77a350d2
...@@ -430,7 +430,9 @@ private: ...@@ -430,7 +430,9 @@ private:
// optimized // optimized
static void S_uninitialized_fill_n(T* dest, size_type n) { static void S_uninitialized_fill_n(T* dest, size_type n) {
if (folly::IsZeroInitializable<T>::value) { if (folly::IsZeroInitializable<T>::value) {
if (LIKELY(n != 0)) {
std::memset(dest, 0, sizeof(T) * n); std::memset(dest, 0, sizeof(T) * n);
}
} else { } else {
auto b = dest; auto b = dest;
auto e = dest + n; auto e = dest + n;
......
...@@ -268,3 +268,17 @@ TEST(FBVector, shrink_to_fit_after_clear) { ...@@ -268,3 +268,17 @@ TEST(FBVector, shrink_to_fit_after_clear) {
EXPECT_EQ(fb1.size(), 0); EXPECT_EQ(fb1.size(), 0);
EXPECT_EQ(fb1.capacity(), 0); EXPECT_EQ(fb1.capacity(), 0);
} }
TEST(FBVector, zero_len) {
fbvector<int> fb1(0);
fbvector<int> fb2(0, 10);
fbvector<int> fb3(std::move(fb1));
fbvector<int> fb4;
fb4 = std::move(fb2);
fbvector<int> fb5 = fb3;
fbvector<int> fb6;
fb6 = fb4;
std::initializer_list<int> il = {};
fb6 = il;
fbvector<int> fb7(fb6.begin(), fb6.end());
}
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment