@@ -80,7 +80,10 @@ impl Processor {
80
80
if * mint_info. key == crate :: native_mint:: id ( ) {
81
81
let rent_exempt_reserve = rent. minimum_balance ( new_account_info_data_len) ;
82
82
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 ) ?;
84
87
} else {
85
88
account. is_native = COption :: None ;
86
89
account. amount = 0 ;
@@ -164,7 +167,10 @@ impl Processor {
164
167
if source_account. delegated_amount < amount {
165
168
return Err ( TokenError :: InsufficientFunds . into ( ) ) ;
166
169
}
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 ) ?;
168
174
if source_account. delegated_amount == 0 {
169
175
source_account. delegate = COption :: None ;
170
176
}
@@ -177,15 +183,25 @@ impl Processor {
177
183
) ?,
178
184
} ;
179
185
180
- source_account. amount -= amount;
186
+ source_account. amount = source_account
187
+ . amount
188
+ . checked_sub ( amount)
189
+ . ok_or ( TokenError :: Overflow ) ?;
181
190
dest_account. amount = dest_account
182
191
. amount
183
192
. checked_add ( amount)
184
193
. ok_or ( TokenError :: Overflow ) ?;
185
194
186
195
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 ) ?;
189
205
}
190
206
191
207
Ok ( ( ) )
@@ -437,7 +453,10 @@ impl Processor {
437
453
if source_account. delegated_amount < amount {
438
454
return Err ( TokenError :: InsufficientFunds . into ( ) ) ;
439
455
}
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 ) ?;
441
460
if source_account. delegated_amount == 0 {
442
461
source_account. delegate = COption :: None ;
443
462
}
@@ -450,8 +469,14 @@ impl Processor {
450
469
) ?,
451
470
}
452
471
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 ) ?;
455
480
456
481
Ok ( ( ) )
457
482
}
@@ -480,7 +505,11 @@ impl Processor {
480
505
account_info_iter. as_slice ( ) ,
481
506
) ?;
482
507
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
+
484
513
* * source_account_info. lamports . borrow_mut ( ) = 0 ;
485
514
source_account. amount = 0 ;
486
515
0 commit comments