Skip to content
This repository has been archived by the owner on Nov 15, 2023. It is now read-only.

Guard some invalid node for proof decoding. #12417

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions primitives/trie/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -986,4 +986,15 @@ mod tests {

assert_eq!(first_storage_root, second_storage_root);
}

#[test]
fn node_with_no_children_fail_decoding() {
let branch = NodeCodec::<Blake2Hasher>::branch_node_nibbled(
b"some_partial".iter().copied(),
24,
vec![None; 16].into_iter(),
Some(trie_db::node::Value::Inline(b"value"[..].into())),
);
assert!(NodeCodec::<Blake2Hasher>::decode(branch.as_slice()).is_err());
}
}
9 changes: 7 additions & 2 deletions primitives/trie/src/node_codec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -304,8 +304,13 @@ const BITMAP_LENGTH: usize = 2;
pub(crate) struct Bitmap(u16);

impl Bitmap {
pub fn decode(mut data: &[u8]) -> Result<Self, codec::Error> {
Ok(Bitmap(u16::decode(&mut data)?))
pub fn decode(data: &[u8]) -> Result<Self, codec::Error> {
let value = u16::decode(&mut &data[..])?;
if value == 0 {
Err("Bitmap without a child.".into())
} else {
Ok(Bitmap(value))
}
}

pub fn value_at(&self, i: usize) -> bool {
Expand Down