Browse Source

Increase formatting buffer

febo 10 months ago
parent
commit
982a51cf84
2 changed files with 11 additions and 13 deletions
  1. 2 8
      p-token/src/processor/amount_to_ui_amount.rs
  2. 9 5
      p-token/src/processor/mod.rs

+ 2 - 8
p-token/src/processor/amount_to_ui_amount.rs

@@ -8,13 +8,7 @@ use token_interface::{
     state::{load, mint::Mint},
     state::{load, mint::Mint},
 };
 };
 
 
-use super::{check_account_owner, MAX_DIGITS_U64};
-
-/// Maximum length of the UI amount string.
-///
-/// The length includes the maximum number of digits in a `u64`` (20)
-/// and the maximum number of punctuation characters (2).
-const MAX_UI_AMOUNT_LENGTH: usize = MAX_DIGITS_U64 + 2;
+use super::{check_account_owner, MAX_FORMATTED_DIGITS};
 
 
 #[inline(always)]
 #[inline(always)]
 pub fn process_amount_to_ui_amount(
 pub fn process_amount_to_ui_amount(
@@ -34,7 +28,7 @@ pub fn process_amount_to_ui_amount(
         load::<Mint>(mint_info.borrow_data_unchecked()).map_err(|_| TokenError::InvalidMint)?
         load::<Mint>(mint_info.borrow_data_unchecked()).map_err(|_| TokenError::InvalidMint)?
     };
     };
 
 
-    let mut logger = Logger::<MAX_UI_AMOUNT_LENGTH>::default();
+    let mut logger = Logger::<MAX_FORMATTED_DIGITS>::default();
     logger.append_with_args(amount, &[Argument::Precision(mint.decimals)]);
     logger.append_with_args(amount, &[Argument::Precision(mint.decimals)]);
     // "Extract" the formatted string from the logger.
     // "Extract" the formatted string from the logger.
     //
     //

+ 9 - 5
p-token/src/processor/mod.rs

@@ -74,8 +74,12 @@ pub use ui_amount_to_amount::process_ui_amount_to_amount;
 /// An uninitialized byte.
 /// An uninitialized byte.
 const UNINIT_BYTE: MaybeUninit<u8> = MaybeUninit::uninit();
 const UNINIT_BYTE: MaybeUninit<u8> = MaybeUninit::uninit();
 
 
-/// Maximum number of digits in a `u64``.
-const MAX_DIGITS_U64: usize = 20;
+/// Maximum number of digits in a formatted `u64`.
+///
+/// The maximum number of digits is equal to the maximum number
+/// of decimals (`u8::MAX`) plus the length of the decimal point
+/// and the leading zero.
+const MAX_FORMATTED_DIGITS: usize = u8::MAX as usize + 2;
 
 
 /// Checks that the account is owned by the expected program.
 /// Checks that the account is owned by the expected program.
 #[inline(always)]
 #[inline(always)]
@@ -146,16 +150,16 @@ fn try_ui_amount_into_amount(ui_amount: &str, decimals: u8) -> Result<u64, Progr
     if (amount_str.is_empty() && after_decimal.is_empty())
     if (amount_str.is_empty() && after_decimal.is_empty())
         || parts.next().is_some()
         || parts.next().is_some()
         || after_decimal.len() > decimals
         || after_decimal.len() > decimals
-        || (length + expected_after_decimal_length) > MAX_DIGITS_U64
+        || (length + expected_after_decimal_length) > MAX_FORMATTED_DIGITS
     {
     {
         return Err(ProgramError::InvalidArgument);
         return Err(ProgramError::InvalidArgument);
     }
     }
 
 
-    let mut digits = [UNINIT_BYTE; MAX_DIGITS_U64];
+    let mut digits = [UNINIT_BYTE; MAX_FORMATTED_DIGITS];
     // SAFETY: `digits` is an array of `MaybeUninit<u8>`, which has the same
     // SAFETY: `digits` is an array of `MaybeUninit<u8>`, which has the same
     // memory layout as `u8`.
     // memory layout as `u8`.
     let slice: &mut [u8] =
     let slice: &mut [u8] =
-        unsafe { from_raw_parts_mut(digits.as_mut_ptr() as *mut _, MAX_DIGITS_U64) };
+        unsafe { from_raw_parts_mut(digits.as_mut_ptr() as *mut _, MAX_FORMATTED_DIGITS) };
 
 
     // SAFETY: the total length of `amount_str` and `after_decimal` is less than
     // SAFETY: the total length of `amount_str` and `after_decimal` is less than
     // `MAX_DIGITS_U64`.
     // `MAX_DIGITS_U64`.