Skip to content
This repository has been archived by the owner on Jan 13, 2025. It is now read-only.

Commit

Permalink
runtime: checked math for Bank::withdraw (#16788)
Browse files Browse the repository at this point in the history
(cherry picked from commit be29568)

# Conflicts:
#	runtime/src/bank.rs

Co-authored-by: Trent Nelson <trent@solana.com>
  • Loading branch information
mergify[bot] and t-nelson authored Apr 24, 2021
1 parent ff95737 commit 2ce6c86
Showing 1 changed file with 6 additions and 4 deletions.
10 changes: 6 additions & 4 deletions runtime/src/bank.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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);
Expand Down

0 comments on commit 2ce6c86

Please sign in to comment.