Skip to content

Fix rational and double on exponents beyond Int and on integer mantissas - #725

Merged
Lysxia merged 7 commits into
haskell:masterfrom
Lotion-3:read-exponent-overflow
Nov 27, 2026
Merged

Lysxia merged 7 commits into
haskell:masterfrom
Lotion-3:read-exponent-overflow

Conversation

@Lotion-3

@Lotion-3 Lotion-3 commented Nov 23, 2026 •

Copy link
Copy Markdown
Contributor

Two problems in floaty, in both Data.Text.Read and Data.Text.Lazy.Read.

1. The exponent is read as an Int and wraps.

rational "1e18446744073709551616" :: Either String (Double, Text)  -- Right (1.0,"")   read: Infinity
double "1e-18446744073709551615"                                    -- Right (10.0,"")  read: 0.0
double "1e9223372036854775808"                                      -- *** Exception: Negative exponent

The last one throws from a pure function: the exponent wraps to minBound, abs minBound is still negative, and 10 ^^ power raises. An exponent with more significant digits than always fit in an Int (18 on 64-bit) is now an error (Left "exponent out of range"). Such digits aren't converted, so an exponent like 1e followed by 100,000 digits is rejected in linear time.

2. Without a fractional part, rational skipped its exact path. floaty computed fromInteger real * (10 ^^ power) in the result type, while 1.0e… goes through f:

Input rational read
0e400 NaN 0.0
1000e-310 0.0 1.0e-307 (1000.0e-310 already gives this)
5e-324 0.0 5.0e-324
17976931348623157e292 Infinity 1.7976931348623157e308

These now go through f too. double gives the same results as before for them (it adds 0 / 1); double "0e400" is still NaN, as 0 * Infinity in Double, and I left that alone.

Tests: a property comparing rational with read on <integer>e<exponent> (the existing ones use show, which always includes a .), and cases in the DDoS group for the largest exponents accepted and the smallest rejected (built from finiteBitSize, so they also hold on 32-bit) and for 100,000-digit exponents.

The new tests fail on master (8 of them); the rest of the suite is unchanged (t714 fails on my Windows machine on master too).

