fixup decode logic for fragmented IOBufs
[folly.git] / folly / experimental / bser / Load.cpp
index 2894e901c1d9cd811c6d5c2db4c684b797a533e3..ad390ee8a866f7405798ba076c0d50d430c62bae 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright 2017 Facebook, Inc.
+ * Copyright 2016-present Facebook, Inc.
  *
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
@@ -61,7 +61,10 @@ static std::string decodeString(Cursor& curs) {
   }
   str.reserve(size_t(len));
 
-  size_t available = curs.length();
+  // peekBytes will advance over any "empty" IOBuf elements until
+  // it reaches the next one with data, so do that to obtain the
+  // true remaining length.
+  size_t available = curs.peekBytes().size();
   while (available < (size_t)len) {
     if (available == 0) {
       // Saw this case when we decodeHeader was returning the incorrect length
@@ -74,7 +77,7 @@ static std::string decodeString(Cursor& curs) {
     str.append(reinterpret_cast<const char*>(curs.data()), available);
     curs.skipAtMost(available);
     len -= available;
-    available = curs.length();
+    available = curs.peekBytes().size();
   }
 
   str.append(reinterpret_cast<const char*>(curs.data()), size_t(len));