@@ -74,8 +74,12 @@ pub use ui_amount_to_amount::process_ui_amount_to_amount;
7474/// An uninitialized byte.
7575const 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