Fails when deserializing non-hex uints (#2247)

This commit is contained in:
Tomasz Drwięga 2016-09-22 18:02:47 +02:00 committed by Gav Wood
parent 15488b3e40
commit 368aca521b
2 changed files with 22 additions and 1 deletions

View File

@ -26,7 +26,7 @@ use util::Mutex;
use v1::traits::EthFilter; use v1::traits::EthFilter;
use v1::types::{BlockNumber, Index, Filter, Log, H256 as RpcH256, U256 as RpcU256}; use v1::types::{BlockNumber, Index, Filter, Log, H256 as RpcH256, U256 as RpcU256};
use v1::helpers::{PollFilter, PollManager, limit_logs}; use v1::helpers::{PollFilter, PollManager, limit_logs};
use v1::helpers::params::{expect_no_params}; use v1::helpers::params::expect_no_params;
use v1::impls::eth::pending_logs; use v1::impls::eth::pending_logs;
/// Eth filter rpc implementation. /// Eth filter rpc implementation.

View File

@ -77,6 +77,10 @@ macro_rules! impl_uint {
return Err(serde::Error::custom("Invalid length.")); return Err(serde::Error::custom("Invalid length."));
} }
if &value[0..2] != "0x" {
return Err(serde::Error::custom("Use hex encoded numbers with 0x prefix."))
}
$other::from_str(&value[2..]).map($name).map_err(|_| serde::Error::custom("Invalid hex value.")) $other::from_str(&value[2..]).map($name).map_err(|_| serde::Error::custom("Invalid hex value."))
} }
@ -100,6 +104,8 @@ mod tests {
use super::U256; use super::U256;
use serde_json; use serde_json;
type Res = Result<U256, serde_json::Error>;
#[test] #[test]
fn should_serialize_u256() { fn should_serialize_u256() {
let serialized1 = serde_json::to_string(&U256(0.into())).unwrap(); let serialized1 = serde_json::to_string(&U256(0.into())).unwrap();
@ -113,6 +119,21 @@ mod tests {
assert_eq!(serialized4, r#""0x100""#); assert_eq!(serialized4, r#""0x100""#);
} }
#[test]
fn should_fail_to_deserialize_decimals() {
let deserialized1: Res = serde_json::from_str(r#""""#);
let deserialized2: Res = serde_json::from_str(r#""0""#);
let deserialized3: Res = serde_json::from_str(r#""10""#);
let deserialized4: Res = serde_json::from_str(r#""1000000""#);
let deserialized5: Res = serde_json::from_str(r#""1000000000000000000""#);
assert!(deserialized1.is_err());
assert!(deserialized2.is_err());
assert!(deserialized3.is_err());
assert!(deserialized4.is_err());
assert!(deserialized5.is_err());
}
#[test] #[test]
fn should_deserialize_u256() { fn should_deserialize_u256() {
let deserialized1: U256 = serde_json::from_str(r#""0x""#).unwrap(); let deserialized1: U256 = serde_json::from_str(r#""0x""#).unwrap();