From 65ab52405394cbfc9e4e516360513335f4ba7dfc Mon Sep 17 00:00:00 2001 From: Gav Wood Date: Mon, 11 Jan 2016 23:59:33 +0100 Subject: [PATCH] Avoid panic on invalid uint data. --- src/bytes.rs | 6 +++++- src/lib.rs | 1 + src/uint.rs | 2 ++ 3 files changed, 8 insertions(+), 1 deletion(-) diff --git a/src/bytes.rs b/src/bytes.rs index 8d33a4108..98ab4c2d7 100644 --- a/src/bytes.rs +++ b/src/bytes.rs @@ -305,7 +305,11 @@ macro_rules! impl_uint_from_bytes { ($name: ident) => { impl FromBytes for $name { fn from_bytes(bytes: &[u8]) -> FromBytesResult<$name> { - Ok($name::from(bytes)) + if bytes.len() <= $name::SIZE { + Ok($name::from(bytes)) + } else { + Err(FromBytesError::DataIsTooLong) + } } } } diff --git a/src/lib.rs b/src/lib.rs index de2cdbdf7..b856431cd 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,4 +1,5 @@ #![feature(op_assign_traits)] +#![feature(associated_consts)] //! Ethcore-util library //! //! ### Rust version: diff --git a/src/uint.rs b/src/uint.rs index 6fc8d19e1..b7411fd30 100644 --- a/src/uint.rs +++ b/src/uint.rs @@ -50,6 +50,8 @@ macro_rules! construct_uint { pub struct $name(pub [u64; $n_words]); impl $name { + pub const SIZE: usize = $n_words * 8; + /// Conversion to u32 #[inline] pub fn low_u32(&self) -> u32 {