Skip to content

Commit 27992e4

Browse files
reuse amount validations (#82)
* reuse amount validations * Add note on safety of source decrement Co-authored-by: Fernando Otero <febo@anza.xyz> * Add note on safety of dest increment Co-authored-by: Fernando Otero <febo@anza.xyz> * lint --------- Co-authored-by: Fernando Otero <febo@anza.xyz>
1 parent 3cfb37d commit 27992e4

File tree

1 file changed

+5
-6
lines changed

1 file changed

+5
-6
lines changed

p-token/src/processor/shared/transfer.rs

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -172,18 +172,17 @@ pub fn process_transfer(
172172
if source_account.is_native() {
173173
// SAFETY: single mutable borrow to `source_account_info` lamports.
174174
let source_lamports = unsafe { source_account_info.borrow_mut_lamports_unchecked() };
175-
*source_lamports = source_lamports
176-
.checked_sub(amount)
177-
.ok_or(TokenError::Overflow)?;
175+
// Note: The amount of a source token account is already validated and the
176+
// `lamports` on the account is always greater than `amount`.
177+
*source_lamports -= amount;
178178

179179
// SAFETY: single mutable borrow to `destination_account_info` lamports; the
180180
// account is already validated to be different from
181181
// `source_account_info`.
182182
let destination_lamports =
183183
unsafe { destination_account_info.borrow_mut_lamports_unchecked() };
184-
*destination_lamports = destination_lamports
185-
.checked_add(amount)
186-
.ok_or(TokenError::Overflow)?;
184+
// Note: The total lamports supply is bound to `u64::MAX`.
185+
*destination_lamports += amount;
187186
}
188187
}
189188

0 commit comments

Comments
 (0)