2020-06-17 10:39:14 -07:00
|
|
|
use crate::instruction::InstructionError;
|
2020-01-10 13:20:15 -08:00
|
|
|
|
2020-02-04 17:04:26 -08:00
|
|
|
/// Deserialize with a limit based the maximum amount of data a program can expect to get.
|
|
|
|
/// This function should be used in place of direct deserialization to help prevent OOM errors
|
2020-01-20 15:27:36 -08:00
|
|
|
pub fn limited_deserialize<T>(instruction_data: &[u8]) -> Result<T, InstructionError>
|
2019-10-23 19:56:07 -07:00
|
|
|
where
|
|
|
|
T: serde::de::DeserializeOwned,
|
|
|
|
{
|
|
|
|
let limit = crate::packet::PACKET_DATA_SIZE as u64;
|
|
|
|
bincode::config()
|
|
|
|
.limit(limit)
|
2020-01-20 15:27:36 -08:00
|
|
|
.deserialize(instruction_data)
|
2019-10-23 19:56:07 -07:00
|
|
|
.map_err(|_| InstructionError::InvalidInstructionData)
|
|
|
|
}
|