return a deserialization error if too many empty nodes detected

This commit is contained in:
Grzegorz Świrski 2025-04-16 10:02:39 +02:00 committed by Qyriad
parent 33e2d226a1
commit 7daabb1964

View file

@ -195,8 +195,21 @@ impl Deserializable for SparseMerklePath {
source: &mut R, source: &mut R,
) -> Result<Self, DeserializationError> { ) -> Result<Self, DeserializationError> {
let depth = source.read_u8()?; let depth = source.read_u8()?;
if depth > SMT_MAX_DEPTH {
return Err(DeserializationError::InvalidValue(format!(
"SparseMerklePath max depth exceeded ({} > {})",
depth, SMT_MAX_DEPTH
)));
}
let empty_nodes_mask = source.read_u64()?; let empty_nodes_mask = source.read_u64()?;
let count = depth as u32 - empty_nodes_mask.count_ones(); let empty_nodes_count = empty_nodes_mask.count_ones();
if empty_nodes_count > depth as u32 {
return Err(DeserializationError::InvalidValue(format!(
"SparseMerklePath has more empty nodes ({}) than its full length ({})",
empty_nodes_count, depth
)));
}
let count = depth as u32 - empty_nodes_count;
let nodes = source.read_many::<RpoDigest>(count as usize)?; let nodes = source.read_many::<RpoDigest>(count as usize)?;
Ok(Self { empty_nodes_mask, nodes }) Ok(Self { empty_nodes_mask, nodes })
} }