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
Browse files Browse the repository at this point in the history
  • Loading branch information
t-nelson authored and mergify[bot] committed Apr 23, 2021
1 parent dcf2d84 commit be29568
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 @@ -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) {
Expand All @@ -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);
Expand Down

0 comments on commit be29568

Please sign in to comment.