runtime: checked math for Bank::withdraw (#16788)

(cherry picked from commit be29568318)

# Conflicts:
#	runtime/src/bank.rs

Co-authored-by: Trent Nelson <trent@solana.com>
This commit is contained in:
mergify[bot]
2021-04-24 00:25:41 +00:00
committed by GitHub
parent ff9573714b
commit 2ce6c86c2a

View File

@ -4014,7 +4014,7 @@ impl Bank {
self.store_account(pubkey, new_account);
}
pub fn withdraw(&self, pubkey: &Pubkey, lamports: u64) -> Result<()> {
fn withdraw(&self, pubkey: &Pubkey, lamports: u64) -> Result<()> {
match self.get_account(pubkey) {
Some(mut account) => {
let min_balance = match get_system_account_kind(&account) {
@ -4024,9 +4024,11 @@ impl Bank {
.minimum_balance(nonce::State::size()),
_ => 0,
};
if lamports + min_balance > account.lamports {
return Err(TransactionError::InsufficientFundsForFee);
}
lamports
.checked_add(min_balance)
.filter(|required_balance| *required_balance <= account.lamports())
.ok_or(TransactionError::InsufficientFundsForFee)?;
account.lamports -= lamports;
self.store_account(pubkey, &account);