return a deserialization error if too many empty nodes detected
This commit is contained in:
parent
68f0b2c9b2
commit
c9ad4e570d
1 changed files with 14 additions and 1 deletions
|
@ -195,8 +195,21 @@ impl Deserializable for SparseMerklePath {
|
|||
source: &mut R,
|
||||
) -> Result<Self, DeserializationError> {
|
||||
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 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)?;
|
||||
Ok(Self { empty_nodes_mask, nodes })
|
||||
}
|
||||
|
|
Loading…
Add table
Reference in a new issue