Skip to content

Commit

Permalink
Guard against incomplete list/maps (#207)
Browse files Browse the repository at this point in the history
  • Loading branch information
knutwalker authored Dec 18, 2024
1 parent 0b175da commit 2c810f4
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 0 deletions.
6 changes: 6 additions & 0 deletions lib/src/packstream/de.rs
Original file line number Diff line number Diff line change
Expand Up @@ -377,6 +377,9 @@ impl<'a, 'de> SeqAccess<'de> for ItemsParser<'a> {
self.len -= 1;

let bytes = self.bytes.get();
if bytes.is_empty() {
return Ok(None);
}
seed.deserialize(Deserializer { bytes }).map(Some)
}

Expand All @@ -398,6 +401,9 @@ impl<'a, 'de> MapAccess<'de> for ItemsParser<'a> {
self.len -= 1;

let bytes = self.bytes.get();
if bytes.is_empty() {
return Ok(None);
}
seed.deserialize(Deserializer { bytes }).map(Some)
}

Expand Down
20 changes: 20 additions & 0 deletions lib/src/packstream/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -611,4 +611,24 @@ mod tests {
let expected = Bytes::copy_from_slice(input);
assert_eq!(actual, expected);
}

#[test]
fn dont_fail_on_wrong_lengths() {
let wrong_list = bolt()
.tiny_list(3) // says it's three elements
.tiny_int(42) // but it's not
.build();

assert_eq!(from_bytes::<Vec<u32>>(wrong_list).unwrap(), vec![42]);

let wrong_map = bolt()
.tiny_map(3) // says it's three elements
.tiny_string("key") // but it's not
.tiny_int(42)
.build();

let data = from_bytes::<std::collections::HashMap<String, u32>>(wrong_map).unwrap();
assert_eq!(data.len(), 1);
assert_eq!(data.get("key").copied(), Some(42));
}
}

0 comments on commit 2c810f4

Please sign in to comment.