@@ -77,12 +77,16 @@ hGetEntropy (UseRdRand h) = \n ->
7777#endif
7878
7979fdReadBS :: Fd -> Int -> IO B.ByteString
80−fdReadBS fd n = do
81− allocaBytes n $ \buf -> do
82− rc <- fdReadBuf fd buf (fromIntegral n)
80+fdReadBS fd n =
81+ allocaBytes n $ \buf -> go buf (fromIntegral n)
82+ where
83+ go buf 0 = B.packCStringLen (castPtr buf, fromIntegral n)
84+ go buf cnt | cnt < n = do
85+ rc <- fdReadBuf fd (plusPtr buf (n - cnt)) (fromIntegral cnt)
8386 case rc of
8487 0 -> ioError (ioeSetErrorString (mkIOError eofErrorType "fdRead" Nothing Nothing) "EOF")
85− n' -> B.packCStringLen (castPtr buf, fromIntegral n')
88+ n' -> go buf (cnt - fromIntegral n')
89+ go _ _ = error "Impossible! The count of bytes left to read is greater than the request or less than zero!"
8690
8791#ifdef HAVE_RDRAND
8892foreign import ccall unsafe "cpu_has_rdrand"