From: Philip Pronin Date: Tue, 4 Dec 2012 05:51:49 +0000 (-0800) Subject: fix GroupVarintDecoder::rest() X-Git-Tag: v0.22.0~1118 X-Git-Url: http://demsky.eecs.uci.edu/git/?a=commitdiff_plain;h=ec4962764d97595a2c468f4342ab8c867517f6f4;p=folly.git fix GroupVarintDecoder::rest() Summary: It makes sense to return subpiece of the original data from ##rest()##. Test Plan: gv tests Reviewed By: soren@fb.com FB internal diff: D647179 --- diff --git a/folly/GroupVarint.h b/folly/GroupVarint.h index 6a3e4709..0ffae3a6 100644 --- a/folly/GroupVarint.h +++ b/folly/GroupVarint.h @@ -507,16 +507,18 @@ class GroupVarintDecoder { explicit GroupVarintDecoder(StringPiece data, size_t maxCount = (size_t)-1) - : p_(data.data()), - end_(data.data() + data.size()), + : rrest_(data.end()), + p_(data.data()), + end_(data.end()), pos_(0), count_(0), remaining_(maxCount) { } void reset(StringPiece data, size_t maxCount=(size_t)-1) { + rrest_ = data.end(); p_ = data.data(); - end_ = data.data() + data.size(); + end_ = data.end(); pos_ = 0; count_ = 0; remaining_ = maxCount; @@ -580,10 +582,14 @@ class GroupVarintDecoder { StringPiece rest() const { // This is only valid after next() returned false CHECK(pos_ == count_ && (p_ == end_ || remaining_ == 0)); - return StringPiece(p_, end_ - p_); + // p_ may point to the internal buffer (tmp_), but we want + // to return subpiece of the original data + size_t size = end_ - p_; + return StringPiece(rrest_ - size, rrest_); } private: + const char* rrest_; const char* p_; const char* end_; char tmp_[Base::kMaxSize];