fix possible panic from short data length (#3751)

This commit is contained in:
samkim-crypto 2022-10-26 09:36:46 +09:00 committed by GitHub
parent 5f3cbc49ae
commit bf10dfddf4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 11 additions and 0 deletions

View File

@ -244,6 +244,9 @@ fn get_extension<S: BaseState, V: Extension>(tlv_data: &[u8]) -> Result<&V, Prog
// get_extension_indices has checked that tlv_data is long enough to include these indices
let length = pod_from_bytes::<Length>(&tlv_data[length_start..value_start])?;
let value_end = value_start.saturating_add(usize::from(*length));
if tlv_data.len() < value_end {
return Err(ProgramError::InvalidAccountData);
}
pod_from_bytes::<V>(&tlv_data[value_start..value_end])
}
@ -933,6 +936,14 @@ mod test {
state.get_extension::<TransferFeeConfig>(),
Err(ProgramError::InvalidAccountData)
);
// data buffer is too small
let buffer = &MINT_WITH_EXTENSION[..MINT_WITH_EXTENSION.len() - 1];
let state = StateWithExtensions::<Mint>::unpack(buffer).unwrap();
assert_eq!(
state.get_extension::<MintCloseAuthority>(),
Err(ProgramError::InvalidAccountData)
);
}
#[test]