From 2ce6c86c2a3cbef145c4ecf99e4443e28d9606bf Mon Sep 17 00:00:00 2001 From: "mergify[bot]" <37929162+mergify[bot]@users.noreply.github.com> Date: Sat, 24 Apr 2021 00:25:41 +0000 Subject: [PATCH] runtime: checked math for Bank::withdraw (#16788) (cherry picked from commit be29568318404e3278a785cc079f006692886eda) # Conflicts: # runtime/src/bank.rs Co-authored-by: Trent Nelson --- 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 82ee80c37426e6..d4304aaf25ab59 100644 --- a/runtime/src/bank.rs +++ b/runtime/src/bank.rs @@ -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);