Don't return a nullptr from IOBufQueue::split(0)
authorOtto Ebeling <otto@fb.com>
Thu, 27 Apr 2017 15:49:50 +0000 (08:49 -0700)
committerFacebook Github Bot <facebook-github-bot@users.noreply.github.com>
Thu, 27 Apr 2017 15:51:53 +0000 (08:51 -0700)
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
folly/io/test/IOBufQueueTest.cpp

index ce3cb8a5ae06b1155f8624f19157f61dd521f291..33c97bc97007eedd23a583cf6226b6d5aacc57ef 100644 (file)
@@ -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;
 }
 
index 8050812e229fba23148b95a7b92e955c22a4d156..3b000ea8cb3f8f920be984dedbf7fb7c7bc1d1a6 100644 (file)
@@ -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"));