From af5ed37b6f4898842d55e7db80e74ba845d00393 Mon Sep 17 00:00:00 2001 From: Otto Ebeling Date: Thu, 27 Apr 2017 08:49:50 -0700 Subject: [PATCH] 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 --- folly/io/IOBufQueue.cpp | 3 +++ folly/io/test/IOBufQueueTest.cpp | 7 +++++++ 2 files changed, 10 insertions(+) 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")); -- 2.34.1