From 5d6e5c3df35d148819a9ff9a94184f5222b19e7e Mon Sep 17 00:00:00 2001 From: "Tobin C. Harding" Date: Tue, 19 Sep 2023 09:53:57 +1000 Subject: [PATCH] decode: Add empty data check 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. --- src/primitives/decode.rs | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/primitives/decode.rs b/src/primitives/decode.rs index 67103b553..f6f2b0759 100644 --- a/src/primitives/decode.rs +++ b/src/primitives/decode.rs @@ -368,6 +368,10 @@ impl<'s> SegwitHrpstring<'s> { pub fn new(s: &'s str) -> Result { 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 {