From: Otto Ebeling Date: Thu, 27 Apr 2017 15:49:50 +0000 (-0700) Subject: Don't return a nullptr from IOBufQueue::split(0) X-Git-Tag: v2017.05.01.00~5 X-Git-Url: http://plrg.eecs.uci.edu/git/?p=folly.git;a=commitdiff_plain;h=af5ed37b6f4898842d55e7db80e74ba845d00393 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 --- diff --git a/folly/io/IOBufQueue.cpp b/folly/io/IOBufQueue.cpp index ce3cb8a5..33c97bc9 100644 --- a/folly/io/IOBufQueue.cpp +++ b/folly/io/IOBufQueue.cpp @@ -212,6 +212,9 @@ unique_ptr IOBufQueue::split(size_t n, bool throwOnUnderflow) { break; } } + if (UNLIKELY(result == nullptr)) { + return IOBuf::create(0); + } return result; } diff --git a/folly/io/test/IOBufQueueTest.cpp b/folly/io/test/IOBufQueueTest.cpp index 8050812e..3b000ea8 100644 --- a/folly/io/test/IOBufQueueTest.cpp +++ b/folly/io/test/IOBufQueueTest.cpp @@ -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"));