dermesser / integer-encoding-rs

Integer encoding for primitive integer types: Supports varint/varint+zigzag and fixed-length integer encoding and decoding, and provides synchronous and asynchronous Write/Read types for easily writing/reading integers.
Other
66 stars 16 forks source link

Fix VarInt decoding with extra bytes at the end #13

Closed jamesbornholt closed 4 years ago

jamesbornholt commented 4 years ago

After #10, decoding a VarInt from a slice can fail if the final byte of the slice has its MSB set, even if the VarInt decoding will never make it that far. This is an issue if we're decoding from a larger slice that has other encoded data after the VarInt.

It's not enough to check the last byte of the slice for the MSB being set -- we have to scan all the bytes and try to decode them, and only fail decoding if we never saw the MSB set in any byte.

jamesbornholt commented 4 years ago

I realized I forgot to make the same change for i64, which is done now.

I also added another test for overflow (encodings that are too long to produce a valid u64/i64), which exposed an off-by-one in the conditional: shift > (10 * 7) should be shift > (9 * 7) to avoid trying to do a shift by > 64 bits.