Skip to content
This repository was archived by the owner on Jan 22, 2025. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion core/src/rpc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1693,7 +1693,7 @@ fn deserialize_bs58_transaction(bs58_transaction: String) -> Result<(Vec<u8>, Tr
}
bincode::config()
.limit(PACKET_DATA_SIZE as u64)
.deserialize(&wire_transaction)
.deserialize_from(&wire_transaction[..])
Comment on lines 1694 to +1696
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm tempted to make this a plain-old bincode::deserialize, since we check the wire_transaction length and return a better error message immediately before this.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If there isn't a performance impact I would say keep this consistent by using deserialize_from. Keeping it is also a good measure in case the other checks change or are removed for some reason.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sounds good. That's where I landed also

.map_err(|err| {
info!("transaction deserialize error: {:?}", err);
Error::invalid_params(&err.to_string())
Expand Down
2 changes: 1 addition & 1 deletion perf/src/packet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ where
{
bincode::config()
.limit(PACKET_DATA_SIZE as u64)
.deserialize(data)
.deserialize_from(data)
}

#[cfg(test)]
Expand Down
23 changes: 22 additions & 1 deletion sdk/src/program_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}
}