Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 2 additions & 3 deletions src/Data/Text/IO.hs
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,7 @@ module Data.Text.IO
import Data.Text (Text)
import Prelude hiding (appendFile, getContents, getLine, interact,
putStr, putStrLn, readFile, writeFile)
import System.IO (Handle, IOMode(..), openFile, stdin, stdout,
withFile)
import System.IO (Handle, IOMode(..), stdin, stdout, withFile)
import qualified Control.Exception as E
import Control.Monad (liftM2, when)
import Data.IORef (readIORef)
Expand All @@ -73,7 +72,7 @@ import System.IO.Error (isEOFError)
-- using 'Data.Text.Encoding.decodeUtf8' '.' 'Data.ByteString.readFile'
-- is a much faster and safer alternative.
readFile :: FilePath -> IO Text
readFile name = openFile name ReadMode >>= hGetContents
readFile name = withFile name ReadMode hGetContents

-- | Write a string to a file. The file is truncated to zero length
-- before writing begins.
Expand Down
15 changes: 15 additions & 0 deletions tests/Tests/Regressions.hs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ module Tests.Regressions
import Control.Exception (ErrorCall, SomeException, handle, evaluate, displayException, try)
import Data.Char (isLetter, chr)
import GHC.Exts (Int(..), sizeofByteArray#)
import Control.Exception (IOException)
import System.IO
import System.IO.Temp (withSystemTempFile)
import Test.Tasty.HUnit (assertBool, assertEqual, assertFailure, (@?=))
Expand All @@ -36,6 +37,8 @@ import qualified Test.Tasty as F
import qualified Test.Tasty.HUnit as F
import Tests.Utils (withTempFile)
import System.IO.Error (isFullError)
import Control.Monad (when)
import Data.Either (isRight)

-- Reported by Michael Snoyman: UTF-8 encoding a large lazy bytestring
-- caused either a segfault or attempt to allocate a negative number
Expand Down Expand Up @@ -213,6 +216,17 @@ t648 = withTempFile $ \_ h -> do
line' <- T.hGetLine h
T.append line "\r" @?= line'

t714 :: IO ()
t714 = withTempFile $ \name h -> do
hSetBinaryMode h True
hPutChar h '\xFF'
hClose h
e :: Either IOException T.Text <- try (T.readFile name)
when (isRight e) (assertFailure "should fail")
-- readFile must close the handle even in the case of an exception,
-- in which case the next line should succeed.
openFile name WriteMode >>= hClose

tests :: F.TestTree
tests = F.testGroup "Regressions"
[ F.testCase "hGetContents_crash" hGetContents_crash
Expand All @@ -233,4 +247,5 @@ tests = F.testGroup "Regressions"
, F.testCase "t559" t559
, F.testCase "t633" t633
, F.testCase "t648" t648
, F.testCase "t714" t714
]
Loading