Commit af5ed37b authored by Otto Ebeling's avatar Otto Ebeling Committed by Facebook Github Bot

Don't return a nullptr from IOBufQueue::split(0)

Summary: There's a gotcha case for IOBufQueue::split when  n==0, it will then return an unique_ptr wrapping a nullptr, which many call sites do not expect.

Reviewed By: meyering

Differential Revision: D4868228

fbshipit-source-id: 418256dba8ca3bcfbae420b6099baa240055b9bb
parent b73507bb
......@@ -212,6 +212,9 @@ unique_ptr<IOBuf> IOBufQueue::split(size_t n, bool throwOnUnderflow) {
break;
}
}
if (UNLIKELY(result == nullptr)) {
return IOBuf::create(0);
}
return result;
}
......
......@@ -167,6 +167,13 @@ TEST(IOBufQueue, SplitAtMost) {
EXPECT_TRUE(queue.empty());
}
TEST(IOBufQueue, SplitZero) {
IOBufQueue queue(clOptions);
queue.append(stringToIOBuf(SCL("Hello world")));
auto buf = queue.split(0);
EXPECT_EQ(buf->computeChainDataLength(), 0);
}
TEST(IOBufQueue, Preallocate) {
IOBufQueue queue(clOptions);
queue.append(string("Hello"));
......
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