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:
@ -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);
|
||||
|
Reference in New Issue
Block a user