long length checks & indentation

This commit is contained in:
Nikolay Volf 2016-01-25 17:27:11 +04:00
parent e592a185ef
commit 01ea703783
2 changed files with 24 additions and 2 deletions

View File

@ -361,3 +361,17 @@ fn test_rlp_data_length_check()
let as_val: Result<String, DecoderError> = rlp.as_val(); let as_val: Result<String, DecoderError> = rlp.as_val();
assert_eq!(Err(DecoderError::RlpInconsistentLengthAndData), as_val); assert_eq!(Err(DecoderError::RlpInconsistentLengthAndData), as_val);
} }
#[test]
fn test_rlp_long_data_length_check()
{
let mut data: Vec<u8> = vec![0xb8, 255];
for _ in 0..253 {
data.push(b'c');
}
let rlp = UntrustedRlp::new(&data);
let as_val: Result<String, DecoderError> = rlp.as_val();
assert_eq!(Err(DecoderError::RlpInconsistentLengthAndData), as_val);
}

View File

@ -346,8 +346,16 @@ impl<'a> Decoder for BasicDecoder<'a> {
Some(l @ 0xb8...0xbf) => { Some(l @ 0xb8...0xbf) => {
let len_of_len = l as usize - 0xb7; let len_of_len = l as usize - 0xb7;
let begin_of_value = 1 as usize + len_of_len; let begin_of_value = 1 as usize + len_of_len;
if bytes.len() < begin_of_value {
return Err(DecoderError::RlpInconsistentLengthAndData);
}
let len = try!(usize::from_bytes(&bytes[1..begin_of_value])); let len = try!(usize::from_bytes(&bytes[1..begin_of_value]));
Ok(try!(f(&bytes[begin_of_value..begin_of_value + len])))
let last_index_of_value = begin_of_value + len;
if bytes.len() < last_index_of_value {
return Err(DecoderError::RlpInconsistentLengthAndData);
}
Ok(try!(f(&bytes[begin_of_value..last_index_of_value])))
} }
// we are reading value, not a list! // we are reading value, not a list!
_ => Err(DecoderError::RlpExpectedToBeData) _ => Err(DecoderError::RlpExpectedToBeData)