runtime: checked math for Bank::withdraw
This commit is contained in:
parent
dcf2d84b24
commit
be29568318
|
@ -4019,7 +4019,7 @@ impl Bank {
|
||||||
self.store_account(pubkey, new_account);
|
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) {
|
match self.get_account_with_fixed_root(pubkey) {
|
||||||
Some(mut account) => {
|
Some(mut account) => {
|
||||||
let min_balance = match get_system_account_kind(&account) {
|
let min_balance = match get_system_account_kind(&account) {
|
||||||
|
@ -4029,9 +4029,11 @@ impl Bank {
|
||||||
.minimum_balance(nonce::State::size()),
|
.minimum_balance(nonce::State::size()),
|
||||||
_ => 0,
|
_ => 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;
|
account.lamports -= lamports;
|
||||||
self.store_account(pubkey, &account);
|
self.store_account(pubkey, &account);
|
||||||
|
|
Loading…
Reference in New Issue