From 0688716af69ab60460c5a6d3d20acb06034719c0 Mon Sep 17 00:00:00 2001 From: Arkadiy Paronyan Date: Wed, 21 Sep 2016 18:59:46 +0800 Subject: [PATCH] Handle RLP to string UTF-8 decoding errors (#2217) --- util/rlp/src/bytes.rs | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/util/rlp/src/bytes.rs b/util/rlp/src/bytes.rs index 5940d21d2..07ac108d6 100644 --- a/util/rlp/src/bytes.rs +++ b/util/rlp/src/bytes.rs @@ -174,6 +174,8 @@ pub enum FromBytesError { DataIsTooLong, /// Integer-representation is non-canonically prefixed with zero byte(s). ZeroPrefixedInt, + /// String representation is not utf-8 + InvalidUtf8, } impl StdError for FromBytesError { @@ -199,7 +201,7 @@ pub trait FromBytes: Sized { impl FromBytes for String { fn from_bytes(bytes: &[u8]) -> FromBytesResult { - Ok(::std::str::from_utf8(bytes).unwrap().to_owned()) + ::std::str::from_utf8(bytes).map(|s| s.to_owned()).map_err(|_| FromBytesError::InvalidUtf8) } }