avoid UB in StringKeyedCommon for default constructed StringPiece
[folly.git] / folly / experimental / StringKeyedCommon.h
index c79d7b7c7078508103904a4a91b27a8c10bb27bc..e29d2c1bacedf1ffca18833e6ecc4ed981dcc9fa 100644 (file)
@@ -28,7 +28,10 @@ StringPiece stringPieceDup(StringPiece piece, const Alloc& alloc) {
   auto size = piece.size();
   auto keyDup = typename Alloc::template rebind<char>::other(alloc)
     .allocate(size);
-  memcpy(keyDup, piece.data(), size * sizeof(StringPiece::value_type));
+  if (size) {
+    memcpy(
+        keyDup, piece.data(), size * sizeof(typename StringPiece::value_type));
+  }
   return StringPiece(keyDup, size);
 }