-
Notifications
You must be signed in to change notification settings - Fork 16
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Silent downcast overflow #21
Comments
Yes this is an unsatisfactory state. However, with an unterminated varint error, there would be a fragment being left in the input stream which will give a nonsense number on next read (at least as nonsensical as a truncated int). Now one could write documentation that the next integer after such an error must be ignored... but that also doesn't seem optimal to me. In any case, I'm open for debate on this topic. |
I think if your read / deserialize data from a stream and get an error, you have to stop reading or re-synchronize the stream (the latter one is mostly only possible for low-level network protocols). I think a user cannot expect a parser to automatically recover from broken data, because there is no way to know which part was broken (in the example case above was it one byte that was broken or was a 32bit value serialized as 64bit? or did we mess up before reading the varint?). It's not only about the "next integer" btw. because likely the protocol the user is trying to deserialize consists of other data types before and after the varint in question. With your argument, I think you should NEVER return an "unterminated varint error" because you could always argue that there could in theory be a 128bit, 256bit, ... wide varint. |
That's a good point, I will look into it. |
@crepererum would you mind reviewing the change in #22? |
I believe that this issue can be closed with the fix implemented. |
When using
VarintReader
with types smaller than 64bits, the current implementation first reads a 64bit integer:integer-encoding-rs/src/reader.rs
Lines 40 to 58 in 14cd007
and then casts it to a smaller integer:
integer-encoding-rs/src/varint.rs
Lines 74 to 77 in 14cd007
integer-encoding-rs/src/varint.rs
Lines 90 to 93 in 14cd007
Now imagine a data stream w/ 9 bits
0xff
followed by0x00
, which would be a pretty large 64bit integer. If you useVarIntReader::read_varint::<i32>(...)
to decode that, it will sucessfully read the 64bit varint, consume all the bytes and during the conversion to 32bit will silently truncate the result. What it should do instead is to read only the number of bytes that are at max required for 32bit (5?) and then fail with"Unterminated varint"
.The text was updated successfully, but these errors were encountered: