From d8d544e17e5816cc75ecb4e8238ec7e5e0c47fa3 Mon Sep 17 00:00:00 2001 From: Alex Pyattaev Date: Wed, 5 Mar 2025 09:04:58 +0000 Subject: [PATCH] comments from review --- gossip/src/crds_value.rs | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/gossip/src/crds_value.rs b/gossip/src/crds_value.rs index 256c74392ee8fa..f97e29816340dc 100644 --- a/gossip/src/crds_value.rs +++ b/gossip/src/crds_value.rs @@ -15,7 +15,11 @@ use { solana_sanitize::{Sanitize, SanitizeError}, solana_signature::Signature, solana_signer::Signer, - std::borrow::{Borrow, Cow}, + std::{ + borrow::{Borrow, Cow}, + io::Cursor, + mem::MaybeUninit, + }, }; /// CrdsValue that is replicated across the cluster @@ -228,13 +232,17 @@ impl<'de> Deserialize<'de> for CrdsValue { data: CrdsData, } let CrdsValue { signature, data } = CrdsValue::deserialize(deserializer)?; - let mut buffer = [0u8; PACKET_DATA_SIZE]; + // To compute the hash of the received CrdsData we need to re-serialize it + // PACKET_DATA_SIZE is always enough since we have just received the value in a packet + let mut buffer: MaybeUninit<[u8; PACKET_DATA_SIZE]> = MaybeUninit::uninit(); + // SAFETY: we are only using this buffer to store serialize results + let buf_ref = unsafe { buffer.assume_init_mut() }; let position = { - let mut cursor = std::io::Cursor::new(buffer.as_mut()); + let mut cursor = Cursor::new(buf_ref.as_mut()); bincode::serialize_into(&mut cursor, &data).map_err(serde::de::Error::custom)?; cursor.position() as usize }; - let hash = solana_sha256_hasher::hashv(&[signature.as_ref(), &buffer[0..position]]); + let hash = solana_sha256_hasher::hashv(&[signature.as_ref(), &buf_ref[0..position]]); Ok(Self { signature, data,