From 2d0165d62517e255e1c2ee6aeffb742bc7791536 Mon Sep 17 00:00:00 2001 From: neithanmo Date: Mon, 3 Aug 2020 19:46:17 -0600 Subject: [PATCH] Returns an Error in case of slicing non-utf-8 strings --- vm/address/src/lib.rs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/vm/address/src/lib.rs b/vm/address/src/lib.rs index 0f08bb58a884..814cbcd6a14a 100644 --- a/vm/address/src/lib.rs +++ b/vm/address/src/lib.rs @@ -162,7 +162,7 @@ impl FromStr for Address { return Err(Error::InvalidLength); } // ensure the network character is valid before converting - let network: Network = match &addr[0..1] { + let network: Network = match addr.get(0..1).ok_or(Error::UnknownNetwork)? { TESTNET_PREFIX => Network::Testnet, MAINNET_PREFIX => Network::Mainnet, _ => { @@ -171,7 +171,7 @@ impl FromStr for Address { }; // get protocol from second character - let protocol: Protocol = match &addr[1..2] { + let protocol: Protocol = match addr.get(1..2).ok_or(Error::UnknownProtocol)? { "0" => Protocol::ID, "1" => Protocol::Secp256k1, "2" => Protocol::Actor, @@ -182,7 +182,7 @@ impl FromStr for Address { }; // bytes after the protocol character is the data payload of the address - let raw = &addr[2..]; + let raw = addr.get(2..).ok_or(Error::InvalidPayload)?; if protocol == Protocol::ID { if raw.len() > 20 { // 20 is max u64 as string