Trait bincode::SizeLimit
[−]
[src]
pub trait SizeLimit { fn add(&mut self, n: u64) -> Result<()>; fn limit(&self) -> Option<u64>; }
A limit on the amount of bytes that can be read or written.
Size limits are an incredibly important part of both encoding and decoding.
In order to prevent DOS attacks on a decoder, it is important to limit the amount of bytes that a single encoded message can be; otherwise, if you are decoding bytes right off of a TCP stream for example, it would be possible for an attacker to flood your server with a 3TB vec, causing the decoder to run out of memory and crash your application! Because of this, you can provide a maximum-number-of-bytes that can be read during decoding, and the decoder will explicitly fail if it has to read any more than that.
On the other side, you want to make sure that you aren't encoding a message that is larger than your decoder expects. By supplying a size limit to an encoding function, the encoder will verify that the structure can be encoded within that limit. This verification occurs before any bytes are written to the Writer, so recovering from an error is easy.
Required Methods
fn add(&mut self, n: u64) -> Result<()>
Tells the SizeLimit that a certain number of bytes has been read or written. Returns Err if the limit has been exceeded.
fn limit(&self) -> Option<u64>
Returns the hard limit (if one exists)