Block refactoring, stricter RLP.

Fixed #234.
Partially fixes #233 for Blocks.
Fixed #222.
This commit is contained in:
Gav Wood
2016-01-26 19:18:22 +01:00
parent 8a665fe313
commit e904d2145f
11 changed files with 151 additions and 102 deletions

View File

@@ -273,7 +273,9 @@ pub enum FromBytesError {
/// TODO [debris] Please document me
DataIsTooShort,
/// TODO [debris] Please document me
DataIsTooLong
DataIsTooLong,
/// Integer-representation is non-canonically prefixed with zero byte(s).
ZeroPrefixedInt,
}
impl StdError for FromBytesError {
@@ -310,6 +312,9 @@ macro_rules! impl_uint_from_bytes {
match bytes.len() {
0 => Ok(0),
l if l <= mem::size_of::<$to>() => {
if bytes[0] == 0 {
return Err(FromBytesError::ZeroPrefixedInt)
}
let mut res = 0 as $to;
for i in 0..l {
let shift = (l - 1 - i) * 8;
@@ -344,7 +349,9 @@ macro_rules! impl_uint_from_bytes {
($name: ident) => {
impl FromBytes for $name {
fn from_bytes(bytes: &[u8]) -> FromBytesResult<$name> {
if bytes.len() <= $name::SIZE {
if !bytes.is_empty() && bytes[0] == 0 {
Err(FromBytesError::ZeroPrefixedInt)
} else if bytes.len() <= $name::SIZE {
Ok($name::from(bytes))
} else {
Err(FromBytesError::DataIsTooLong)

View File

@@ -7,6 +7,8 @@ use bytes::FromBytesError;
pub enum DecoderError {
/// TODO [debris] Please document me
FromBytesError(FromBytesError),
/// Given data has additional bytes at the end of the valid RLP fragment.
RlpIsTooBig,
/// TODO [debris] Please document me
RlpIsTooShort,
/// TODO [debris] Please document me

View File

@@ -46,6 +46,9 @@ impl PayloadInfo {
value_len: value_len,
}
}
/// Total size of the RLP.
pub fn total(&self) -> usize { self.header_len + self.value_len }
}
/// Data-oriented view onto rlp-slice.