From 49613f505a3b070b44bec2eeef6bdef6afc3a4e2 Mon Sep 17 00:00:00 2001 From: Laksh Agarwal <184763813+Lotion-3@users.noreply.github.com> Date: Mon, 23 Nov 2026 06:09:02 -0500 Subject: [PATCH 1/7] Fix rational and double on exponents beyond Int and on integer mantissas 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. --- src/Data/Text/Internal/Read.hs | 8 ++++++++ src/Data/Text/Lazy/Read.hs | 8 +++----- src/Data/Text/Read.hs | 8 +++----- tests/Tests/Properties/Read.hs | 32 +++++++++++++++++++++++++++++++- 4 files changed, 45 insertions(+), 11 deletions(-) diff --git a/src/Data/Text/Internal/Read.hs b/src/Data/Text/Internal/Read.hs index 17d631543..9df306069 100644 --- a/src/Data/Text/Internal/Read.hs +++ b/src/Data/Text/Internal/Read.hs @@ -13,6 +13,7 @@ module Data.Text.Internal.Read IReader , IParser(..) , T(..) + , clampExponent , digitToInt , hexDigitToInt , perhaps @@ -54,6 +55,13 @@ instance Monad (IParser t) where data T = T !Integer !Int +-- | Clamp the exponent of a floating point number to the range of 'Int'. +-- A larger exponent gives zero or infinity for any mantissa that fits in +-- memory, and 'minBound' is avoided so that 'abs' cannot overflow. +clampExponent :: Integer -> Int +clampExponent = fromInteger . max (negate maxInt) . min maxInt + where maxInt = toInteger (maxBound :: Int) + perhaps :: a -> IParser t a -> IParser t a perhaps def m = P $ \t -> case runP m t of Left _ -> Right (def,t) diff --git a/src/Data/Text/Lazy/Read.hs b/src/Data/Text/Lazy/Read.hs index 9757d2783..f0d66e826 100644 --- a/src/Data/Text/Lazy/Read.hs +++ b/src/Data/Text/Lazy/Read.hs @@ -236,11 +236,9 @@ floaty f = runP $ do n <- P decimal return $ T n digits let e c = c == ord8 'e' || c == ord8 'E' - power <- perhaps 0 (charAscii e >> signa (P decimal) :: Parser Int) - let n = if fracDigits == 0 - then if power == 0 - then fromInteger real - else fromInteger real * (10 ^^ power) + power <- clampExponent <$> perhaps 0 (charAscii e >> signa (P decimal) :: Parser Integer) + let n = if fracDigits == 0 && power == 0 + then fromInteger real else f real fraction (10 ^ fracDigits) power return $! if sign == ord8 '+' then n diff --git a/src/Data/Text/Read.hs b/src/Data/Text/Read.hs index c4c6dae48..fe6a825d4 100644 --- a/src/Data/Text/Read.hs +++ b/src/Data/Text/Read.hs @@ -228,11 +228,9 @@ floaty f = runP $ do n <- P decimal return $ T n digits let e c = c == ord8 'e' || c == ord8 'E' - power <- perhaps 0 (charAscii e >> signa (P decimal) :: Parser Int) - let n = if fracDigits == 0 - then if power == 0 - then fromInteger real - else fromInteger real * (10 ^^ power) + power <- clampExponent <$> perhaps 0 (charAscii e >> signa (P decimal) :: Parser Integer) + let n = if fracDigits == 0 && power == 0 + then fromInteger real else f real fraction (10 ^ fracDigits) power return $! if sign == ord8 '+' then n diff --git a/tests/Tests/Properties/Read.hs b/tests/Tests/Properties/Read.hs index 4b2ed34f4..a1769cb8f 100644 --- a/tests/Tests/Properties/Read.hs +++ b/tests/Tests/Properties/Read.hs @@ -83,6 +83,17 @@ tl_read_double tol n s = where t = TL.dropWhile isFloaty s +-- | Numbers without a fractional part, such as @123e-310@, which 'show' never produces. +t_read_rational_exponent :: Integer -> Int -> Property +t_read_rational_exponent m e = + T.rational (T.pack s) === Right (read s :: Double, mempty) + where s = show m ++ "e" ++ show (e `mod` 800 - 400) + +tl_read_rational_exponent :: Integer -> Int -> Property +tl_read_rational_exponent m e = + TL.rational (TL.pack s) === Right (read s :: Double, mempty) + where s = show m ++ "e" ++ show (e `mod` 800 - 400) + testRead :: TestTree testRead = testGroup "read" [ @@ -103,6 +114,8 @@ testRead = testProperty "t_rational 1.3e-2" (t_read_rational 1.3e-2), testProperty "tl_rational" tl_read_rational, testProperty "tl_rational 9e-3" (tl_read_rational 9e-3), + testProperty "t_rational_exponent" t_read_rational_exponent, + testProperty "tl_rational_exponent" tl_read_rational_exponent, localOption (mkTimeout 100000) $ testGroup "DDoS attacks" [ testProperty "t_double large positive exponent" $ @@ -121,7 +134,24 @@ testRead = testProperty "tl_rational large positive exponent" $ TL.rational (TL.pack "1.1e1000000000") === Right (1 / 0 :: Double, mempty), testProperty "tl_rational large negative exponent" $ - TL.rational (TL.pack "1.1e-1000000000") === Right (0.0 :: Double, mempty) + TL.rational (TL.pack "1.1e-1000000000") === Right (0.0 :: Double, mempty), + + testProperty "t_double exponent out of Int range" $ + map T.double ["1e9223372036854775808", "1e18446744073709551616", "1e-9223372036854775808"] + === [Right (1 / 0, mempty), Right (1 / 0, mempty), Right (0.0, mempty)], + testProperty "tl_double exponent out of Int range" $ + map TL.double ["1e9223372036854775808", "1e18446744073709551616", "1e-9223372036854775808"] + === [Right (1 / 0, mempty), Right (1 / 0, mempty), Right (0.0, mempty)], + testProperty "t_rational exponent out of Int range" $ + map T.rational ["1e9223372036854775808", "1e18446744073709551616", "1e-9223372036854775808"] + === [Right (1 / 0 :: Double, mempty), Right (1 / 0, mempty), Right (0.0, mempty)], + testProperty "tl_rational exponent out of Int range" $ + map TL.rational ["1e9223372036854775808", "1e18446744073709551616", "1e-9223372036854775808"] + === [Right (1 / 0 :: Double, mempty), Right (1 / 0, mempty), Right (0.0, mempty)], + testProperty "t_double long exponent" $ + T.double (T.pack ("1e" ++ replicate 100000 '9')) === Right (1 / 0, mempty), + testProperty "tl_double long exponent" $ + TL.double (TL.pack ("1e-" ++ replicate 100000 '9')) === Right (0.0, mempty) ] ] From b9f33829a401c37b257c8a29e995c7c4e20fec89 Mon Sep 17 00:00:00 2001 From: Laksh Agarwal <184763813+Lotion-3@users.noreply.github.com> Date: Tue, 24 Nov 2026 00:37:52 -0500 Subject: [PATCH 2/7] Build the exact value of rational from integers 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. --- src/Data/Text/Lazy/Read.hs | 5 ++++- src/Data/Text/Read.hs | 5 ++++- 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/src/Data/Text/Lazy/Read.hs b/src/Data/Text/Lazy/Read.hs index f0d66e826..9ed466952 100644 --- a/src/Data/Text/Lazy/Read.hs +++ b/src/Data/Text/Lazy/Read.hs @@ -185,7 +185,10 @@ rational = floaty $ \real frac fracDenom power -> -- the small loss of precision for Double does not matter much because the result is -- likely infinity or zero anyway. if abs power < 1000 - then fromRational ((real % 1 + frac % fracDenom) * (10 ^^ power)) + then let n = real * fracDenom + frac + in fromRational $ if power >= 0 + then (n * 10 ^ power) % fracDenom + else n % (fracDenom * 10 ^ negate power) else fromRational (real % 1 + frac % fracDenom) * (10 ^^ power) -- | Read a rational number. diff --git a/src/Data/Text/Read.hs b/src/Data/Text/Read.hs index fe6a825d4..0b8a6c153 100644 --- a/src/Data/Text/Read.hs +++ b/src/Data/Text/Read.hs @@ -180,7 +180,10 @@ rational = floaty $ \real frac fracDenom power -> -- the small loss of precision for Double does not matter much because the result is -- likely infinity or zero anyway. if abs power < 1000 - then fromRational ((real % 1 + frac % fracDenom) * (10 ^^ power)) + then let n = real * fracDenom + frac + in fromRational $ if power >= 0 + then (n * 10 ^ power) % fracDenom + else n % (fracDenom * 10 ^ negate power) else fromRational (real % 1 + frac % fracDenom) * (10 ^^ power) -- | Read a rational number. From 487e53d1fc1f041c37242af52a75b1e3c9a0861c Mon Sep 17 00:00:00 2001 From: Laksh Agarwal <184763813+Lotion-3@users.noreply.github.com> Date: Wed, 25 Nov 2026 01:43:33 -0500 Subject: [PATCH 3/7] Make an exponent outside the range of Int an error 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. --- src/Data/Text/Internal/Read.hs | 24 ++++++++++++------ src/Data/Text/Lazy/Read.hs | 16 ++++++++++-- src/Data/Text/Read.hs | 16 ++++++++++-- tests/Tests/Properties/Read.hs | 46 +++++++++++++++++++++++++--------- 4 files changed, 79 insertions(+), 23 deletions(-) diff --git a/src/Data/Text/Internal/Read.hs b/src/Data/Text/Internal/Read.hs index 9df306069..05a9cd94c 100644 --- a/src/Data/Text/Internal/Read.hs +++ b/src/Data/Text/Internal/Read.hs @@ -13,10 +13,11 @@ module Data.Text.Internal.Read IReader , IParser(..) , T(..) - , clampExponent , digitToInt , hexDigitToInt + , maxExponentDigits , perhaps + , toExponent ) where import Control.Applicative as App (Applicative(..)) @@ -55,12 +56,21 @@ instance Monad (IParser t) where data T = T !Integer !Int --- | Clamp the exponent of a floating point number to the range of 'Int'. --- A larger exponent gives zero or infinity for any mantissa that fits in --- memory, and 'minBound' is avoided so that 'abs' cannot overflow. -clampExponent :: Integer -> Int -clampExponent = fromInteger . max (negate maxInt) . min maxInt - where maxInt = toInteger (maxBound :: Int) +-- | The number of digits of 'maxBound' :: 'Int'. An exponent with more +-- significant digits is out of range. +maxExponentDigits :: Int +maxExponentDigits = length (show (maxBound :: Int)) + +-- | Fail if an exponent is outside the range of 'Int'. 'minBound' is +-- replaced by @-'maxBound'@, which gives the same result, so that +-- @10 '^^' power@ cannot overflow. +toExponent :: Integer -> IParser t Int +toExponent n + | n < negate (maxInt + 1) || n > maxInt = P $ \_ -> Left "exponent out of range" + | otherwise = App.pure (fromInteger (max (negate maxInt) n)) + +maxInt :: Integer +maxInt = toInteger (maxBound :: Int) perhaps :: a -> IParser t a -> IParser t a perhaps def m = P $ \t -> case runP m t of diff --git a/src/Data/Text/Lazy/Read.hs b/src/Data/Text/Lazy/Read.hs index 9ed466952..240a1c9ce 100644 --- a/src/Data/Text/Lazy/Read.hs +++ b/src/Data/Text/Lazy/Read.hs @@ -175,6 +175,7 @@ signed f = runP (signa (P f)) -- -- >rational "3.foo" == Right (3.0, ".foo") -- >rational "3e" == Right (3.0, "e") +-- >rational "1e9223372036854775808" == Left "exponent out of range" rational :: Fractional a => Reader a {-# SPECIALIZE rational :: Reader Double #-} rational = floaty $ \real frac fracDenom power -> @@ -184,7 +185,7 @@ rational = floaty $ \real frac fracDenom power -> -- Otherwise divide first, apply fromRational and scale last: -- the small loss of precision for Double does not matter much because the result is -- likely infinity or zero anyway. - if abs power < 1000 + if power > -1000 && power < 1000 then let n = real * fracDenom + frac in fromRational $ if power >= 0 then (n * 10 ^ power) % fracDenom @@ -239,7 +240,7 @@ floaty f = runP $ do n <- P decimal return $ T n digits let e c = c == ord8 'e' || c == ord8 'E' - power <- clampExponent <$> perhaps 0 (charAscii e >> signa (P decimal) :: Parser Integer) + power <- toExponent =<< perhaps 0 (charAscii e >> signa (P exponentDigits)) let n = if fracDigits == 0 && power == 0 then fromInteger real else f real fraction (10 ^ fracDigits) power @@ -250,6 +251,17 @@ floaty f = runP $ do int64ToInt :: Int64 -> Int int64ToInt = fromIntegral +-- | Like 'decimal', but digits beyond the range of 'Int' are not converted, +-- so a long exponent is read in linear time. Such an exponent is replaced by +-- a power of 10 that is also out of range. +exponentDigits :: Reader Integer +exponentDigits txt + | T.null h = Left "input does not start with a digit" + | T.compareLength significant (fromIntegral maxExponentDigits) == GT = Right (10 ^ maxExponentDigits, t) + | otherwise = Right (T.foldl' (\n d -> n * 10 + toInteger (digitToInt d)) 0 significant, t) + where (# h, t #) = spanAscii_ (\w -> w - ord8 '0' < 10) txt + significant = T.dropWhile (== '0') h + ord8 :: Char -> Word8 ord8 = fromIntegral . ord diff --git a/src/Data/Text/Read.hs b/src/Data/Text/Read.hs index 0b8a6c153..791b8f939 100644 --- a/src/Data/Text/Read.hs +++ b/src/Data/Text/Read.hs @@ -170,6 +170,7 @@ signed f = runP (signa (P f)) -- -- >rational "3.foo" == Right (3.0, ".foo") -- >rational "3e" == Right (3.0, "e") +-- >rational "1e9223372036854775808" == Left "exponent out of range" rational :: Fractional a => Reader a {-# SPECIALIZE rational :: Reader Double #-} rational = floaty $ \real frac fracDenom power -> @@ -179,7 +180,7 @@ rational = floaty $ \real frac fracDenom power -> -- Otherwise divide first, apply fromRational and scale last: -- the small loss of precision for Double does not matter much because the result is -- likely infinity or zero anyway. - if abs power < 1000 + if power > -1000 && power < 1000 then let n = real * fracDenom + frac in fromRational $ if power >= 0 then (n * 10 ^ power) % fracDenom @@ -231,7 +232,7 @@ floaty f = runP $ do n <- P decimal return $ T n digits let e c = c == ord8 'e' || c == ord8 'E' - power <- clampExponent <$> perhaps 0 (charAscii e >> signa (P decimal) :: Parser Integer) + power <- toExponent =<< perhaps 0 (charAscii e >> signa (P exponentDigits)) let n = if fracDigits == 0 && power == 0 then fromInteger real else f real fraction (10 ^ fracDigits) power @@ -239,5 +240,16 @@ floaty f = runP $ do then n else -n +-- | Like 'decimal', but digits beyond the range of 'Int' are not converted, +-- so a long exponent is read in linear time. Such an exponent is replaced by +-- a power of 10 that is also out of range. +exponentDigits :: Reader Integer +exponentDigits txt + | T.null h = Left "input does not start with a digit" + | T.compareLength significant (maxExponentDigits) == GT = Right (10 ^ maxExponentDigits, t) + | otherwise = Right (T.foldl' (\n d -> n * 10 + toInteger (digitToInt d)) 0 significant, t) + where (# h,t #) = spanAscii_ (\w -> w - ord8 '0' < 10) txt + significant = T.dropWhile (== '0') h + ord8 :: Char -> Word8 ord8 = fromIntegral . ord diff --git a/tests/Tests/Properties/Read.hs b/tests/Tests/Properties/Read.hs index a1769cb8f..35de67b34 100644 --- a/tests/Tests/Properties/Read.hs +++ b/tests/Tests/Properties/Read.hs @@ -94,6 +94,18 @@ tl_read_rational_exponent m e = TL.rational (TL.pack s) === Right (read s :: Double, mempty) where s = show m ++ "e" ++ show (e `mod` 800 - 400) +-- | Numbers with the largest and smallest exponents in the range of 'Int'. +atIntBounds :: [String] +atIntBounds = ["1e" ++ show (maxBound :: Int), "1e" ++ show (minBound :: Int)] + +-- | Numbers with exponents just outside the range of 'Int', and far outside it. +outOfIntRange :: [String] +outOfIntRange = + [ "1e" ++ show (maxInt + 1), "1e" ++ show (negate maxInt - 2) + , "1e" ++ show (maxInt + 1) ++ "0", "1e-" ++ show (maxInt + 1) ++ "0" + , "1e18446744073709551616" ] + where maxInt = toInteger (maxBound :: Int) + testRead :: TestTree testRead = testGroup "read" [ @@ -136,22 +148,32 @@ testRead = testProperty "tl_rational large negative exponent" $ TL.rational (TL.pack "1.1e-1000000000") === Right (0.0 :: Double, mempty), + testProperty "t_double exponent at the Int bounds" $ + map (T.double . T.pack) atIntBounds === [Right (1 / 0, mempty), Right (0.0, mempty)], + testProperty "tl_double exponent at the Int bounds" $ + map (TL.double . TL.pack) atIntBounds === [Right (1 / 0, mempty), Right (0.0, mempty)], + testProperty "t_rational exponent at the Int bounds" $ + map (T.rational . T.pack) atIntBounds === [Right (1 / 0 :: Double, mempty), Right (0.0, mempty)], + testProperty "tl_rational exponent at the Int bounds" $ + map (TL.rational . TL.pack) atIntBounds === [Right (1 / 0 :: Double, mempty), Right (0.0, mempty)], + testProperty "t_double exponent out of Int range" $ - map T.double ["1e9223372036854775808", "1e18446744073709551616", "1e-9223372036854775808"] - === [Right (1 / 0, mempty), Right (1 / 0, mempty), Right (0.0, mempty)], + conjoin [T.double (T.pack t) === Left "exponent out of range" | t <- outOfIntRange], testProperty "tl_double exponent out of Int range" $ - map TL.double ["1e9223372036854775808", "1e18446744073709551616", "1e-9223372036854775808"] - === [Right (1 / 0, mempty), Right (1 / 0, mempty), Right (0.0, mempty)], + conjoin [TL.double (TL.pack t) === Left "exponent out of range" | t <- outOfIntRange], testProperty "t_rational exponent out of Int range" $ - map T.rational ["1e9223372036854775808", "1e18446744073709551616", "1e-9223372036854775808"] - === [Right (1 / 0 :: Double, mempty), Right (1 / 0, mempty), Right (0.0, mempty)], + conjoin [T.rational (T.pack t) === (Left "exponent out of range" :: Either String (Double, T.Text)) | t <- outOfIntRange], testProperty "tl_rational exponent out of Int range" $ - map TL.rational ["1e9223372036854775808", "1e18446744073709551616", "1e-9223372036854775808"] - === [Right (1 / 0 :: Double, mempty), Right (1 / 0, mempty), Right (0.0, mempty)], - testProperty "t_double long exponent" $ - T.double (T.pack ("1e" ++ replicate 100000 '9')) === Right (1 / 0, mempty), - testProperty "tl_double long exponent" $ - TL.double (TL.pack ("1e-" ++ replicate 100000 '9')) === Right (0.0, mempty) + conjoin [TL.rational (TL.pack t) === (Left "exponent out of range" :: Either String (Double, TL.Text)) | t <- outOfIntRange], + + testProperty "t_double long exponent" $ once $ + T.double (T.append "1e" (T.replicate 100000 "9")) === Left "exponent out of range", + testProperty "tl_double long exponent" $ once $ + TL.double (TL.append "1e-" (TL.replicate 100000 "9")) === Left "exponent out of range", + testProperty "t_double exponent with many leading zeros" $ once $ + T.double (T.concat ["1e-", T.replicate 100000 "0", "3"]) === Right (1.0e-3, mempty), + testProperty "tl_double exponent with many leading zeros" $ once $ + TL.double (TL.concat ["1e", TL.replicate 100000 "0", "3"]) === Right (1000.0, mempty) ] ] From a0a479e2dba6689790a67f4c98f2879fcfaf8a8c Mon Sep 17 00:00:00 2001 From: Laksh Agarwal <184763813+Lotion-3@users.noreply.github.com> Date: Thu, 26 Nov 2026 00:49:23 -0500 Subject: [PATCH 4/7] Read exponents as Int, with as many digits as always fit 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". --- src/Data/Text/Internal/Read.hs | 26 +++++++-------- src/Data/Text/Lazy/Read.hs | 16 +++++----- src/Data/Text/Read.hs | 16 +++++----- tests/Tests/Properties/Read.hs | 58 +++++++++++++++++++--------------- 4 files changed, 60 insertions(+), 56 deletions(-) diff --git a/src/Data/Text/Internal/Read.hs b/src/Data/Text/Internal/Read.hs index 05a9cd94c..a05edac05 100644 --- a/src/Data/Text/Internal/Read.hs +++ b/src/Data/Text/Internal/Read.hs @@ -17,12 +17,13 @@ module Data.Text.Internal.Read , hexDigitToInt , maxExponentDigits , perhaps - , toExponent + , checkExponent ) where import Control.Applicative as App (Applicative(..)) import Control.Arrow (first) import Control.Monad (ap) +import Data.Bits (finiteBitSize) import Data.Char (ord) type IReader t a = t -> Either String (a,t) @@ -56,21 +57,18 @@ instance Monad (IParser t) where data T = T !Integer !Int --- | The number of digits of 'maxBound' :: 'Int'. An exponent with more +-- | The number of digits that always fit in an 'Int'. An exponent with more -- significant digits is out of range. maxExponentDigits :: Int -maxExponentDigits = length (show (maxBound :: Int)) - --- | Fail if an exponent is outside the range of 'Int'. 'minBound' is --- replaced by @-'maxBound'@, which gives the same result, so that --- @10 '^^' power@ cannot overflow. -toExponent :: Integer -> IParser t Int -toExponent n - | n < negate (maxInt + 1) || n > maxInt = P $ \_ -> Left "exponent out of range" - | otherwise = App.pure (fromInteger (max (negate maxInt) n)) - -maxInt :: Integer -maxInt = toInteger (maxBound :: Int) +maxExponentDigits = (finiteBitSize (0 :: Int) - 1) * 3 `div` 10 + +-- | Fail on an exponent that is out of range. Its digits are read as +-- 'maxBound', which no exponent in range can reach, and checked here because +-- the 'perhaps' that allows a number without an exponent would catch the error. +checkExponent :: Int -> IParser t Int +checkExponent n + | abs n == maxBound = P $ \_ -> Left "exponent out of range" + | otherwise = App.pure n perhaps :: a -> IParser t a -> IParser t a perhaps def m = P $ \t -> case runP m t of diff --git a/src/Data/Text/Lazy/Read.hs b/src/Data/Text/Lazy/Read.hs index 240a1c9ce..1d091055d 100644 --- a/src/Data/Text/Lazy/Read.hs +++ b/src/Data/Text/Lazy/Read.hs @@ -185,7 +185,7 @@ rational = floaty $ \real frac fracDenom power -> -- Otherwise divide first, apply fromRational and scale last: -- the small loss of precision for Double does not matter much because the result is -- likely infinity or zero anyway. - if power > -1000 && power < 1000 + if - 1000 < power && power < 1000 then let n = real * fracDenom + frac in fromRational $ if power >= 0 then (n * 10 ^ power) % fracDenom @@ -240,7 +240,7 @@ floaty f = runP $ do n <- P decimal return $ T n digits let e c = c == ord8 'e' || c == ord8 'E' - power <- toExponent =<< perhaps 0 (charAscii e >> signa (P exponentDigits)) + power <- checkExponent =<< perhaps 0 (charAscii e >> signa (P exponentDigits)) let n = if fracDigits == 0 && power == 0 then fromInteger real else f real fraction (10 ^ fracDigits) power @@ -251,14 +251,14 @@ floaty f = runP $ do int64ToInt :: Int64 -> Int int64ToInt = fromIntegral --- | Like 'decimal', but digits beyond the range of 'Int' are not converted, --- so a long exponent is read in linear time. Such an exponent is replaced by --- a power of 10 that is also out of range. -exponentDigits :: Reader Integer +-- | Like 'decimal', but an exponent with more than 'maxExponentDigits' +-- significant digits is not converted, so a long exponent is read in linear +-- time. It is read as 'maxBound', which 'checkExponent' rejects. +exponentDigits :: Reader Int exponentDigits txt | T.null h = Left "input does not start with a digit" - | T.compareLength significant (fromIntegral maxExponentDigits) == GT = Right (10 ^ maxExponentDigits, t) - | otherwise = Right (T.foldl' (\n d -> n * 10 + toInteger (digitToInt d)) 0 significant, t) + | T.compareLength significant (fromIntegral maxExponentDigits) == GT = Right (maxBound, t) + | otherwise = Right (T.foldl' (\n d -> n * 10 + digitToInt d) 0 significant, t) where (# h, t #) = spanAscii_ (\w -> w - ord8 '0' < 10) txt significant = T.dropWhile (== '0') h diff --git a/src/Data/Text/Read.hs b/src/Data/Text/Read.hs index 791b8f939..a14a1297e 100644 --- a/src/Data/Text/Read.hs +++ b/src/Data/Text/Read.hs @@ -180,7 +180,7 @@ rational = floaty $ \real frac fracDenom power -> -- Otherwise divide first, apply fromRational and scale last: -- the small loss of precision for Double does not matter much because the result is -- likely infinity or zero anyway. - if power > -1000 && power < 1000 + if - 1000 < power && power < 1000 then let n = real * fracDenom + frac in fromRational $ if power >= 0 then (n * 10 ^ power) % fracDenom @@ -232,7 +232,7 @@ floaty f = runP $ do n <- P decimal return $ T n digits let e c = c == ord8 'e' || c == ord8 'E' - power <- toExponent =<< perhaps 0 (charAscii e >> signa (P exponentDigits)) + power <- checkExponent =<< perhaps 0 (charAscii e >> signa (P exponentDigits)) let n = if fracDigits == 0 && power == 0 then fromInteger real else f real fraction (10 ^ fracDigits) power @@ -240,14 +240,14 @@ floaty f = runP $ do then n else -n --- | Like 'decimal', but digits beyond the range of 'Int' are not converted, --- so a long exponent is read in linear time. Such an exponent is replaced by --- a power of 10 that is also out of range. -exponentDigits :: Reader Integer +-- | Like 'decimal', but an exponent with more than 'maxExponentDigits' +-- significant digits is not converted, so a long exponent is read in linear +-- time. It is read as 'maxBound', which 'checkExponent' rejects. +exponentDigits :: Reader Int exponentDigits txt | T.null h = Left "input does not start with a digit" - | T.compareLength significant (maxExponentDigits) == GT = Right (10 ^ maxExponentDigits, t) - | otherwise = Right (T.foldl' (\n d -> n * 10 + toInteger (digitToInt d)) 0 significant, t) + | TU.lengthWord8 significant > maxExponentDigits = Right (maxBound, t) + | otherwise = Right (T.foldl' (\n d -> n * 10 + digitToInt d) 0 significant, t) where (# h,t #) = spanAscii_ (\w -> w - ord8 '0' < 10) txt significant = T.dropWhile (== '0') h diff --git a/tests/Tests/Properties/Read.hs b/tests/Tests/Properties/Read.hs index 35de67b34..9717ee73e 100644 --- a/tests/Tests/Properties/Read.hs +++ b/tests/Tests/Properties/Read.hs @@ -6,6 +6,7 @@ module Tests.Properties.Read ( testRead ) where +import Data.Bits (finiteBitSize) import Data.Char (isDigit, isHexDigit) import Numeric (showHex) import Test.Tasty (TestTree, testGroup, localOption, mkTimeout) @@ -94,17 +95,22 @@ tl_read_rational_exponent m e = TL.rational (TL.pack s) === Right (read s :: Double, mempty) where s = show m ++ "e" ++ show (e `mod` 800 - 400) --- | Numbers with the largest and smallest exponents in the range of 'Int'. -atIntBounds :: [String] -atIntBounds = ["1e" ++ show (maxBound :: Int), "1e" ++ show (minBound :: Int)] +-- | The number of exponent digits that always fit in an 'Int'. +exponentDigits :: Int +exponentDigits = (finiteBitSize (0 :: Int) - 1) * 3 `div` 10 --- | Numbers with exponents just outside the range of 'Int', and far outside it. -outOfIntRange :: [String] -outOfIntRange = - [ "1e" ++ show (maxInt + 1), "1e" ++ show (negate maxInt - 2) - , "1e" ++ show (maxInt + 1) ++ "0", "1e-" ++ show (maxInt + 1) ++ "0" +-- | Numbers with the largest and smallest exponents that are in range. +atExponentBounds :: [String] +atExponentBounds = ["1e" ++ nines, "1e-" ++ nines] + where nines = replicate exponentDigits '9' + +-- | Numbers with exponents just out of range, and far out of range. +outOfExponentRange :: [String] +outOfExponentRange = + [ "1e1" ++ zeros, "1e-1" ++ zeros + , "1e" ++ show (maxBound :: Int), "1e" ++ show (minBound :: Int) , "1e18446744073709551616" ] - where maxInt = toInteger (maxBound :: Int) + where zeros = replicate exponentDigits '0' testRead :: TestTree testRead = @@ -148,23 +154,23 @@ testRead = testProperty "tl_rational large negative exponent" $ TL.rational (TL.pack "1.1e-1000000000") === Right (0.0 :: Double, mempty), - testProperty "t_double exponent at the Int bounds" $ - map (T.double . T.pack) atIntBounds === [Right (1 / 0, mempty), Right (0.0, mempty)], - testProperty "tl_double exponent at the Int bounds" $ - map (TL.double . TL.pack) atIntBounds === [Right (1 / 0, mempty), Right (0.0, mempty)], - testProperty "t_rational exponent at the Int bounds" $ - map (T.rational . T.pack) atIntBounds === [Right (1 / 0 :: Double, mempty), Right (0.0, mempty)], - testProperty "tl_rational exponent at the Int bounds" $ - map (TL.rational . TL.pack) atIntBounds === [Right (1 / 0 :: Double, mempty), Right (0.0, mempty)], - - testProperty "t_double exponent out of Int range" $ - conjoin [T.double (T.pack t) === Left "exponent out of range" | t <- outOfIntRange], - testProperty "tl_double exponent out of Int range" $ - conjoin [TL.double (TL.pack t) === Left "exponent out of range" | t <- outOfIntRange], - testProperty "t_rational exponent out of Int range" $ - conjoin [T.rational (T.pack t) === (Left "exponent out of range" :: Either String (Double, T.Text)) | t <- outOfIntRange], - testProperty "tl_rational exponent out of Int range" $ - conjoin [TL.rational (TL.pack t) === (Left "exponent out of range" :: Either String (Double, TL.Text)) | t <- outOfIntRange], + testProperty "t_double exponent at the bounds" $ + map (T.double . T.pack) atExponentBounds === [Right (1 / 0, mempty), Right (0.0, mempty)], + testProperty "tl_double exponent at the bounds" $ + map (TL.double . TL.pack) atExponentBounds === [Right (1 / 0, mempty), Right (0.0, mempty)], + testProperty "t_rational exponent at the bounds" $ + map (T.rational . T.pack) atExponentBounds === [Right (1 / 0 :: Double, mempty), Right (0.0, mempty)], + testProperty "tl_rational exponent at the bounds" $ + map (TL.rational . TL.pack) atExponentBounds === [Right (1 / 0 :: Double, mempty), Right (0.0, mempty)], + + testProperty "t_double exponent out of range" $ + conjoin [T.double (T.pack t) === Left "exponent out of range" | t <- outOfExponentRange], + testProperty "tl_double exponent out of range" $ + conjoin [TL.double (TL.pack t) === Left "exponent out of range" | t <- outOfExponentRange], + testProperty "t_rational exponent out of range" $ + conjoin [T.rational (T.pack t) === (Left "exponent out of range" :: Either String (Double, T.Text)) | t <- outOfExponentRange], + testProperty "tl_rational exponent out of range" $ + conjoin [TL.rational (TL.pack t) === (Left "exponent out of range" :: Either String (Double, TL.Text)) | t <- outOfExponentRange], testProperty "t_double long exponent" $ once $ T.double (T.append "1e" (T.replicate 100000 "9")) === Left "exponent out of range", From 66a38dd1e3373f55d049224170127e9210587e88 Mon Sep 17 00:00:00 2001 From: Laksh Agarwal <184763813+Lotion-3@users.noreply.github.com> Date: Thu, 26 Nov 2026 05:41:26 -0500 Subject: [PATCH 5/7] Parse the exponent without a sentinel value 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. --- src/Data/Text/Internal/Read.hs | 30 ++++++++++++++++++--------- src/Data/Text/Lazy/Read.hs | 37 +++++++++++++++++++--------------- src/Data/Text/Read.hs | 37 +++++++++++++++++++--------------- 3 files changed, 63 insertions(+), 41 deletions(-) diff --git a/src/Data/Text/Internal/Read.hs b/src/Data/Text/Internal/Read.hs index a05edac05..b792673fc 100644 --- a/src/Data/Text/Internal/Read.hs +++ b/src/Data/Text/Internal/Read.hs @@ -17,7 +17,10 @@ module Data.Text.Internal.Read , hexDigitToInt , maxExponentDigits , perhaps - , checkExponent + , catchOrBind + , err + , snapshot + , reset ) where import Control.Applicative as App (Applicative(..)) @@ -43,7 +46,7 @@ instance Applicative (IParser t) where instance Monad (IParser t) where return = App.pure m >>= k = P $ \t -> case runP m t of - Left err -> Left err + Left msg -> Left msg Right (a,t') -> runP (k a) t' {-# INLINE (>>=) #-} @@ -62,13 +65,22 @@ data T = T !Integer !Int maxExponentDigits :: Int maxExponentDigits = (finiteBitSize (0 :: Int) - 1) * 3 `div` 10 --- | Fail on an exponent that is out of range. Its digits are read as --- 'maxBound', which no exponent in range can reach, and checked here because --- the 'perhaps' that allows a number without an exponent would catch the error. -checkExponent :: Int -> IParser t Int -checkExponent n - | abs n == maxBound = P $ \_ -> Left "exponent out of range" - | otherwise = App.pure n +-- | @catchOrBind p q k@ runs @p@; if it fails, runs @q@ on the same input +-- instead, and otherwise continues with @k@, whose failures are not caught. +catchOrBind :: IParser t a -> IParser t b -> (a -> IParser t b) -> IParser t b +catchOrBind p q k = P $ \t -> case runP p t of + Left _ -> runP q t + Right (a,t') -> runP (k a) t' + +err :: String -> IParser t a +err msg = P $ \_ -> Left msg + +-- | The remaining input, to go back to with 'reset'. +snapshot :: IParser t t +snapshot = P $ \t -> Right (t,t) + +reset :: t -> a -> IParser t a +reset t a = P $ \_ -> Right (a,t) perhaps :: a -> IParser t a -> IParser t a perhaps def m = P $ \t -> case runP m t of diff --git a/src/Data/Text/Lazy/Read.hs b/src/Data/Text/Lazy/Read.hs index 1d091055d..b25843398 100644 --- a/src/Data/Text/Lazy/Read.hs +++ b/src/Data/Text/Lazy/Read.hs @@ -217,8 +217,14 @@ signa :: Num a => Parser a -> Parser a {-# SPECIALIZE signa :: Parser Int64 -> Parser Int64 #-} {-# SPECIALIZE signa :: Parser Integer -> Parser Integer #-} signa p = do - sign <- perhaps (ord8 '+') $ charAscii (\c -> c == ord8 '-' || c == ord8 '+') - if sign == ord8 '+' then p else negate `liftM` p + sw <- sign + if sw == ord8 '+' then p else negate `liftM` p + +sign :: Parser Word8 +sign = perhaps (ord8 '+') $ charAscii (\c -> c == ord8 '-' || c == ord8 '+') + +asciiDigits :: Parser Text +asciiDigits = P $ \txt -> let (# h, t #) = spanAscii_ (\w -> w - ord8 '0' < 10) txt in Right (h, t) charAscii :: (Word8 -> Bool) -> Parser Word8 charAscii p = P $ \case @@ -232,7 +238,7 @@ charAscii p = P $ \case floaty :: Fractional a => (Integer -> Integer -> Integer -> Int -> a) -> Reader a {-# INLINE floaty #-} floaty f = runP $ do - sign <- perhaps (ord8 '+') $ charAscii (\c -> c == ord8 '-' || c == ord8 '+') + sw <- sign real <- P decimal T fraction fracDigits <- perhaps (T 0 0) $ do _ <- charAscii (== ord8 '.') @@ -240,28 +246,27 @@ floaty f = runP $ do n <- P decimal return $ T n digits let e c = c == ord8 'e' || c == ord8 'E' - power <- checkExponent =<< perhaps 0 (charAscii e >> signa (P exponentDigits)) + beforeE <- snapshot + power <- catchOrBind (charAscii e) (pure 0) $ \_ -> do + esw <- sign + ds <- asciiDigits + let significant = T.dropWhile (== '0') ds + if T.null ds + then reset beforeE 0 + else if T.compareLength significant (fromIntegral maxExponentDigits) == GT + then err "exponent out of range" + else pure $ (if esw == ord8 '+' then id else negate) + (T.foldl' (\n d -> n * 10 + digitToInt d) 0 significant) let n = if fracDigits == 0 && power == 0 then fromInteger real else f real fraction (10 ^ fracDigits) power - return $! if sign == ord8 '+' + return $! if sw == ord8 '+' then n else -n int64ToInt :: Int64 -> Int int64ToInt = fromIntegral --- | Like 'decimal', but an exponent with more than 'maxExponentDigits' --- significant digits is not converted, so a long exponent is read in linear --- time. It is read as 'maxBound', which 'checkExponent' rejects. -exponentDigits :: Reader Int -exponentDigits txt - | T.null h = Left "input does not start with a digit" - | T.compareLength significant (fromIntegral maxExponentDigits) == GT = Right (maxBound, t) - | otherwise = Right (T.foldl' (\n d -> n * 10 + digitToInt d) 0 significant, t) - where (# h, t #) = spanAscii_ (\w -> w - ord8 '0' < 10) txt - significant = T.dropWhile (== '0') h - ord8 :: Char -> Word8 ord8 = fromIntegral . ord diff --git a/src/Data/Text/Read.hs b/src/Data/Text/Read.hs index a14a1297e..48b2d7209 100644 --- a/src/Data/Text/Read.hs +++ b/src/Data/Text/Read.hs @@ -212,8 +212,14 @@ signa :: Num a => Parser a -> Parser a {-# SPECIALIZE signa :: Parser Int64 -> Parser Int64 #-} {-# SPECIALIZE signa :: Parser Integer -> Parser Integer #-} signa p = do - sign <- perhaps (ord8 '+') $ charAscii (\c -> c == ord8 '-' || c == ord8 '+') - if sign == ord8 '+' then p else negate `liftM` p + sw <- sign + if sw == ord8 '+' then p else negate `liftM` p + +sign :: Parser Word8 +sign = perhaps (ord8 '+') $ charAscii (\c -> c == ord8 '-' || c == ord8 '+') + +asciiDigits :: Parser Text +asciiDigits = P $ \txt -> let (# h, t #) = spanAscii_ (\w -> w - ord8 '0' < 10) txt in Right (h, t) charAscii :: (Word8 -> Bool) -> Parser Word8 charAscii p = P $ \(Text arr off len) -> let c = A.unsafeIndex arr off in @@ -224,7 +230,7 @@ charAscii p = P $ \(Text arr off len) -> let c = A.unsafeIndex arr off in floaty :: Fractional a => (Integer -> Integer -> Integer -> Int -> a) -> Reader a {-# INLINE floaty #-} floaty f = runP $ do - sign <- perhaps (ord8 '+') $ charAscii (\c -> c == ord8 '-' || c == ord8 '+') + sw <- sign real <- P decimal T fraction fracDigits <- perhaps (T 0 0) $ do _ <- charAscii (== ord8 '.') @@ -232,24 +238,23 @@ floaty f = runP $ do n <- P decimal return $ T n digits let e c = c == ord8 'e' || c == ord8 'E' - power <- checkExponent =<< perhaps 0 (charAscii e >> signa (P exponentDigits)) + beforeE <- snapshot + power <- catchOrBind (charAscii e) (pure 0) $ \_ -> do + esw <- sign + ds <- asciiDigits + let significant = T.dropWhile (== '0') ds + if T.null ds + then reset beforeE 0 + else if TU.lengthWord8 significant > maxExponentDigits + then err "exponent out of range" + else pure $ (if esw == ord8 '+' then id else negate) + (T.foldl' (\n d -> n * 10 + digitToInt d) 0 significant) let n = if fracDigits == 0 && power == 0 then fromInteger real else f real fraction (10 ^ fracDigits) power - return $! if sign == ord8 '+' + return $! if sw == ord8 '+' then n else -n --- | Like 'decimal', but an exponent with more than 'maxExponentDigits' --- significant digits is not converted, so a long exponent is read in linear --- time. It is read as 'maxBound', which 'checkExponent' rejects. -exponentDigits :: Reader Int -exponentDigits txt - | T.null h = Left "input does not start with a digit" - | TU.lengthWord8 significant > maxExponentDigits = Right (maxBound, t) - | otherwise = Right (T.foldl' (\n d -> n * 10 + digitToInt d) 0 significant, t) - where (# h,t #) = spanAscii_ (\w -> w - ord8 '0' < 10) txt - significant = T.dropWhile (== '0') h - ord8 :: Char -> Word8 ord8 = fromIntegral . ord From bce9b8b5974ea93ad5deb6ed404790d13e6873a1 Mon Sep 17 00:00:00 2001 From: Laksh Agarwal <184763813+Lotion-3@users.noreply.github.com> Date: Thu, 26 Nov 2026 05:58:53 -0500 Subject: [PATCH 6/7] Inline the significant exponent digits 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. --- src/Data/Text/Lazy/Read.hs | 5 ++--- src/Data/Text/Read.hs | 5 ++--- 2 files changed, 4 insertions(+), 6 deletions(-) diff --git a/src/Data/Text/Lazy/Read.hs b/src/Data/Text/Lazy/Read.hs index b25843398..60691af56 100644 --- a/src/Data/Text/Lazy/Read.hs +++ b/src/Data/Text/Lazy/Read.hs @@ -250,13 +250,12 @@ floaty f = runP $ do power <- catchOrBind (charAscii e) (pure 0) $ \_ -> do esw <- sign ds <- asciiDigits - let significant = T.dropWhile (== '0') ds if T.null ds then reset beforeE 0 - else if T.compareLength significant (fromIntegral maxExponentDigits) == GT + else if T.compareLength (T.dropWhile (== '0') ds) (fromIntegral maxExponentDigits) == GT then err "exponent out of range" else pure $ (if esw == ord8 '+' then id else negate) - (T.foldl' (\n d -> n * 10 + digitToInt d) 0 significant) + (T.foldl' (\n d -> n * 10 + digitToInt d) 0 ds) let n = if fracDigits == 0 && power == 0 then fromInteger real else f real fraction (10 ^ fracDigits) power diff --git a/src/Data/Text/Read.hs b/src/Data/Text/Read.hs index 48b2d7209..7b20fee82 100644 --- a/src/Data/Text/Read.hs +++ b/src/Data/Text/Read.hs @@ -242,13 +242,12 @@ floaty f = runP $ do power <- catchOrBind (charAscii e) (pure 0) $ \_ -> do esw <- sign ds <- asciiDigits - let significant = T.dropWhile (== '0') ds if T.null ds then reset beforeE 0 - else if TU.lengthWord8 significant > maxExponentDigits + else if TU.lengthWord8 (T.dropWhile (== '0') ds) > maxExponentDigits then err "exponent out of range" else pure $ (if esw == ord8 '+' then id else negate) - (T.foldl' (\n d -> n * 10 + digitToInt d) 0 significant) + (T.foldl' (\n d -> n * 10 + digitToInt d) 0 ds) let n = if fracDigits == 0 && power == 0 then fromInteger real else f real fraction (10 ^ fracDigits) power From 8a61f2f91e9b1d6c5fcd811e9e68c0af69f9a9d9 Mon Sep 17 00:00:00 2001 From: Laksh Agarwal <184763813+Lotion-3@users.noreply.github.com> Date: Fri, 27 Nov 2026 14:37:48 -0500 Subject: [PATCH 7/7] Tests: use exponents that fit in a 32-bit Int, and shorter long exponents 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. --- tests/Tests/Properties/Read.hs | 24 ++++++++++++------------ 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/tests/Tests/Properties/Read.hs b/tests/Tests/Properties/Read.hs index 9717ee73e..3f2ac77a8 100644 --- a/tests/Tests/Properties/Read.hs +++ b/tests/Tests/Properties/Read.hs @@ -137,22 +137,22 @@ testRead = localOption (mkTimeout 100000) $ testGroup "DDoS attacks" [ testProperty "t_double large positive exponent" $ - T.double (T.pack "1.1e1000000000") === Right (1 / 0, mempty), + T.double (T.pack "1.1e100000000") === Right (1 / 0, mempty), testProperty "t_double large negative exponent" $ - T.double (T.pack "1.1e-1000000000") === Right (0.0, mempty), + T.double (T.pack "1.1e-100000000") === Right (0.0, mempty), testProperty "tl_double large positive exponent" $ - TL.double (TL.pack "1.1e1000000000") === Right (1 / 0, mempty), + TL.double (TL.pack "1.1e100000000") === Right (1 / 0, mempty), testProperty "tl_double large negative exponent" $ - TL.double (TL.pack "1.1e-1000000000") === Right (0.0, mempty), + TL.double (TL.pack "1.1e-100000000") === Right (0.0, mempty), testProperty "t_rational large positive exponent" $ - T.rational (T.pack "1.1e1000000000") === Right (1 / 0 :: Double, mempty), + T.rational (T.pack "1.1e100000000") === Right (1 / 0 :: Double, mempty), testProperty "t_rational large negative exponent" $ - T.rational (T.pack "1.1e-1000000000") === Right (0.0 :: Double, mempty), + T.rational (T.pack "1.1e-100000000") === Right (0.0 :: Double, mempty), testProperty "tl_rational large positive exponent" $ - TL.rational (TL.pack "1.1e1000000000") === Right (1 / 0 :: Double, mempty), + TL.rational (TL.pack "1.1e100000000") === Right (1 / 0 :: Double, mempty), testProperty "tl_rational large negative exponent" $ - TL.rational (TL.pack "1.1e-1000000000") === Right (0.0 :: Double, mempty), + TL.rational (TL.pack "1.1e-100000000") === Right (0.0 :: Double, mempty), testProperty "t_double exponent at the bounds" $ map (T.double . T.pack) atExponentBounds === [Right (1 / 0, mempty), Right (0.0, mempty)], @@ -173,13 +173,13 @@ testRead = conjoin [TL.rational (TL.pack t) === (Left "exponent out of range" :: Either String (Double, TL.Text)) | t <- outOfExponentRange], testProperty "t_double long exponent" $ once $ - T.double (T.append "1e" (T.replicate 100000 "9")) === Left "exponent out of range", + T.double (T.append "1e" (T.replicate 10000 "9")) === Left "exponent out of range", testProperty "tl_double long exponent" $ once $ - TL.double (TL.append "1e-" (TL.replicate 100000 "9")) === Left "exponent out of range", + TL.double (TL.append "1e-" (TL.replicate 10000 "9")) === Left "exponent out of range", testProperty "t_double exponent with many leading zeros" $ once $ - T.double (T.concat ["1e-", T.replicate 100000 "0", "3"]) === Right (1.0e-3, mempty), + T.double (T.concat ["1e-", T.replicate 10000 "0", "3"]) === Right (1.0e-3, mempty), testProperty "tl_double exponent with many leading zeros" $ once $ - TL.double (TL.concat ["1e", TL.replicate 100000 "0", "3"]) === Right (1000.0, mempty) + TL.double (TL.concat ["1e", TL.replicate 10000 "0", "3"]) === Right (1000.0, mempty) ] ]