Remove redundant array allocation and copy in OutPointDecoder
What changed, and why it matters
This commit is a straightforward internal code cleanup. It removes a temporary 36-byte buffer and manual byte copying when decoding an OutPoint (a Bitcoin transaction reference), replacing it with a direct split of the already-decoded byte array. There is no security-relevant change.
No security action needed. Treat as a normal refactoring/review commit.
Security signals we found
No strong security signals were identified.
Evidence from the diff
OutPointDecoder::end() previously decoded into a 36-byte array, then copied the first 32 bytes into txid_buf and the last 4 bytes into vout_buf before constructing Txid and u32. The patch uses internals::array::ArrayExt::split_array::<32, 4>() to split the decoded array directly, eliminating the intermediate buffers and copy_from_slice calls. The functional behavior is unchanged.
Changed components
primitives/src/transaction.rsOutPointDecoder::end()Inspect captured patch +4 / −7
diff --git a/primitives/src/transaction.rs b/primitives/src/transaction.rs
index 33aac638..2da02135 100644
--- a/primitives/src/transaction.rs
+++ b/primitives/src/transaction.rs
@@ -27,6 +27,7 @@ use encoding::{
use hashes::sha256d;
#[cfg(feature = "alloc")]
use internals::compact_size;
+use internals::array::ArrayExt as _;
use internals::write_err;
#[cfg(feature = "serde")]
use serde::{de, Deserialize, Deserializer, Serialize, Serializer};
@@ -1102,14 +1103,10 @@ impl encoding::Decoder for OutPointDecoder {
#[inline]
fn end(self) -> Result<Self::Output, Self::Error> {
let encoded = self.0.end().map_err(OutPointDecoderError)?;
+ let (txid_buf, vout_buf) = encoded.split_array::<32, 4>();
- let mut txid_buf = [0_u8; 32];
- txid_buf.copy_from_slice(&encoded[..32]);
- let txid = Txid::from_byte_array(txid_buf);
-
- let mut vout_buf = [0_u8; 4];
- vout_buf.copy_from_slice(&encoded[32..]);
- let vout = u32::from_le_bytes(vout_buf);
+ let txid = Txid::from_byte_array(*txid_buf);
+ let vout = u32::from_le_bytes(*vout_buf);
Ok(OutPoint { txid, vout })
}
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.