Commit a578696f authored by Wez Furlong's avatar Wez Furlong Committed by Facebook Github Bot

reuse more of Cursor to avoid future issues

Summary:
Following on from D6755842; we don't have to repeat as
much of the internals of `readFixedString`, so... don't!

Reviewed By: yfeldblum

Differential Revision: D6756062

fbshipit-source-id: db3e4fd62e48bf155a656ee57df84274021027f7
parent 93f4d05d
...@@ -59,29 +59,24 @@ static std::string decodeString(Cursor& curs) { ...@@ -59,29 +59,24 @@ static std::string decodeString(Cursor& curs) {
if (len < 0) { if (len < 0) {
throw std::range_error("string length must not be negative"); throw std::range_error("string length must not be negative");
} }
str.reserve(size_t(len));
// We could use Cursor::readFixedString() here, but we'd like
// peekBytes will advance over any "empty" IOBuf elements until // to throw our own exception with some increased diagnostics.
// it reaches the next one with data, so do that to obtain the str.resize(len);
// true remaining length.
size_t available = curs.peekBytes().size(); // The start of the string data, mutable.
while (available < (size_t)len) { auto* dest = &str[0];
if (available == 0) {
// Saw this case when we decodeHeader was returning the incorrect length auto pulled = curs.pullAtMost(dest, len);
// and we were splitting off too few bytes from the IOBufQueue if (pulled != size_t(len)) {
throwDecodeError( // Saw this case when decodeHeader was returning the incorrect length
curs, // and we were splitting off too few bytes from the IOBufQueue
"no data available while decoding a string, header was " throwDecodeError(
"not decoded properly"); curs,
} "no data available while decoding a string, header was "
str.append(reinterpret_cast<const char*>(curs.data()), available); "not decoded properly");
curs.skipAtMost(available);
len -= available;
available = curs.peekBytes().size();
} }
str.append(reinterpret_cast<const char*>(curs.data()), size_t(len));
curs.skipAtMost(size_t(len));
return str; return str;
} }
......
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