Make unrecognized field of SighashTypeParseError an InputString
What changed, and why it matters
This commit is a small internal cleanup in the rust-bitcoin library. It changes how an error message stores the unrecognized text a user typed, switching from a standard String to a special no-allocation string wrapper called InputString. The visible effect is only a slight rewording of the error message shown when someone types an invalid sighash type. There is no security vulnerability being fixed here.
No security action required. Treat as a normal refactoring/reliability change. Review the new error message wording if downstream code parses the string literally.
Security signals we found
No strong security signals were identified.
Evidence from the diff
The patch replaces the unrecognized: String field in SighashTypeParseError with unrecognized: InputString from the internals crate. InputString is designed to avoid unconditional alloc dependencies. The Display implementation is updated to use InputString::display_cannot_parse("SIGHASH string"), which changes the message from unrecognized SIGHASH string '{}' to failed to parse '{}' as SIGHASH string. Tests are updated to match the new wording. No behavioral or cryptographic changes are present.
Changed components
crypto/src/sighash.rsbitcoin/src/crypto/sighash.rsbitcoin/src/blockdata/transaction.rsInspect captured patch +8 / −8
diff --git a/bitcoin/src/blockdata/transaction.rs b/bitcoin/src/blockdata/transaction.rs
index d6ae56d9..ddbcf550 100644
--- a/bitcoin/src/blockdata/transaction.rs
+++ b/bitcoin/src/blockdata/transaction.rs
@@ -1657,7 +1657,7 @@ mod tests {
for s in sht_mistakes {
assert_eq!(
s.parse::<EcdsaSighashType>().unwrap_err().to_string(),
- format!("unrecognized SIGHASH string '{}'", s)
+ format!("failed to parse '{}' as SIGHASH string", s)
);
}
}
diff --git a/bitcoin/src/crypto/sighash.rs b/bitcoin/src/crypto/sighash.rs
index 0fac99c7..c4bc5505 100644
--- a/bitcoin/src/crypto/sighash.rs
+++ b/bitcoin/src/crypto/sighash.rs
@@ -1835,7 +1835,7 @@ mod tests {
for s in sht_mistakes {
assert_eq!(
s.parse::<TapSighashType>().unwrap_err().to_string(),
- format!("unrecognized SIGHASH string '{}'", s)
+ format!("failed to parse '{}' as SIGHASH string", s)
);
}
}
diff --git a/crypto/src/sighash.rs b/crypto/src/sighash.rs
index 1ea94d67..1a5f6035 100644
--- a/crypto/src/sighash.rs
+++ b/crypto/src/sighash.rs
@@ -8,7 +8,6 @@
//! [BIP-0341]: <https://github.com/bitcoin/bips/blob/150ab6f5c3aca9da05fccc5b435e9667853407f4/bip-0341.mediawiki>
//! [BIP-0143]: <https://github.com/bitcoin/bips/blob/99701f68a88ce33b2d0838eb84e115cef505b4c2/bip-0143.mediawiki>
-use alloc::borrow::ToOwned;
use core::{fmt, str};
#[cfg(feature = "arbitrary")]
@@ -71,7 +70,7 @@ impl str::FromStr for TapSighashType {
"SIGHASH_ALL|SIGHASH_ANYONECANPAY" => Ok(Self::AllPlusAnyoneCanPay),
"SIGHASH_NONE|SIGHASH_ANYONECANPAY" => Ok(Self::NonePlusAnyoneCanPay),
"SIGHASH_SINGLE|SIGHASH_ANYONECANPAY" => Ok(Self::SinglePlusAnyoneCanPay),
- _ => Err(SighashTypeParseError { unrecognized: s.to_owned() }),
+ _ => Err(SighashTypeParseError { unrecognized: s.into() }),
}
}
}
@@ -146,7 +145,7 @@ impl str::FromStr for EcdsaSighashType {
"SIGHASH_ALL|SIGHASH_ANYONECANPAY" => Ok(Self::AllPlusAnyoneCanPay),
"SIGHASH_NONE|SIGHASH_ANYONECANPAY" => Ok(Self::NonePlusAnyoneCanPay),
"SIGHASH_SINGLE|SIGHASH_ANYONECANPAY" => Ok(Self::SinglePlusAnyoneCanPay),
- _ => Err(SighashTypeParseError { unrecognized: s.to_owned() }),
+ _ => Err(SighashTypeParseError { unrecognized: s.into() }),
}
}
}
@@ -228,9 +227,10 @@ impl From<EcdsaSighashType> for TapSighashType {
/// Error types for signature hashing.
pub mod error {
- use alloc::string::String;
use core::fmt;
+ use internals::error::InputString;
+
/// Integer is not a consensus valid sighash type.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct InvalidSighashTypeError(pub u32);
@@ -269,12 +269,12 @@ pub mod error {
#[non_exhaustive]
pub struct SighashTypeParseError {
/// The unrecognized string we attempted to parse.
- pub unrecognized: String,
+ pub unrecognized: InputString,
}
impl fmt::Display for SighashTypeParseError {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
- write!(f, "unrecognized SIGHASH string '{}'", self.unrecognized)
+ write!(f, "{}", self.unrecognized.display_cannot_parse("SIGHASH string"))
}
}
Why this scored 18/100
Community notes
Notes can correct, qualify, or add evidence to the AI analysis. Every note shown here has been validated by a human moderator.
The AI analysis stands alone for now. Submit a note if you can add evidence or important context.