Limited Deserialize isn't limiting anything (#10952)
* Add failing test * Use deserialize_from to enable limit
This commit is contained in:
@ -9,6 +9,27 @@ where
|
||||
let limit = crate::packet::PACKET_DATA_SIZE as u64;
|
||||
bincode::config()
|
||||
.limit(limit)
|
||||
.deserialize(instruction_data)
|
||||
.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()); // crate::packet::PACKET_DATA_SIZE - 12: size limit, minus enum variant and vec len() serialized sizes
|
||||
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()); // Extra byte should bump serialized size over the size limit
|
||||
let serialized = bincode::serialize(&item).unwrap();
|
||||
assert!(limited_deserialize::<Foo>(&serialized).is_err());
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user