Skip to content

Commit

Permalink
Merge pull request #4 from RCasatta/fix_short_schema
Browse files Browse the repository at this point in the history
Fix panic for parsing uri shorter than the length of the schema
  • Loading branch information
Kixunil authored Jul 30, 2021
2 parents c41101d + a4ffaf0 commit 9c96451
Showing 1 changed file with 15 additions and 1 deletion.
16 changes: 15 additions & 1 deletion bip78/src/uri.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ impl<'a> TryFrom<&'a str> for Uri<'a> {
}

let prefix = "bitcoin:";
if !s.chars().zip(prefix.chars()).all(|(left, right)| left.to_ascii_lowercase() == right) {
if !s.chars().zip(prefix.chars()).all(|(left, right)| left.to_ascii_lowercase() == right) || s.len() < 8 {
return Err(InternalBip21Error::BadSchema(s.into()).into())
}
let uri_without_prefix = &s[prefix.len()..];
Expand Down Expand Up @@ -141,3 +141,17 @@ impl From<InternalPjParseError> for ParseUriError {
PjParseError(value).into()
}
}


#[cfg(test)]
mod tests {
use crate::Uri;
use std::str::FromStr;

#[test]
fn test_short() {
assert!(Uri::from_str("").is_err());
assert!(Uri::from_str("bitcoin").is_err());
assert!(Uri::from_str("bitcoin:").is_err());
}
}

0 comments on commit 9c96451

Please sign in to comment.