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

Commit 04f3fe9

Browse files
committed
Increase formatting buffer
1 parent a6b53b6 commit 04f3fe9

File tree

2 files changed

+11
-13
lines changed

2 files changed

+11
-13
lines changed

program/src/processor/amount_to_ui_amount.rs

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,7 @@ use token_interface::{
88
state::{load, mint::Mint},
99
};
1010

11-
use super::{check_account_owner, MAX_DIGITS_U64};
12-
13-
/// Maximum length of the UI amount string.
14-
///
15-
/// The length includes the maximum number of digits in a `u64`` (20)
16-
/// and the maximum number of punctuation characters (2).
17-
const MAX_UI_AMOUNT_LENGTH: usize = MAX_DIGITS_U64 + 2;
11+
use super::{check_account_owner, MAX_FORMATTED_DIGITS};
1812

1913
#[inline(always)]
2014
pub fn process_amount_to_ui_amount(
@@ -34,7 +28,7 @@ pub fn process_amount_to_ui_amount(
3428
load::<Mint>(mint_info.borrow_data_unchecked()).map_err(|_| TokenError::InvalidMint)?
3529
};
3630

37-
let mut logger = Logger::<MAX_UI_AMOUNT_LENGTH>::default();
31+
let mut logger = Logger::<MAX_FORMATTED_DIGITS>::default();
3832
logger.append_with_args(amount, &[Argument::Precision(mint.decimals)]);
3933
// "Extract" the formatted string from the logger.
4034
//

program/src/processor/mod.rs

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -74,8 +74,12 @@ pub use ui_amount_to_amount::process_ui_amount_to_amount;
7474
/// An uninitialized byte.
7575
const UNINIT_BYTE: MaybeUninit<u8> = MaybeUninit::uninit();
7676

77-
/// Maximum number of digits in a `u64``.
78-
const MAX_DIGITS_U64: usize = 20;
77+
/// Maximum number of digits in a formatted `u64`.
78+
///
79+
/// The maximum number of digits is equal to the maximum number
80+
/// of decimals (`u8::MAX`) plus the length of the decimal point
81+
/// and the leading zero.
82+
const MAX_FORMATTED_DIGITS: usize = u8::MAX as usize + 2;
7983

8084
/// Checks that the account is owned by the expected program.
8185
#[inline(always)]
@@ -146,16 +150,16 @@ fn try_ui_amount_into_amount(ui_amount: &str, decimals: u8) -> Result<u64, Progr
146150
if (amount_str.is_empty() && after_decimal.is_empty())
147151
|| parts.next().is_some()
148152
|| after_decimal.len() > decimals
149-
|| (length + expected_after_decimal_length) > MAX_DIGITS_U64
153+
|| (length + expected_after_decimal_length) > MAX_FORMATTED_DIGITS
150154
{
151155
return Err(ProgramError::InvalidArgument);
152156
}
153157

154-
let mut digits = [UNINIT_BYTE; MAX_DIGITS_U64];
158+
let mut digits = [UNINIT_BYTE; MAX_FORMATTED_DIGITS];
155159
// SAFETY: `digits` is an array of `MaybeUninit<u8>`, which has the same
156160
// memory layout as `u8`.
157161
let slice: &mut [u8] =
158-
unsafe { from_raw_parts_mut(digits.as_mut_ptr() as *mut _, MAX_DIGITS_U64) };
162+
unsafe { from_raw_parts_mut(digits.as_mut_ptr() as *mut _, MAX_FORMATTED_DIGITS) };
159163

160164
// SAFETY: the total length of `amount_str` and `after_decimal` is less than
161165
// `MAX_DIGITS_U64`.

0 commit comments

Comments
 (0)