From be29568318404e3278a785cc079f006692886eda Mon Sep 17 00:00:00 2001 From: Trent Nelson Date: Fri, 23 Apr 2021 02:38:54 -0600 Subject: [PATCH] runtime: checked math for Bank::withdraw --- runtime/src/bank.rs | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/runtime/src/bank.rs b/runtime/src/bank.rs index 1da53c00c6..aa09dbf3a0 100644 --- a/runtime/src/bank.rs +++ b/runtime/src/bank.rs @@ -4019,7 +4019,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_with_fixed_root(pubkey) { Some(mut account) => { let min_balance = match get_system_account_kind(&account) { @@ -4029,9 +4029,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);