Skip to content
This repository was archived by the owner on Nov 6, 2020. It is now read-only.

Commit

Permalink
Merge pull request #38 from gavofyork/gav
Browse files Browse the repository at this point in the history
Fix panic in crypto, avoid incorrect casting in bytes.
  • Loading branch information
arkpar committed Jan 11, 2016
2 parents 584bcda + e2de777 commit eab9f01
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 27 deletions.
46 changes: 21 additions & 25 deletions src/bytes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
//! }
//! ```
use std::mem;
use std::fmt;
use std::slice;
use std::cmp::Ordering;
Expand Down Expand Up @@ -260,19 +261,23 @@ impl FromBytes for String {
}
}

impl FromBytes for u64 {
fn from_bytes(bytes: &[u8]) -> FromBytesResult<u64> {
match bytes.len() {
0 => Ok(0),
l @ 1...8 => {
let mut res = 0u64;
for i in 0..l {
let shift = (l - 1 - i) * 8;
res = res + ((bytes[i] as u64) << shift);
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 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 $to) << shift);
}
Ok(res)
}
_ => Err(FromBytesError::DataIsTooLong)
}
Ok(res)
}
_ => Err(FromBytesError::DataIsTooLong)
}
}
}
Expand All @@ -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) => {
Expand Down
2 changes: 1 addition & 1 deletion src/crypto.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/rlp/rlperrors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ pub enum DecoderError {
RlpIsTooShort,
RlpExpectedToBeList,
RlpExpectedToBeData,
RlpIncorrectListLen
RlpIncorrectListLen,
}

impl StdError for DecoderError {
Expand Down

0 comments on commit eab9f01

Please sign in to comment.