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

@ -358,6 +358,20 @@ fn test_rlp_data_length_check()
let data = vec![0x84, b'c', b'a', b't'];
let rlp = UntrustedRlp::new(&data);
let as_val: Result<String, DecoderError> = rlp.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

@ -333,7 +333,7 @@ impl<'a> Decoder for BasicDecoder<'a> {
Some(l @ 0x80...0xb7) => {
let last_index_of = 1 + l as usize - 0x80;
if bytes.len() < last_index_of {
return Err(DecoderError::RlpInconsistentLengthAndData);
return Err(DecoderError::RlpInconsistentLengthAndData);
}
let d = &bytes[1..last_index_of];
if l == 0x81 && d[0] < 0x80 {
@ -346,8 +346,16 @@ impl<'a> Decoder for BasicDecoder<'a> {
Some(l @ 0xb8...0xbf) => {
let len_of_len = l as usize - 0xb7;
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]));
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!
_ => Err(DecoderError::RlpExpectedToBeData)