Fix crash in Address::from_str if input isn't long enough

This commit is contained in:
Matt Corallo 2018-03-20 15:37:08 -04:00
parent 8ae711ce0f
commit 855b377d51
1 changed files with 3 additions and 2 deletions

View File

@ -241,8 +241,9 @@ impl FromStr for Address {
fn from_str(s: &str) -> Result<Address, Error> { fn from_str(s: &str) -> Result<Address, Error> {
// bech32 (note that upper or lowercase is allowed but NOT mixed case) // bech32 (note that upper or lowercase is allowed but NOT mixed case)
if &s.as_bytes()[0..3] == b"bc1" || &s.as_bytes()[0..3] == b"tb1" || if s.len() >= 3 &&
&s.as_bytes()[0..3] == b"BC1" || &s.as_bytes()[0..3] == b"TB1" { (&s.as_bytes()[0..3] == b"bc1" || &s.as_bytes()[0..3] == b"tb1" ||
&s.as_bytes()[0..3] == b"BC1" || &s.as_bytes()[0..3] == b"TB1") {
let witprog = try!(WitnessProgram::from_address(s)); let witprog = try!(WitnessProgram::from_address(s));
let network = match witprog.network() { let network = match witprog.network() {
bitcoin_bech32::constants::Network::Bitcoin => Network::Bitcoin, bitcoin_bech32::constants::Network::Bitcoin => Network::Bitcoin,