Skip to content
Open
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
6 changes: 3 additions & 3 deletions rmp/src/decode/est.rs
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,7 @@ impl MessageLen {
}

fn read_sequence(&mut self, data: &mut &[u8], mut items_left: u32) -> Option<()> {
let parent_depth = self.current_depth;
self.current_depth += 1;
if self.current_depth > self.max_depth {
return self.fail(WIP::LimitExceeded);
Expand All @@ -244,17 +245,16 @@ impl MessageLen {
let position_before_item = self.position;
self.read_one_item(data).or_else(|| {
self.set_max_position(position_before_item + items_left as usize);
// -1, because it will increase depth again when resumed
self.sequences_wip.push(Seq {
items_left: non_zero,
depth: self.current_depth - 1,
depth: parent_depth,
});
None
})?;
items_left -= 1;
}
debug_assert!(self.current_depth > 0);
self.current_depth -= 1;
self.current_depth = parent_depth;
Some(())
}

Expand Down
14 changes: 14 additions & 0 deletions rmp/tests/func/est.rs
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,20 @@ fn map() {
check_estimates(&out, &[1, 7, 7, 7, 9, 9, 11, 11, 11, 11, 11, 14, 14, 21, 21, 21, 21, 21, 21, 21, 21, 22, 26, 26, 28, 28, 28, 28, 29, 31, 31, 35, 35, 35, 35, -35]);
}

#[test]
fn incremental_depth() {
let msg = [0x92, 0x91, 0xc0, 0x90]; // [[nil], []]

// Given `msg` is a valid message
assert_eq!(msg.len(), MessageLen::with_limits(2, 16).incremental_len(&msg).unwrap());

// Regression test: when reading the message in chunks, we used to fail due
// to incorrect depth tracking
let mut incremental = MessageLen::with_limits(2, 16);
assert!(matches!(incremental.incremental_len(&msg[..2]), Err(LenError::Truncated(_))));
assert_eq!(msg.len(), incremental.incremental_len(&msg[2..]).unwrap());
}

#[test]
fn nested() {
let mut out = Vec::new();
Expand Down