matttproud/golang_protobuf_extensions — pull request #4 by u-c-l (2014-07-11 18:50:04+00:00)
Title: Fix two more bugs.

Unfortunately, two more subtle bugs to fix:

- If the encoded protobuf message is only three bytes long (or less),
not only the whole protobuf will fit into headerBuf, but also the
beginning of the next length varint. This will completely wreak
havoc in the read process. (This bug never bit us in Prometheus
because the protobuf messages were all long enough. But protobufs in
general can be very small (0 bytes in fact). I added a test for
that.)

- If the reader stops in the middle of a length varint, it will
interpret the null byte that is already in headerBuf as the final
byte of the varint, which again wreaks havoc. (This bug only shows
up in Prometheus after
https://github.com/prometheus/prometheus/commit/dfb9c2a072d5c1003e78c4921988c21bc76ab07a
, and it is very rare... needs very unlucky fragmentation on the
network.)

Solution to the first bug is to read byte-wise, which is slightly less
effective, but simplifies the code later, because we don't "overshoot"
anymore. Solution to the second bug is to only read the parts of
headerBuf that have been written to.