Skip to content

Backport of #809: fix crash in Descriptor::parse_desc #810

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

Open
wants to merge 3 commits into
base: release-12.x
Choose a base branch
from
Open
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
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "miniscript"
version = "12.3.1"
version = "12.3.2"
authors = ["Andrew Poelstra <[email protected]>, Sanket Kanjalkar <[email protected]>"]
license = "CC0-1.0"
homepage = "https://github.com/rust-bitcoin/rust-miniscript/"
Expand Down
24 changes: 18 additions & 6 deletions src/descriptor/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -731,12 +731,9 @@ impl Descriptor<DescriptorPublicKey> {
}

let descriptor = Descriptor::<String>::from_str(s)?;
let descriptor = descriptor.translate_pk(&mut keymap_pk).map_err(|e| {
Error::Unexpected(
e.expect_translator_err("No Outer context errors")
.to_string(),
)
})?;
let descriptor = descriptor
.translate_pk(&mut keymap_pk)
.map_err(TranslateErr::flatten)?;

Ok((descriptor, keymap_pk.0))
}
Expand Down Expand Up @@ -2052,4 +2049,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 @@ -350,10 +350,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