From 766528ef09427667d978b2a8a26f46484ad632ae Mon Sep 17 00:00:00 2001 From: "Dr. Chat" Date: Wed, 1 Jan 2025 14:04:20 -0600 Subject: [PATCH] Panic if we attempt to serialize a `Node` with two adjacent tree entries --- atrium-repo/src/mst.rs | 19 ++++++++++++------- 1 file changed, 12 insertions(+), 7 deletions(-) diff --git a/atrium-repo/src/mst.rs b/atrium-repo/src/mst.rs index b4e92387..83b3758d 100644 --- a/atrium-repo/src/mst.rs +++ b/atrium-repo/src/mst.rs @@ -809,14 +809,19 @@ impl Node { let mut prev_key = vec![]; let mut i = 0usize; while i != ents.len() { - // Skip this window if the first entry is not a leaf. - let leaf = if let Some(leaf) = ents.get(i).and_then(NodeEntry::leaf) { - leaf - } else { - i += 1; - continue; + let (leaf, tree) = match (ents.get(i), ents.get(i + 1)) { + (Some(NodeEntry::Tree(_)), Some(NodeEntry::Tree(_))) => { + // We should never encounter this. If this is hit, something went wrong when modifying the tree. + panic!("attempted to serialize node with two adjacent trees") + } + (Some(NodeEntry::Leaf(leaf)), Some(NodeEntry::Tree(tree))) => (leaf, Some(tree)), + (Some(NodeEntry::Leaf(leaf)), _) => (leaf, None), + // Skip this window if the first entry is not a leaf. + _ => { + i += 1; + continue; + } }; - let tree = ents.get(i + 1).and_then(NodeEntry::tree); let prefix = prefix(&prev_key, &leaf.key.as_bytes());