Skip to content

Potential loss of precision when parsing long and integer types #1299

@Gigiz

Description

@Gigiz

Hi

I noticed a potential issue in how XML numeric types are parsed. In particular, this section of code:

if (name === 'int' || name === 'integer' || name === 'short' || name === 'long') {
   value = parseInt(text, 10);
}

While this works fine for short and int (which are within JavaScript's safe integer range), it can lead to loss of precision for long and integer types.

In XML Schema:

long is typically a 64-bit signed integer, with values up to ±9.22e18

integer is unbounded, and can be arbitrarily large

JavaScript’s Number type cannot safely represent integers beyond 2^53 - 1 (Number.MAX_SAFE_INTEGER). Using parseInt here may result in inaccurate values for large numbers.

For example:

parseInt("9223372036854775807", 10) // returns 9223372036854776000

which is incorrect

Maybe, consider using BigInt for parsing long and integer values:

if (name === 'long' || name === 'integer') {
   value = BigInt(text);
} 

xs:integer (unlimited)
xs:long (64 bit)
xs:unsignedLong (0  2⁶⁴  1)
xs:negativeInteger, xs:positiveInteger, xs:nonNegativeInteger, xs:nonPositiveInteger (all subset of xs:integer)
xs:decimal

This would ensure large values are parsed accurately.

Let me know what you think — happy to help with a PR if this approach sounds good.

Thanks again!

Metadata

Metadata

Assignees

Labels

No labels
No labels

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions