Don't call memcpy with empty buffer in IOBuf::copyBuffer().
authorKyle Nekritz <knekritz@fb.com>
Mon, 27 Feb 2017 18:48:39 +0000 (10:48 -0800)
committerFacebook Github Bot <facebook-github-bot@users.noreply.github.com>
Mon, 27 Feb 2017 18:52:07 +0000 (10:52 -0800)
Summary: Fixes "undefined behavior" in ubsan builds when using an empty ByteRange.

Reviewed By: siyengar

Differential Revision: D4613663

fbshipit-source-id: 4b53ba764609acc986340f10613f84585fa697cf

folly/io/IOBuf.h
folly/io/test/IOBufTest.cpp

index 621d39334c2825656054efe185c5d8f53fb2d07c..87a3ec9e78100680e9a518cca537a6f7c7760b51 100644 (file)
@@ -1439,7 +1439,9 @@ inline std::unique_ptr<IOBuf> IOBuf::copyBuffer(
   uint64_t capacity = headroom + size + minTailroom;
   std::unique_ptr<IOBuf> buf = create(capacity);
   buf->advance(headroom);
-  memcpy(buf->writableData(), data, size);
+  if (size != 0) {
+    memcpy(buf->writableData(), data, size);
+  }
   buf->append(size);
   return buf;
 }
index 85b9dcb36cad690ce84339f14a94607b5596bdf5..6e0cf4418494bcce09084ff5a83a85f1e38786b8 100644 (file)
@@ -717,6 +717,11 @@ TEST(IOBuf, maybeCopyBuffer) {
   EXPECT_EQ(nullptr, buf.get());
 }
 
+TEST(IOBuf, copyEmptyBuffer) {
+  auto buf = IOBuf::copyBuffer(nullptr, 0);
+  EXPECT_EQ(buf->length(), 0);
+}
+
 namespace {
 
 int customDeleterCount = 0;