Commit d0683981 authored by Kyle Nekritz's avatar Kyle Nekritz Committed by Facebook Github Bot

Don't call memcpy with empty buffer in IOBuf::copyBuffer().

Summary: Fixes "undefined behavior" in ubsan builds when using an empty ByteRange.

Reviewed By: siyengar

Differential Revision: D4613663

fbshipit-source-id: 4b53ba764609acc986340f10613f84585fa697cf
parent da08e63c
......@@ -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;
}
......
......@@ -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;
......
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