Skip to content

fix crash in Descriptor::parse_desc found by fuzzer #809

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Apr 28, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 16 additions & 1 deletion src/descriptor/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -771,7 +771,7 @@ impl Descriptor<DescriptorPublicKey> {
let descriptor = Descriptor::<String>::from_str(s)?;
let descriptor = descriptor
.translate_pk(&mut keymap_pk)
.map_err(|e| e.expect_translator_err("No Outer context errors"))?;
.map_err(TranslateErr::flatten)?;

Ok((descriptor, keymap_pk.0))
}
Expand Down Expand Up @@ -2168,4 +2168,19 @@ pk(03f28773c2d975288bc7d1d205c3748651b075fbc6610e58cddeeddf8f19405aa8))";
Desc::from_str(&format!("tr({},pk({}))", x_only_key, uncomp_key)).unwrap_err();
Desc::from_str(&format!("tr({},pk({}))", x_only_key, x_only_key)).unwrap();
}

#[test]
fn regression_806() {
let secp = secp256k1::Secp256k1::signing_only();
type Desc = Descriptor<DescriptorPublicKey>;
// OK
Desc::from_str("pkh(111111111111111111111111111111110000008375319363688624584A111111)")
.unwrap_err();
// ERR: crashes in translate_pk
Desc::parse_descriptor(
&secp,
"pkh(111111111111111111111111111111110000008375319363688624584A111111)",
)
.unwrap_err();
}
}
20 changes: 16 additions & 4 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -356,10 +356,22 @@ impl<E> TranslateErr<E> {
///
/// This function will panic if the Error is OutError.
pub fn expect_translator_err(self, msg: &str) -> E {
if let Self::TranslatorErr(v) = self {
v
} else {
panic!("{}", msg)
match self {
Self::TranslatorErr(v) => v,
Self::OuterError(ref e) => {
panic!("Unexpected Miniscript error when translating: {}\nMessage: {}", e, msg)
}
}
}
}

impl TranslateErr<Error> {
/// If we are doing a translation where our "outer error" is the generic
/// Miniscript error, eliminate the `TranslateErr` type which is just noise.
pub fn flatten(self) -> Error {
match self {
Self::TranslatorErr(e) => e,
Self::OuterError(e) => e,
}
}
}
Expand Down
Loading