diff --git a/src/Data/Text/Internal/Read.hs b/src/Data/Text/Internal/Read.hs index 17d63154..b792673f 100644 --- a/src/Data/Text/Internal/Read.hs +++ b/src/Data/Text/Internal/Read.hs @@ -15,12 +15,18 @@ module Data.Text.Internal.Read , T(..) , digitToInt , hexDigitToInt + , maxExponentDigits , perhaps + , catchOrBind + , err + , snapshot + , reset ) 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) @@ -40,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 (>>=) #-} @@ -54,6 +60,28 @@ instance Monad (IParser t) where data T = T !Integer !Int +-- | The number of digits that always fit in an 'Int'. An exponent with more +-- significant digits is out of range. +maxExponentDigits :: Int +maxExponentDigits = (finiteBitSize (0 :: Int) - 1) * 3 `div` 10 + +-- | @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 Left _ -> Right (def,t) diff --git a/src/Data/Text/Lazy/Read.hs b/src/Data/Text/Lazy/Read.hs index 9757d278..60691af5 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,8 +185,11 @@ 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 - then fromRational ((real % 1 + frac % fracDenom) * (10 ^^ power)) + if - 1000 < power && power < 1000 + 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. @@ -213,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 @@ -228,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 '.') @@ -236,13 +246,20 @@ 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) + beforeE <- snapshot + power <- catchOrBind (charAscii e) (pure 0) $ \_ -> do + esw <- sign + ds <- asciiDigits + if T.null ds + then reset beforeE 0 + 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 ds) + 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 diff --git a/src/Data/Text/Read.hs b/src/Data/Text/Read.hs index c4c6dae4..7b20fee8 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,8 +180,11 @@ 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 - then fromRational ((real % 1 + frac % fracDenom) * (10 ^^ power)) + if - 1000 < power && power < 1000 + 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. @@ -208,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 @@ -220,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 '.') @@ -228,13 +238,20 @@ 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) + beforeE <- snapshot + power <- catchOrBind (charAscii e) (pure 0) $ \_ -> do + esw <- sign + ds <- asciiDigits + if T.null ds + then reset beforeE 0 + 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 ds) + 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 diff --git a/tests/Tests/Properties/Read.hs b/tests/Tests/Properties/Read.hs index 4b2ed34f..3f2ac77a 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) @@ -83,6 +84,34 @@ 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) + +-- | The number of exponent digits that always fit in an 'Int'. +exponentDigits :: Int +exponentDigits = (finiteBitSize (0 :: Int) - 1) * 3 `div` 10 + +-- | 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 zeros = replicate exponentDigits '0' + testRead :: TestTree testRead = testGroup "read" [ @@ -103,25 +132,54 @@ 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" $ - 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)], + 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 10000 "9")) === Left "exponent out of range", + testProperty "tl_double long exponent" $ once $ + 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 10000 "0", "3"]) === Right (1.0e-3, mempty), + testProperty "tl_double exponent with many leading zeros" $ once $ + TL.double (TL.concat ["1e", TL.replicate 10000 "0", "3"]) === Right (1000.0, mempty) ] ]