From 1359d80672ea6327b10d3271e98e9595cc2f7933 Mon Sep 17 00:00:00 2001 From: Michal Nazarewicz Date: Wed, 13 Sep 2023 15:02:30 +0200 Subject: [PATCH] sealable-tire: verify expected error when hashing Extension nodes MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Addresses a left-over `XXX` comment which slipped in previous commit. Extension nodes never have empty keys. Panic if that’s not held when calculating node’s hash. This mirrors code in SetContext::handle_extension where the opposite invariant is held and code panics if the key is too long. --- sealable-trie/src/nodes.rs | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/sealable-trie/src/nodes.rs b/sealable-trie/src/nodes.rs index 29a09069..2814e714 100644 --- a/sealable-trie/src/nodes.rs +++ b/sealable-trie/src/nodes.rs @@ -208,12 +208,16 @@ impl<'a, P, S> Node<'a, P, S> { // tag = 0b100v_0000 where v indicates whether the child is // a value reference. let tag = 0x80 | (u8::from(child.is_value()) << 4); - // XXX - if let Ok(len) = key.encode_into(key_buf, tag) { - buf[len..len + 32].copy_from_slice(child.hash().as_slice()); - len + 32 - } else { - return hash_extension_slow_path(*key, child); + match key.encode_into(key_buf, tag) { + Ok(len) => { + buf[len..len + 32] + .copy_from_slice(child.hash().as_slice()); + len + 32 + } + Err(bits::EncodeError::EmptySlice) => { + return hash_extension_slow_path(*key, child); + } + Err(bits::EncodeError::SliceTooLong) => unreachable!(), } } };