diff --git a/mcrouter/lib/fbi/cpp/LowerBoundPrefixMap.h b/mcrouter/lib/fbi/cpp/LowerBoundPrefixMap.h index ffa887be5..d039296f2 100644 --- a/mcrouter/lib/fbi/cpp/LowerBoundPrefixMap.h +++ b/mcrouter/lib/fbi/cpp/LowerBoundPrefixMap.h @@ -40,8 +40,10 @@ class SmallPrefix { data_ = folly::Endian::swap(data_); return; } - std::memcpy(&data_, s.data(), s.size()); - data_ = folly::Endian::swap(data_); + if (FOLLY_LIKELY(s.data() != nullptr)) { + std::memcpy(&data_, s.data(), s.size()); + data_ = folly::Endian::swap(data_); + } } // default operator== and operator<=> are ok here diff --git a/mcrouter/lib/fbi/cpp/test/LowerBoundPrefixMapTest.cpp b/mcrouter/lib/fbi/cpp/test/LowerBoundPrefixMapTest.cpp index 6626a9a6d..31821da0d 100644 --- a/mcrouter/lib/fbi/cpp/test/LowerBoundPrefixMapTest.cpp +++ b/mcrouter/lib/fbi/cpp/test/LowerBoundPrefixMapTest.cpp @@ -205,5 +205,10 @@ TEST(LowerBoundPrefixMapTest, EmptyMap) { ASSERT_TRUE(lbMap.empty()); } +TEST(LowerBoundPrefixMapTest, NullKey) { + LowerBoundPrefixMap lbMap; + ASSERT_EQ(lbMap.findPrefix(std::string_view{}), lbMap.end()); +} + } // namespace } // namespace facebook::memcache