Fix panic in crypto, avoid incorrect casting in bytes.
This commit is contained in:
parent
48fbf24e21
commit
e2de777c30
34
src/bytes.rs
34
src/bytes.rs
@ -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,15 +261,17 @@ impl FromBytes for String {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl FromBytes for u64 {
|
macro_rules! impl_uint_from_bytes {
|
||||||
fn from_bytes(bytes: &[u8]) -> FromBytesResult<u64> {
|
($to: ident) => {
|
||||||
|
impl FromBytes for $to {
|
||||||
|
fn from_bytes(bytes: &[u8]) -> FromBytesResult<$to> {
|
||||||
match bytes.len() {
|
match bytes.len() {
|
||||||
0 => Ok(0),
|
0 => Ok(0),
|
||||||
l @ 1...8 => {
|
l if l <= mem::size_of::<$to>() => {
|
||||||
let mut res = 0u64;
|
let mut res = 0 as $to;
|
||||||
for i in 0..l {
|
for i in 0..l {
|
||||||
let shift = (l - 1 - i) * 8;
|
let shift = (l - 1 - i) * 8;
|
||||||
res = res + ((bytes[i] as u64) << shift);
|
res = res + ((bytes[i] as $to) << shift);
|
||||||
}
|
}
|
||||||
Ok(res)
|
Ok(res)
|
||||||
}
|
}
|
||||||
@ -276,6 +279,8 @@ impl FromBytes for u64 {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
impl FromBytes for bool {
|
impl FromBytes for bool {
|
||||||
fn from_bytes(bytes: &[u8]) -> FromBytesResult<bool> {
|
fn from_bytes(bytes: &[u8]) -> FromBytesResult<bool> {
|
||||||
@ -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) => {
|
||||||
|
@ -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,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -8,7 +8,7 @@ pub enum DecoderError {
|
|||||||
RlpIsTooShort,
|
RlpIsTooShort,
|
||||||
RlpExpectedToBeList,
|
RlpExpectedToBeList,
|
||||||
RlpExpectedToBeData,
|
RlpExpectedToBeData,
|
||||||
RlpIncorrectListLen
|
RlpIncorrectListLen,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl StdError for DecoderError {
|
impl StdError for DecoderError {
|
||||||
|
Loading…
Reference in New Issue
Block a user