on master help sign in
Properly fix the int overflow bug reported by Ian
9825285Bryan O'Sullivan
Data/Text.hs+2
@@ -807,6 +807,8 @@ replicate n t@(Text a o l)
807 | n <= 0 || l <= 0 = empty
807 | n <= 0 || l <= 0 = empty
808 | n == 1 = t
808 | n == 1 = t
809 | isSingleton t = replicateChar n (unsafeHead t)
809 | isSingleton t = replicateChar n (unsafeHead t)
810+ | len < n = error $ "Data.Text.replicate: invalid length " ++
811+ show n -- multiplication overflow
810 | otherwise = Text (A.run x) 0 len
812 | otherwise = Text (A.run x) 0 len
811 where
813 where
812 len = l * n
814 len = l * n
Data/Text/Array.hs+1−1
@@ -103,7 +103,7 @@ instance IArray (MArray s) where
103-- | Create an uninitialized mutable array.
103-- | Create an uninitialized mutable array.
104new :: forall s. Int -> ST s (MArray s)
104new :: forall s. Int -> ST s (MArray s)
105new n
105new n
106− | len < 0 = error $ "Data.Text.Array.unsafeNew: invalid length " ++ show n
106+ | len < n = error $ "Data.Text.Array.unsafeNew: invalid length " ++ show n
107 | otherwise = ST $ \s1# ->
107 | otherwise = ST $ \s1# ->
108 case newByteArray# len# s1# of
108 case newByteArray# len# s1# of
109 (# s2#, marr# #) -> (# s2#, MArray marr#
109 (# s2#, marr# #) -> (# s2#, MArray marr#
tests/Regressions.hs+13−1
@@ -1,4 +1,4 @@
1−{-# LANGUAGE ScopedTypeVariables #-}
1+{-# LANGUAGE OverloadedStrings, ScopedTypeVariables #-}
2
2
3-- Regression tests for specific bugs.
3-- Regression tests for specific bugs.
4
4
@@ -6,6 +6,7 @@ import Control.Exception (SomeException, handle)
6import System.IO
6import System.IO
7import qualified Data.ByteString as B
7import qualified Data.ByteString as B
8import qualified Data.ByteString.Lazy as LB
8import qualified Data.ByteString.Lazy as LB
9+import qualified Data.Text as T
9import qualified Data.Text.IO as T
10import qualified Data.Text.IO as T
10import qualified Data.Text.Lazy as LT
11import qualified Data.Text.Lazy as LT
11import qualified Data.Text.Lazy.Encoding as LE
12import qualified Data.Text.Lazy.Encoding as LE
@@ -30,10 +31,21 @@ hGetContents_crash = withTempFile $ \ path h -> do
30 handle (\(_::SomeException) -> return ()) $
31 handle (\(_::SomeException) -> return ()) $
31 T.hGetContents h' >> assertFailure "T.hGetContents should crash"
32 T.hGetContents h' >> assertFailure "T.hGetContents should crash"
32
33
34+-- Reported by Ian Lynagh: attempting to allocate a sufficiently large
35+-- string (via either Array.new or Text.replicate) could result in an
36+-- integer overflow.
37+replicate_crash = handle (\(_::SomeException) -> return ()) $
38+ T.replicate (2^power) "0123456789abcdef" `seq`
39+ assertFailure "T.replicate should crash"
40+ where
41+ power | maxBound == (2147483647::Int) = 28
42+ | otherwise = 60 :: Int
43+
33tests :: F.Test
44tests :: F.Test
34tests = F.testGroup "crashers" [
45tests = F.testGroup "crashers" [
35 F.testCase "hGetContents_crash" hGetContents_crash
46 F.testCase "hGetContents_crash" hGetContents_crash
36 , F.testCase "lazy_encode_crash" lazy_encode_crash
47 , F.testCase "lazy_encode_crash" lazy_encode_crash
48+ , F.testCase "replicate_crash" replicate_crash
37 ]
49 ]
38
50
39main = F.defaultMain [tests]
51main = F.defaultMain [tests]
text.cabal+1−1
@@ -1,5 +1,5 @@
1name: text
1name: text
2−version: 0.9.0.2
2+version: 0.9.0.1
3homepage: http://code.haskell.org/text
3homepage: http://code.haskell.org/text
4synopsis: An efficient packed Unicode text type.
4synopsis: An efficient packed Unicode text type.
5description:
5description: