Fail stake init if account data is the wrong size (#13767) (#13769)

(cherry picked from commit 38a3ed96bb)

Co-authored-by: Tyera Eulberg <teulberg@gmail.com>
This commit is contained in:
mergify[bot]
2020-11-23 21:30:33 +00:00
committed by GitHub
parent 086e653a0b
commit 74d57b1c2f

View File

@ -805,6 +805,9 @@ impl<'a> StakeAccount for KeyedAccount<'a> {
lockup: &Lockup,
rent: &Rent,
) -> Result<(), InstructionError> {
if self.data_len()? != std::mem::size_of::<StakeState>() {
return Err(InstructionError::InvalidAccountData);
}
if let StakeState::Uninitialized = self.state()? {
let rent_exempt_reserve = rent.minimum_balance(self.data_len()?);
@ -2168,6 +2171,43 @@ mod tests {
);
}
#[test]
fn test_initialize_incorrect_account_sizes() {
let stake_pubkey = solana_sdk::pubkey::new_rand();
let stake_lamports = 42;
let stake_account =
Account::new_ref(stake_lamports, std::mem::size_of::<StakeState>() + 1, &id());
let stake_keyed_account = KeyedAccount::new(&stake_pubkey, false, &stake_account);
assert_eq!(
stake_keyed_account.initialize(
&Authorized::default(),
&Lockup::default(),
&Rent {
lamports_per_byte_year: 42,
..Rent::free()
},
),
Err(InstructionError::InvalidAccountData)
);
let stake_account =
Account::new_ref(stake_lamports, std::mem::size_of::<StakeState>() - 1, &id());
let stake_keyed_account = KeyedAccount::new(&stake_pubkey, false, &stake_account);
assert_eq!(
stake_keyed_account.initialize(
&Authorized::default(),
&Lockup::default(),
&Rent {
lamports_per_byte_year: 42,
..Rent::free()
},
),
Err(InstructionError::InvalidAccountData)
);
}
#[test]
fn test_deactivate() {
let stake_pubkey = solana_sdk::pubkey::new_rand();