The length of the ByteString is Int . If Int is 32 bits, a 7 GB file will exceed the Int range and the buffer request will be the wrong size and can easily request a negative size.
readFile code converts file size to Int for buffer request
readFile :: FilePath -> IO ByteString readFile f = bracket (openBinaryFile f ReadMode) hClose (\h -> hFileSize h >>= hGet h . fromIntegral)
and if it is an overflow, the most likely outcomes are the “illegal byte size” error or segmentation error.
If at all possible, use lazy ByteString to process large files. In your case, you should pretty much make this possible, since with 32-bit Int s it is not possible to create a 7GB ByteString .
If you need a ByteString line to process and the line is not too long, you can go through the lazy ByteString to achieve this
import qualified Data.ByteString.Lazy.Char8 as LC import qualified Data.ByteString.Char8 as C main = do ... content <- LC.readFile path let llns = LC.lines content slns = map (C.concat . LC.toChunks) llns foobar slns
but if you can change your processing to handle lazy ByteString s, it will probably be better.
Daniel Fischer
source share