Skip to content

Commit 4839435

Browse files
authored
Check length (#236)
* Check length * Remove length check
1 parent 94e9954 commit 4839435

File tree

2 files changed

+11
-9
lines changed

2 files changed

+11
-9
lines changed

program/rust/src/deserialize.rs

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,21 @@ use solana_program::program_error::ProgramError;
1616

1717
/// Interpret the bytes in `data` as a value of type `T`
1818
pub fn load<T: Pod>(data: &[u8]) -> Result<&T, ProgramError> {
19-
try_from_bytes(&data[0..size_of::<T>()]).map_err(|_| ProgramError::InvalidArgument)
19+
try_from_bytes(
20+
data.get(0..size_of::<T>())
21+
.ok_or(ProgramError::InvalidArgument)?,
22+
)
23+
.map_err(|_| ProgramError::InvalidArgument)
2024
}
2125

2226
/// Interpret the bytes in `data` as a mutable value of type `T`
2327
#[allow(unused)]
2428
pub fn load_mut<T: Pod>(data: &mut [u8]) -> Result<&mut T, ProgramError> {
25-
try_from_bytes_mut(&mut data[0..size_of::<T>()]).map_err(|_| ProgramError::InvalidArgument)
29+
try_from_bytes_mut(
30+
data.get_mut(0..size_of::<T>())
31+
.ok_or(ProgramError::InvalidArgument)?,
32+
)
33+
.map_err(|_| ProgramError::InvalidArgument)
2634
}
2735

2836
/// Get the data stored in `account` as a value of type `T`

program/rust/src/processor.rs

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
use std::mem::size_of;
2-
31
use solana_program::program_error::ProgramError;
42
use solana_program::pubkey::Pubkey;
53
use solana_program::sysvar::slot_history::AccountInfo;
@@ -44,11 +42,7 @@ pub fn process_instruction(
4442
instruction_data: &[u8],
4543
input: *mut u8,
4644
) -> OracleResult {
47-
let cmd_hdr_size = size_of::<cmd_hdr>();
48-
if instruction_data.len() < cmd_hdr_size {
49-
return Err(ProgramError::InvalidArgument);
50-
}
51-
let cmd_data = load::<cmd_hdr>(&instruction_data[..cmd_hdr_size])?;
45+
let cmd_data = load::<cmd_hdr>(instruction_data)?;
5246

5347
if cmd_data.ver_ != PC_VERSION {
5448
//FIXME: I am not sure what's best to do here (this is copied from C)

0 commit comments

Comments
 (0)