fail with size limit on large maps too

This commit is contained in:
Ty Overby 2015-05-28 16:12:46 -07:00
parent 1c3f457cf9
commit ce286b2519
2 changed files with 23 additions and 4 deletions

View File

@ -354,7 +354,15 @@ impl<'a, R: Read> Decoder for DecoderReader<'a, R> {
fn read_map<T, F>(&mut self, f: F) -> 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());
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)
}
fn read_map_elt_key<T, F>(&mut self, _: usize, f: F) -> DecodingResult<T>

View File

@ -335,9 +335,20 @@ fn no_oom() {
}
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)
{
let y : Result<Vec<u8>, _> = decode_from(&mut Cursor::new(&x[..]), Bounded(10));
match y {
Err(DecodingError::SizeLimit) => assert!(true),
_ => assert!(false)
}
}
{
let y : Result<HashMap<u8, u8>, _> = decode_from(&mut Cursor::new(&x[..]), Bounded(10));
match y {
Err(DecodingError::SizeLimit) => assert!(true),
_ => assert!(false)
}
}
}