Skip to content
This repository was archived by the owner on Mar 11, 2025. It is now read-only.

Commit 0b71752

Browse files
Use checked math everywhere (#346)
1 parent 49e6e4a commit 0b71752

File tree

1 file changed

+38
-9
lines changed

1 file changed

+38
-9
lines changed

token/program/src/processor.rs

Lines changed: 38 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,10 @@ impl Processor {
8080
if *mint_info.key == crate::native_mint::id() {
8181
let rent_exempt_reserve = rent.minimum_balance(new_account_info_data_len);
8282
account.is_native = COption::Some(rent_exempt_reserve);
83-
account.amount = new_account_info.lamports() - rent_exempt_reserve;
83+
account.amount = new_account_info
84+
.lamports()
85+
.checked_sub(rent_exempt_reserve)
86+
.ok_or(TokenError::Overflow)?;
8487
} else {
8588
account.is_native = COption::None;
8689
account.amount = 0;
@@ -164,7 +167,10 @@ impl Processor {
164167
if source_account.delegated_amount < amount {
165168
return Err(TokenError::InsufficientFunds.into());
166169
}
167-
source_account.delegated_amount -= amount;
170+
source_account.delegated_amount = source_account
171+
.delegated_amount
172+
.checked_sub(amount)
173+
.ok_or(TokenError::Overflow)?;
168174
if source_account.delegated_amount == 0 {
169175
source_account.delegate = COption::None;
170176
}
@@ -177,15 +183,25 @@ impl Processor {
177183
)?,
178184
};
179185

180-
source_account.amount -= amount;
186+
source_account.amount = source_account
187+
.amount
188+
.checked_sub(amount)
189+
.ok_or(TokenError::Overflow)?;
181190
dest_account.amount = dest_account
182191
.amount
183192
.checked_add(amount)
184193
.ok_or(TokenError::Overflow)?;
185194

186195
if source_account.is_native() {
187-
**source_account_info.lamports.borrow_mut() -= amount;
188-
**dest_account_info.lamports.borrow_mut() += amount;
196+
let source_starting_lamports = source_account_info.lamports();
197+
**source_account_info.lamports.borrow_mut() = source_starting_lamports
198+
.checked_sub(amount)
199+
.ok_or(TokenError::Overflow)?;
200+
201+
let dest_starting_lamports = dest_account_info.lamports();
202+
**dest_account_info.lamports.borrow_mut() = dest_starting_lamports
203+
.checked_add(amount)
204+
.ok_or(TokenError::Overflow)?;
189205
}
190206

191207
Ok(())
@@ -437,7 +453,10 @@ impl Processor {
437453
if source_account.delegated_amount < amount {
438454
return Err(TokenError::InsufficientFunds.into());
439455
}
440-
source_account.delegated_amount -= amount;
456+
source_account.delegated_amount = source_account
457+
.delegated_amount
458+
.checked_sub(amount)
459+
.ok_or(TokenError::Overflow)?;
441460
if source_account.delegated_amount == 0 {
442461
source_account.delegate = COption::None;
443462
}
@@ -450,8 +469,14 @@ impl Processor {
450469
)?,
451470
}
452471

453-
source_account.amount -= amount;
454-
mint.supply -= amount;
472+
source_account.amount = source_account
473+
.amount
474+
.checked_sub(amount)
475+
.ok_or(TokenError::Overflow)?;
476+
mint.supply = mint
477+
.supply
478+
.checked_sub(amount)
479+
.ok_or(TokenError::Overflow)?;
455480

456481
Ok(())
457482
}
@@ -480,7 +505,11 @@ impl Processor {
480505
account_info_iter.as_slice(),
481506
)?;
482507

483-
**dest_account_info.lamports.borrow_mut() += source_account_info.lamports();
508+
let dest_starting_lamports = dest_account_info.lamports();
509+
**dest_account_info.lamports.borrow_mut() = dest_starting_lamports
510+
.checked_add(source_account_info.lamports())
511+
.ok_or(TokenError::Overflow)?;
512+
484513
**source_account_info.lamports.borrow_mut() = 0;
485514
source_account.amount = 0;
486515

0 commit comments

Comments
 (0)