bitcoin: Use ptr::cast function intstead of manually casting
What changed, and why it matters
This is a small code cleanup in the rust-bitcoin library. It replaces one style of low-level pointer casting with a more modern, equivalent Rust function. The change does not alter the actual behavior of the code; it is purely a stylistic/refactoring improvement.
No security action required. Treat as a normal refactoring commit.
Security signals we found
No strong security signals were identified.
Evidence from the diff
The commit modifies bitcoin/src/taproot/merkle_branch/borrowed.rs. It changes decode_exact from casting a slice reference via &*(nodes as *const _ as *const [TapNodeHash]) to using slice::from_raw_parts(nodes.as_ptr().cast::<TapNodeHash>(), nodes.len()). The safety comment is updated to cite #[repr(transparent)] and the inner [u8; 32] layout. The functionality—reinterpreting &[[u8; 32]] as &[TapNodeHash]—remains the same.
Changed components
bitcoin/src/taproot/merkle_branch/borrowed.rsInspect captured patch +6 / −5
diff --git a/bitcoin/src/taproot/merkle_branch/borrowed.rs b/bitcoin/src/taproot/merkle_branch/borrowed.rs
index d28af7b5..37e6179d 100644
--- a/bitcoin/src/taproot/merkle_branch/borrowed.rs
+++ b/bitcoin/src/taproot/merkle_branch/borrowed.rs
@@ -1,4 +1,5 @@
use core::borrow::{Borrow, BorrowMut};
+use core::slice;
use internals::slice::SliceExt;
pub use privacy_boundary::TaprootMerkleBranch;
@@ -95,11 +96,11 @@ impl TaprootMerkleBranch {
fn decode_exact(
nodes: &[[u8; TAPROOT_CONTROL_NODE_SIZE]],
) -> Result<&Self, InvalidMerkleTreeDepthError> {
- // SAFETY:
- // The lifetime of the returned reference is the same as the lifetime of the input
- // reference, the size of `TapNodeHash` is equal to `TAPROOT_CONTROL_NODE_SIZE` and the
- // alignment of `TapNodeHash` is equal to the alignment of `u8` (see tests below).
- Self::from_hashes(unsafe { &*(nodes as *const _ as *const [TapNodeHash]) })
+ // SAFETY: `TapNodeHash` is `#[repr(transparent)]` and contains a (type which is
+ // `#[repr(transparent)]` and contains a) `[u8; 32]`.
+ Self::from_hashes(unsafe {
+ slice::from_raw_parts(nodes.as_ptr().cast::<TapNodeHash>(), nodes.len())
+ })
}
fn from_hashes(nodes: &[TapNodeHash]) -> Result<&Self, InvalidMerkleTreeDepthError> {
Why this scored 12/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.