Benchmarks (ReadNumbers against master, on a local numbers.txt of 100,000 mixed numbers, since text-test-data isn't in the repo): double and the integer readers are unchanged; rational is 42% faster (lazy: 29%), as it now builds the exact value from Integers once instead of with Rational arithmetic in 10 ^^ power.

The exponent was read as an Int and wrapped: "1e18446744073709551616"
gave 1.0, and "1e9223372036854775808" threw "Negative exponent", since
the exponent became minBound. Read it as an Integer and clamp it to the
Int range; a larger exponent gives zero or infinity anyway.

Without a fractional part, floaty computed `fromInteger real * 10 ^^ power`
in the result type, so rational lost its exact path: "0e400" gave NaN
and "5e-324" gave 0.0. Pass these to the reader's own function too.
@Lysxia

Lysxia commented Nov 23, 2026

Copy link
Copy Markdown
Contributor

What's the rationale for clamping instead of returning an error?

@Lysxia

Lysxia commented Nov 23, 2026

Copy link
Copy Markdown
Contributor

Could a compromise be to go through the exact path only if abs exponent > 80? This would recover some of the performance for small exponents.

@Lysxia Lysxia mentioned this pull request Nov 23, 2026
Computing the numerator and denominator as integers and making the Rational once avoids the gcd at every step of 10 ^^ power in Rational. ReadNumbers: RationalText 52.9 -> 31.3 ms, RationalLazyText 70.7 -> 48.5 ms against master.
@Lotion-3

Copy link
Copy Markdown
Contributor Author

read gives Infinity and 0.0 for these (e.g. read "1e9223372036854775808" :: Double), and rational "1e9223372036854775807" already returns Infinity, so an error only from 2^63 on seemed like an arbitrary cut-off. Happy to make it an error if you prefer.

On the shortcut: it isn't exact for small exponents either. For random <integer>e<-80..80>, 42% differ from read in the last digit, e.g. 6e-4 gives 6.000000000000001e-4. But most of the cost turned out to be the Rational arithmetic in 10 ^^ power, so in b9f3382 the exact path builds the numerator and denominator as Integers and makes the Rational once. Against master, RationalText goes from 52.9 to 31.3 ms and RationalLazyText from 70.7 to 48.5 ms; the other ReadNumbers benchmarks are unchanged.

@Lysxia

Lysxia commented Nov 24, 2026

Copy link
Copy Markdown
Contributor

Yes, let's make it an error when the exponent overflows an Int, and replace abs power < 1000 with two comparisons to avoid the problem with minBound.

@Lysxia

Lysxia commented Nov 24, 2026

Copy link
Copy Markdown
Contributor

I'm thinking we should even reject exponents > 1,000,000 because for Rational this can still be a cause of DoS. @Bodigrim does that sound reasonable to you?

I'm not sure whether (1) there are uses of rational @Rational on untrusted inputs, and (2) there are existing legitimate uses of such large exponents that would break because of this.

@Bodigrim

Copy link
Copy Markdown
Contributor

I don't think there exists a robust way to make usage of untrusted Rational (or Integer) safe: presumably the program is going to do some computations with them, and those can easily cause memory exhaustion very soon, even if inputs were reasonably small. So I feel like adding ad-hoc guardrails is a rather futile endeavour.

It is a responsibility of a user to choose appropriate data types. If they want to be safe from DDoS, they should not be using Rational, that's it.

An exponent with more significant digits than maxBound is not converted,
so a long exponent is still read in linear time. rational compares the
exponent with two comparisons instead of abs.
@Lotion-3

Copy link
Copy Markdown
Contributor Author

Done in 487e53d: an exponent outside the Int range is now Left "exponent out of range", and rational uses two comparisons. I left out the 1,000,000 limit, per Bodigrim. This also fixes the ppc64le timeout: the 100,000-digit test built a large Integer there, and now the digits are only converted when they fit in an Int.

Comment thread src/Data/Text/Read.hs Resolved
Comment thread src/Data/Text/Internal/Read.hs Resolved
Comment thread src/Data/Text/Lazy/Read.hs Resolved
Comment thread src/Data/Text/Internal/Read.hs Resolved
Comment thread src/Data/Text/Lazy/Read.hs Resolved
An exponent with more than (finiteBitSize (0 :: Int) - 1) * 3 `div` 10
significant digits is out of range, so the digits that are converted
always fit in an Int, and toExponent's Integer bounds and minBound clamp
are no longer needed. A longer exponent is read as maxBound and rejected
after the perhaps around the exponent, which would otherwise turn the
error into "no exponent".
@Lotion-3

Copy link
Copy Markdown
Contributor Author

Thanks! Applied in a0a479e: the exponent is now read as an Int with at most (finiteBitSize (0 :: Int) - 1) * 3 div 10 significant digits (lengthWord8 in the strict module), so toExponent's Integer bounds and the minBound clamp are gone, and the check is - 1000 < power && power < 1000.

One difference from the suggestion: exponentDigits runs inside perhaps 0 (...), so a Left there would mean "no exponent", and rational "1e1000000000000000000" would give Right (1.0, "e1000000000000000000"). Instead it reads a too-long exponent as maxBound, which no exponent in range can reach, and checkExponent rejects that after the perhaps. The bound tests now use 18 nines (accepted) and 1e1 followed by 18 zeros (rejected).

Comment thread src/Data/Text/Internal/Read.hs Resolved
Comment thread src/Data/Text/Lazy/Read.hs Resolved
Use catchOrBind so that only a missing "e" falls back to no exponent,
go back to before the "e" when no digits follow it, and fail directly
when there are too many significant digits, instead of reading them as
maxBound and checking that afterwards.
Comment thread src/Data/Text/Lazy/Read.hs Resolved
Comment thread src/Data/Text/Read.hs Resolved
Leading zeros don't change the value, so the digits can be folded as
they are; the zeros are only dropped for the length check.
…ents

On 32-bit platforms an exponent may have at most 9 significant
digits, so the large exponent tests now use 1.1e100000000.  The long
exponent tests timed out under emulation (0.12 s against 0.1 s) with
100,000 digits; 10,000 is still far out of range.
@Lotion-3

Copy link
Copy Markdown
Contributor Author

The i386, armv7 and emulated jobs failed on the tests, fixed in 8a61f2f:

  • On 32-bit, maxExponentDigits is 9, so the existing tests with 1.1e1000000000 (10 digits) now give Left "exponent out of range" instead of Infinity/0.0. I changed them to 1.1e100000000. This does mean a 10-digit exponent that fits in an Int32 is now rejected on 32-bit; if you would rather accept every exponent that fits, I can compare the value against maxBound instead of counting digits.
  • Under emulation the 100,000-digit exponent tests took 0.12 s against the 0.1 s timeout; they now use 10,000 digits.

@Lysxia
Lysxia merged commit 7369664 into haskell:master Nov 27, 2026
24 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants