1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
use crate::instruction::InstructionError;
use bincode::config::Options;
pub fn limited_deserialize<T>(instruction_data: &[u8]) -> Result<T, InstructionError>
where
T: serde::de::DeserializeOwned,
{
let limit = crate::packet::PACKET_DATA_SIZE as u64;
bincode::options()
.with_limit(limit)
.with_fixint_encoding()
.allow_trailing_bytes()
.deserialize_from(instruction_data)
.map_err(|_| InstructionError::InvalidInstructionData)
}
#[cfg(test)]
pub mod tests {
use super::*;
#[test]
fn test_limited_deserialize() {
#[derive(Deserialize, Serialize)]
enum Foo {
Bar(Vec<u8>),
}
let item = Foo::Bar([1; crate::packet::PACKET_DATA_SIZE - 12].to_vec());
let serialized = bincode::serialize(&item).unwrap();
assert!(limited_deserialize::<Foo>(&serialized).is_ok());
let item = Foo::Bar([1; crate::packet::PACKET_DATA_SIZE - 11].to_vec());
let serialized = bincode::serialize(&item).unwrap();
assert!(limited_deserialize::<Foo>(&serialized).is_err());
}
}