Skip to content

Commit 46f0507

Browse files
committed
address edge case
1 parent 739708e commit 46f0507

File tree

2 files changed

+18
-0
lines changed

2 files changed

+18
-0
lines changed

packages/ripple-binary-codec/src/types/st-number.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff 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

packages/ripple-binary-codec/test/st-number.test.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff 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)

0 commit comments

Comments
 (0)