File tree Expand file tree Collapse file tree 2 files changed +18
-0
lines changed
packages/ripple-binary-codec Expand file tree Collapse file tree 2 files changed +18
-0
lines changed Original file line number Diff line number Diff line change @@ -140,6 +140,18 @@ function normalize(
140140
141141 if ( lastDigit != null && lastDigit >= BigInt ( 5 ) ) {
142142 m += BigInt ( 1 )
143+ // After rounding, mantissa may exceed MAX_INT64 again
144+ if ( m > MAX_INT64 ) {
145+ if ( exponent >= MAX_EXPONENT ) {
146+ throw new Error ( 'Exponent overflow: value too large to represent' )
147+ }
148+ lastDigit = m % BigInt ( 10 )
149+ exponent += 1
150+ m /= BigInt ( 10 )
151+ if ( lastDigit >= BigInt ( 5 ) ) {
152+ m += BigInt ( 1 )
153+ }
154+ }
143155 }
144156
145157 if ( isNegative ) m = - m
Original file line number Diff line number Diff line change @@ -180,6 +180,12 @@ describe('STNumber', () => {
180180 expect ( sn . toJSON ( ) ) . toEqual ( '1e22' )
181181 } )
182182
183+ it ( 'mantissa > MAX_INT64' , ( ) => {
184+ const value = '92233720368547758079'
185+ const sn = STNumber . from ( value )
186+ expect ( sn . toJSON ( ) ) . toEqual ( '922337203685477581e2' )
187+ } )
188+
183189 it ( 'throws on exponent overflow (value too large)' , ( ) => {
184190 // 1e40000 has exponent 40000, after normalization exponent = 40000 - 18 = 39982
185191 // which exceeds MAX_EXPONENT (32768)
You can’t perform that action at this time.
0 commit comments