What changed, and why it matters
This commit only adds new unit tests that check error messages and error chaining for a Bitcoin transaction witness decoder. It does not change any production code, fix a bug, or alter behavior. There is no security issue here.
No action needed; this is a routine test-only commit.
Security signals we found
No strong security signals were identified.
Evidence from the diff
The diff adds three test cases in primitives/src/witness.rs verifying that WitnessDecoderError displays a non-empty string, has a source error under std, and that UnexpectedEofError also displays a non-empty string. It imports alloc::string::ToString and std::error::Error for these assertions. No library logic is modified.
Changed components
primitives/src/witness.rs (test module only)Inspect captured patch +36 / −0
diff --git a/primitives/src/witness.rs b/primitives/src/witness.rs
index 82ed1f04..3de884ff 100644
--- a/primitives/src/witness.rs
+++ b/primitives/src/witness.rs
@@ -956,8 +956,12 @@ fn decode_unchecked(slice: &mut &[u8]) -> u64 {
#[cfg(test)]
mod test {
+ #[cfg(feature = "alloc")]
+ use alloc::string::ToString;
#[cfg(feature = "alloc")]
use alloc::{format, vec};
+ #[cfg(feature = "std")]
+ use std::error::Error as _;
#[cfg(feature = "alloc")]
use encoding::Decodable as _;
@@ -1435,6 +1439,9 @@ mod test {
err,
WitnessDecoderError(WitnessDecoderErrorInner::LengthPrefixDecode(_))
));
+ assert!(!err.to_string().is_empty());
+ #[cfg(feature = "std")]
+ assert!(err.source().is_some());
}
#[test]
@@ -1571,6 +1578,35 @@ mod test {
assert_eq!(decoder.read_limit(), 499);
}
+ #[test]
+ #[cfg(feature = "alloc")]
+ fn decoder_end_without_witness_count_errors() {
+ let err = WitnessDecoder::new().end().unwrap_err();
+ assert!(matches!(
+ err,
+ WitnessDecoderError(WitnessDecoderErrorInner::UnexpectedEof(UnexpectedEofError {
+ missing_elements: 0
+ }))
+ ));
+ assert!(!err.to_string().is_empty());
+ #[cfg(feature = "std")]
+ assert!(err.source().is_some());
+ }
+
+ #[test]
+ #[cfg(feature = "alloc")]
+ fn decoder_unexpected_eof_error() {
+ let mut decoder = WitnessDecoder::new();
+ let mut slice = [0x01].as_slice(); // witness element count = 1.
+ assert!(decoder.push_bytes(&mut slice).unwrap());
+
+ let inner = match decoder.end().unwrap_err() {
+ WitnessDecoderError(WitnessDecoderErrorInner::UnexpectedEof(inner)) => inner,
+ err => panic!("unexpected error: {err}"),
+ };
+ assert!(!inner.to_string().is_empty());
+ }
+
#[test]
#[cfg(feature = "alloc")]
fn reserve_batch_returns_existing_len() {
Why this scored 15/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.