Merge pull request #38 from gavofyork/gav

Fix panic in crypto, avoid incorrect casting in bytes.
This commit is contained in:
Arkadiy Paronyan 2016-01-12 00:58:04 +01:00
commit eab9f010f9
3 changed files with 23 additions and 27 deletions

View File

@ -34,6 +34,7 @@
//! } //! }
//! ``` //! ```
use std::mem;
use std::fmt; use std::fmt;
use std::slice; use std::slice;
use std::cmp::Ordering; use std::cmp::Ordering;
@ -260,19 +261,23 @@ impl FromBytes for String {
} }
} }
impl FromBytes for u64 { macro_rules! impl_uint_from_bytes {
fn from_bytes(bytes: &[u8]) -> FromBytesResult<u64> { ($to: ident) => {
match bytes.len() { impl FromBytes for $to {
0 => Ok(0), fn from_bytes(bytes: &[u8]) -> FromBytesResult<$to> {
l @ 1...8 => { match bytes.len() {
let mut res = 0u64; 0 => Ok(0),
for i in 0..l { l if l <= mem::size_of::<$to>() => {
let shift = (l - 1 - i) * 8; let mut res = 0 as $to;
res = res + ((bytes[i] as u64) << shift); for i in 0..l {
let shift = (l - 1 - i) * 8;
res = res + ((bytes[i] as $to) << shift);
}
Ok(res)
}
_ => Err(FromBytesError::DataIsTooLong)
} }
Ok(res)
} }
_ => Err(FromBytesError::DataIsTooLong)
} }
} }
} }
@ -287,20 +292,11 @@ impl FromBytes for bool {
} }
} }
//impl_uint_from_bytes!(u8);
macro_rules! impl_map_from_bytes { impl_uint_from_bytes!(u16);
($from: ident, $to: ident) => { impl_uint_from_bytes!(u32);
impl FromBytes for $from { impl_uint_from_bytes!(u64);
fn from_bytes(bytes: &[u8]) -> FromBytesResult<$from> { impl_uint_from_bytes!(usize);
$to::from_bytes(bytes).map(| x | { x as $from })
}
}
}
}
impl_map_from_bytes!(usize, u64);
impl_map_from_bytes!(u16, u64);
impl_map_from_bytes!(u32, u64);
macro_rules! impl_uint_from_bytes { macro_rules! impl_uint_from_bytes {
($name: ident) => { ($name: ident) => {

View File

@ -37,7 +37,7 @@ impl From<::secp256k1::Error> for CryptoError {
::secp256k1::Error::InvalidPublicKey => CryptoError::InvalidPublic, ::secp256k1::Error::InvalidPublicKey => CryptoError::InvalidPublic,
::secp256k1::Error::InvalidSignature => CryptoError::InvalidSignature, ::secp256k1::Error::InvalidSignature => CryptoError::InvalidSignature,
::secp256k1::Error::InvalidSecretKey => CryptoError::InvalidSecret, ::secp256k1::Error::InvalidSecretKey => CryptoError::InvalidSecret,
_ => panic!("Crypto error: {:?}", e), _ => CryptoError::InvalidSignature,
} }
} }
} }

View File

@ -8,7 +8,7 @@ pub enum DecoderError {
RlpIsTooShort, RlpIsTooShort,
RlpExpectedToBeList, RlpExpectedToBeList,
RlpExpectedToBeData, RlpExpectedToBeData,
RlpIncorrectListLen RlpIncorrectListLen,
} }
impl StdError for DecoderError { impl StdError for DecoderError {