Skip to content

Commit 175df94

Browse files
committed
Merge #130: Fix index out of bounds bug
ea644f0 decode: Add unit test for parsing segwit address (Tobin C. Harding) 5d6e5c3 decode: Add empty data check (Tobin C. Harding) Pull request description: Noob bug here, I accessed an array without first checking that it was non-empty :( Adds unit test as a separate patch, re-arrange to verify the bug. Discovered while fuzzing #128. ACKs for top commit: apoelstra: ACK ea644f0 Tree-SHA512: dda0b91f8e2f94ba47b0d6f386e1edc17938bf4c71851ec387f922389ba384693655ed863e98bb95f60970bbf36844cc6c58b654c666b6ab1116cd6dea970ba2
2 parents ea16371 + ea644f0 commit 175df94

File tree

1 file changed

+26
-0
lines changed

1 file changed

+26
-0
lines changed

src/primitives/decode.rs

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -368,6 +368,10 @@ impl<'s> SegwitHrpstring<'s> {
368368
pub fn new(s: &'s str) -> Result<Self, SegwitHrpstringError> {
369369
let unchecked = UncheckedHrpstring::new(s)?;
370370

371+
if unchecked.data.is_empty() {
372+
return Err(SegwitHrpstringError::MissingWitnessVersion);
373+
}
374+
371375
// Unwrap ok since check_characters (in `Self::new`) checked the bech32-ness of this char.
372376
let witness_version = Fe32::from_char(unchecked.data[0].into()).unwrap();
373377
if witness_version.to_u8() > 16 {
@@ -943,4 +947,26 @@ mod tests {
943947
assert!(CheckedHrpstring::new::<Bech32>(valid).is_ok())
944948
}
945949
}
950+
951+
macro_rules! check_invalid_segwit_addresses {
952+
($($test_name:ident, $reason:literal, $address:literal);* $(;)?) => {
953+
$(
954+
#[test]
955+
fn $test_name() {
956+
let res = SegwitHrpstring::new($address);
957+
if res.is_ok() {
958+
panic!("{} sting should not be valid: {}", $address, $reason);
959+
}
960+
}
961+
)*
962+
}
963+
}
964+
check_invalid_segwit_addresses! {
965+
invalid_segwit_address_0, "missing hrp", "1qar0srrr7xfkvy5l643lydnw9re59gtzzwf5mdq";
966+
invalid_segwit_address_1, "missing data-checksum", "91111";
967+
invalid_segwit_address_2, "invalid witness version", "bc14r0srrr7xfkvy5l643lydnw9re59gtzzwf5mdq";
968+
invalid_segwit_address_3, "invalid checksum length", "bc1q5mdq";
969+
invalid_segwit_address_4, "missing data", "bc1qwf5mdq";
970+
invalid_segwit_address_5, "invalid program length", "bc14r0srrr7xfkvy5l643lydnw9rewf5mdq";
971+
}
946972
}

0 commit comments

Comments
 (0)