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

View File

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

View File

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