mirror of https://git.sr.ht/~stygianentity/bincode
parent
9ad558de5f
commit
ee0dccbd02
|
|
@ -335,7 +335,15 @@ impl<'a, R: Read> Decoder for DecoderReader<'a, R> {
|
||||||
fn read_seq<T, F>(&mut self, f: F) -> DecodingResult<T>
|
fn read_seq<T, F>(&mut self, f: F) -> DecodingResult<T>
|
||||||
where F: FnOnce(&mut DecoderReader<'a, R>, usize) -> DecodingResult<T>
|
where F: FnOnce(&mut DecoderReader<'a, R>, usize) -> DecodingResult<T>
|
||||||
{
|
{
|
||||||
|
use std::mem::size_of;
|
||||||
|
use std::usize;
|
||||||
let len = try!(self.read_usize());
|
let len = try!(self.read_usize());
|
||||||
|
if let SizeLimit::Bounded(x) = self.size_limit {
|
||||||
|
if (len > usize::MAX / size_of::<T>()) ||
|
||||||
|
(len * size_of::<T>()) as u64 > (x - self.read) {
|
||||||
|
return Err(DecodingError::SizeLimit)
|
||||||
|
}
|
||||||
|
}
|
||||||
f(self, len)
|
f(self, len)
|
||||||
}
|
}
|
||||||
fn read_seq_elt<T, F>(&mut self, _: usize, f: F) -> DecodingResult<T>
|
fn read_seq_elt<T, F>(&mut self, _: usize, f: F) -> DecodingResult<T>
|
||||||
|
|
|
||||||
18
src/test.rs
18
src/test.rs
|
|
@ -319,3 +319,21 @@ fn test_slicebox() {
|
||||||
fn test_multi_strings() {
|
fn test_multi_strings() {
|
||||||
assert!(encode(&("foo", "bar", "baz"), Infinite).is_ok());
|
assert!(encode(&("foo", "bar", "baz"), Infinite).is_ok());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn no_oom() {
|
||||||
|
use std::io::Cursor;
|
||||||
|
|
||||||
|
#[derive(RustcEncodable)]
|
||||||
|
struct FakeVec {
|
||||||
|
len: u64,
|
||||||
|
byte: u8
|
||||||
|
}
|
||||||
|
|
||||||
|
let x = encode(&FakeVec { len: 0xffffffffffffffffu64, byte: 1 }, Bounded(10)).unwrap();
|
||||||
|
let y : Result<Vec<u8>, _> = decode_from(&mut Cursor::new(&x[..]), Bounded(10));
|
||||||
|
match y {
|
||||||
|
Err(DecodingError::SizeLimit) => assert!(true),
|
||||||
|
_ => assert!(false)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue