primitives: Return reference to inner error in source
What changed, and why it matters
This commit fixes a small bug in how Rust error chains are reported for two Bitcoin parsing error types. Previously, when asking 'what caused this error?', the code accidentally skipped past the immediate cause and returned the next level up. Now it correctly returns the immediate inner error. This is a correctness fix for error reporting and does not directly create a security vulnerability or allow attacks.
No immediate security action required. Treat as a normal correctness/maintenance patch. Reviewers may want to verify no other error types in the crate make the same delegation mistake.
Security signals we found
Error-chain correctness fix
No unsafe code
No input parsing logic changed
No cryptographic or consensus code touched
Evidence from the diff
The patch changes the std::error::Error::source implementations for ParseHeaderError and ParseTransactionError in rust-bitcoin’s primitives crate. The old code delegated to std::error::Error::source(&self.0), which recursively called source() on the inner error, effectively stripping one layer from the error chain. The new code returns Some(&self.0), exposing the immediate wrapped error as the source. This aligns with Rust’s error-chain convention and improves diagnostic accuracy. It is a bug fix with no direct memory-safety, cryptographic, or network-security impact.
Changed components
primitives/src/block.rsprimitives/src/transaction.rsInspect captured patch +2 / −6
diff --git a/primitives/src/block.rs b/primitives/src/block.rs
index 005ef6b4..4fad3d95 100644
--- a/primitives/src/block.rs
+++ b/primitives/src/block.rs
@@ -559,9 +559,7 @@ impl fmt::Display for ParseHeaderError {
#[cfg(all(feature = "hex", feature = "std"))]
impl std::error::Error for ParseHeaderError {
- fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
- std::error::Error::source(&self.0)
- }
+ fn source(&self) -> Option<&(dyn std::error::Error + 'static)> { Some(&self.0) }
}
encoding::encoder_newtype_exact! {
diff --git a/primitives/src/transaction.rs b/primitives/src/transaction.rs
index 4cdd4477..ffaa52e2 100644
--- a/primitives/src/transaction.rs
+++ b/primitives/src/transaction.rs
@@ -422,9 +422,7 @@ impl fmt::Display for ParseTransactionError {
#[cfg(all(feature = "hex", feature = "alloc", feature = "std"))]
impl std::error::Error for ParseTransactionError {
- fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
- std::error::Error::source(&self.0)
- }
+ fn source(&self) -> Option<&(dyn std::error::Error + 'static)> { Some(&self.0) }
}
/// The decoder for the [`Transaction`] type.
Why this scored 17/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.