forked from solana-labs/solana
-
Notifications
You must be signed in to change notification settings - Fork 262
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
9c20984
commit efaf811
Showing
7 changed files
with
99 additions
and
48 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
[package] | ||
name = "solana-limited-deserialize" | ||
description = "Solana function for deserializing with a limit" | ||
documentation = "https://docs.rs/solana-limited-deserialize" | ||
version = { workspace = true } | ||
authors = { workspace = true } | ||
repository = { workspace = true } | ||
homepage = { workspace = true } | ||
license = { workspace = true } | ||
edition = { workspace = true } | ||
|
||
[dependencies] | ||
bincode = { workspace = true } | ||
serde = { workspace = true } | ||
solana-instruction = { workspace = true, default-features = false, features = [ | ||
"std", | ||
] } | ||
|
||
[dev-dependencies] | ||
solana-program = { path = "../program" } | ||
|
||
[package.metadata.docs.rs] | ||
targets = ["x86_64-unknown-linux-gnu"] | ||
|
||
[lints] | ||
workspace = true |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
//! Contains a single utility function for deserializing from [bincode]. | ||
//! | ||
//! [bincode]: https://docs.rs/bincode | ||
use {bincode::config::Options, solana_instruction::error::InstructionError}; | ||
|
||
/// 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 | ||
pub fn limited_deserialize<T>(instruction_data: &[u8], limit: u64) -> Result<T, InstructionError> | ||
where | ||
T: serde::de::DeserializeOwned, | ||
{ | ||
bincode::options() | ||
.with_limit(limit) | ||
.with_fixint_encoding() // As per https://github.com/servo/bincode/issues/333, these two options are needed | ||
.allow_trailing_bytes() // to retain the behavior of bincode::deserialize with the new `options()` method | ||
.deserialize_from(instruction_data) | ||
.map_err(|_| InstructionError::InvalidInstructionData) | ||
} | ||
|
||
#[cfg(test)] | ||
pub mod tests { | ||
use {super::*, solana_program::system_instruction::SystemInstruction}; | ||
|
||
#[test] | ||
fn test_limited_deserialize_advance_nonce_account() { | ||
let item = SystemInstruction::AdvanceNonceAccount; | ||
let mut serialized = bincode::serialize(&item).unwrap(); | ||
|
||
assert_eq!( | ||
serialized.len(), | ||
4, | ||
"`SanitizedMessage::get_durable_nonce()` may need a change" | ||
); | ||
|
||
assert_eq!( | ||
limited_deserialize::<SystemInstruction>(&serialized, 4).as_ref(), | ||
Ok(&item) | ||
); | ||
assert!(limited_deserialize::<SystemInstruction>(&serialized, 3).is_err()); | ||
|
||
serialized.push(0); | ||
assert_eq!( | ||
limited_deserialize::<SystemInstruction>(&serialized, 4).as_ref(), | ||
Ok(&item) | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,48 +1 @@ | ||
//! Contains a single utility function for deserializing from [bincode]. | ||
//! | ||
//! [bincode]: https://docs.rs/bincode | ||
use {crate::instruction::InstructionError, bincode::config::Options}; | ||
|
||
/// 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 | ||
pub fn limited_deserialize<T>(instruction_data: &[u8], limit: u64) -> Result<T, InstructionError> | ||
where | ||
T: serde::de::DeserializeOwned, | ||
{ | ||
bincode::options() | ||
.with_limit(limit) | ||
.with_fixint_encoding() // As per https://github.com/servo/bincode/issues/333, these two options are needed | ||
.allow_trailing_bytes() // to retain the behavior of bincode::deserialize with the new `options()` method | ||
.deserialize_from(instruction_data) | ||
.map_err(|_| InstructionError::InvalidInstructionData) | ||
} | ||
|
||
#[cfg(test)] | ||
pub mod tests { | ||
use {super::*, solana_program::system_instruction::SystemInstruction}; | ||
|
||
#[test] | ||
fn test_limited_deserialize_advance_nonce_account() { | ||
let item = SystemInstruction::AdvanceNonceAccount; | ||
let mut serialized = bincode::serialize(&item).unwrap(); | ||
|
||
assert_eq!( | ||
serialized.len(), | ||
4, | ||
"`SanitizedMessage::get_durable_nonce()` may need a change" | ||
); | ||
|
||
assert_eq!( | ||
limited_deserialize::<SystemInstruction>(&serialized, 4).as_ref(), | ||
Ok(&item) | ||
); | ||
assert!(limited_deserialize::<SystemInstruction>(&serialized, 3).is_err()); | ||
|
||
serialized.push(0); | ||
assert_eq!( | ||
limited_deserialize::<SystemInstruction>(&serialized, 4).as_ref(), | ||
Ok(&item) | ||
); | ||
} | ||
} | ||
pub use solana_limited_deserialize::limited_deserialize; |