Skip to content

winio: fixed bytestring reading interface.

Hi,

It seems that the bytestring reading interface is completely different from the String reading interface.

We seem to have forgotten to update the offset pointer for the bytestring interface. As such winio will hang indefinitely when reading bytestrings longer than a buffer size (32k).

import System.Environment
import qualified Data.ByteString.Lazy as BL
import Data.Word

fold_tailrec :: (a -> b -> a) -> a -> [b] -> a
fold_tailrec _ acc [] =
    acc
fold_tailrec foldFun acc (x : xs) =
    fold_tailrec foldFun (foldFun acc x) xs

fold_tailrec' :: (a -> b -> a) -> a -> [b] -> a
fold_tailrec' _ acc [] =
    acc
fold_tailrec' foldFun acc (x : xs) =
    let acc' = foldFun acc x
    in seq acc' (fold_tailrec' foldFun acc' xs)

main :: IO ()
main =
    do
        args <- getArgs
        let filename = head args
        byteString <- BL.readFile filename
        let wordsList = BL.unpack byteString
        -- wordsList is supposed to be lazy (bufferized)
        let bytesCount = fold_tailrec (\acc word -> acc + 1) 0 wordsList
        print ("Total bytes in " ++ filename ++ ": "
               ++ (show bytesCount))

stresses the I/O manager and bytestring interface and will hang before this change.

Ok for master and GHC 9?

Merge request reports