Skip to content

Commit

Permalink
decode: Add empty data check
Browse files Browse the repository at this point in the history
Currently we index into the data field without first checking it is not
empty. For context, the indexing is done _before_ we do segwit validity
checks which check for correct data lengths.

This is a real noob bug , bad Tobin - no biscuit.

Add a check for the empty array before indexing into it.
  • Loading branch information
tcharding committed Sep 18, 2023
1 parent dcdbffc commit e73c483
Showing 1 changed file with 4 additions and 0 deletions.
4 changes: 4 additions & 0 deletions src/primitives/decode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -368,6 +368,10 @@ impl<'s> SegwitHrpstring<'s> {
pub fn new(s: &'s str) -> Result<Self, SegwitHrpstringError> {
let unchecked = UncheckedHrpstring::new(s)?;

if unchecked.data.is_empty() {
return Err(SegwitHrpstringError::MissingWitnessVersion);
}

// Unwrap ok since check_characters (in `Self::new`) checked the bech32-ness of this char.
let witness_version = Fe32::from_char(unchecked.data[0].into()).unwrap();
if witness_version.to_u8() > 16 {
Expand Down

0 comments on commit e73c483

Please sign in to comment.