hashes: Fix reverse hashes for no-hex debug
What changed, and why it matters
This commit fixes a minor display bug in how certain Bitcoin hash values are printed as debug text when the optional 'hex' feature is turned off. Reversed hash types (like transaction IDs in Bitcoin, which are conventionally shown backwards) were being printed in the wrong order. This only affects debug/log output, not the actual hash data or cryptographic operations, so it is not a direct security vulnerability.
No urgent security action required. Treat as a normal correctness fix. If relying on debug output of reversed hash types in no-hex builds, update to the patched version to avoid log/display confusion.
Security signals we found
Feature-gated code path (no-hex builds) had incorrect behavior
Incorrect byte ordering in debug representation could mislead developers or logging systems
No cryptographic, memory-safety, or consensus-critical code changed
Evidence from the diff
The impl_debug_only! macro in hashes/src/macros.rs provides a Debug implementation for hash types when the hex feature is disabled. Previously it ignored the $reverse macro parameter and always printed bytes in forward order. The patch passes self.as_byte_array().iter().rev() to debug_hex() when $reverse is true, and adds a unit test verifying a reversed 32-byte hash with bytes[31] = 0xff renders as ff00...00.
Changed components
hashes/src/macros.rsimpl_debug_only! macroDebug formatting for reversed hash types in no-hex buildsInspect captured patch +14 / −2
diff --git a/hashes/src/macros.rs b/hashes/src/macros.rs
index bb530b7f..bc4ba68d 100644
--- a/hashes/src/macros.rs
+++ b/hashes/src/macros.rs
@@ -291,7 +291,11 @@ macro_rules! impl_debug_only {
impl<$($gen: $gent),*> $crate::_export::_core::fmt::Debug for $ty<$($gen),*> {
#[inline]
fn fmt(&self, f: &mut $crate::_export::_core::fmt::Formatter) -> $crate::_export::_core::fmt::Result {
- $crate::debug_hex(self.as_byte_array(), f)
+ if $reverse {
+ $crate::debug_hex(self.as_byte_array().iter().rev(), f)
+ } else {
+ $crate::debug_hex(self.as_byte_array(), f)
+ }
}
}
}
@@ -577,7 +581,15 @@ mod test {
let want = "0000000000000000000000000000000000000000000000000000000000000000";
let got = format!("{:?}", TestHash::all_zeros());
- assert_eq!(got, want)
+ assert_eq!(got, want);
+
+ // Check that reversing works
+ let mut bytes = [0u8; 32];
+ bytes[31] = 0xff;
+ let hash = TestHash::from_byte_array(bytes);
+ let want = "ff00000000000000000000000000000000000000000000000000000000000000";
+ let got = format!("{:?}", hash);
+ assert_eq!(got, want);
}
#[test]
Why this scored 20/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.