[project @ 2001-10-31 12:26:17 by simonmar]
authorsimonmar <unknown>
Wed, 31 Oct 2001 12:26:17 +0000 (12:26 +0000)
committersimonmar <unknown>
Wed, 31 Oct 2001 12:26:17 +0000 (12:26 +0000)
Handle completely empty files without crashing in slurpFileExpandTabs.

ghc/compiler/utils/StringBuffer.lhs

index f49449e..1aedc7b 100644 (file)
@@ -198,9 +198,14 @@ slurpFileExpandTabs fname = do
          then ioError (userError "slurpFile: file too big")
           else do
            let sz_i = fromInteger sz
-               sz_i' = (sz_i * 12) `div` 10            -- add 20% for tabs
-           chunk <- allocMem sz_i'
-           trySlurp handle sz_i' chunk
+            if sz_i == 0
+                       -- empty file: just allocate a buffer containing '\0'
+               then do chunk <- allocMem 1
+                       writeCharOffAddr chunk 0 '\0'
+                       return (chunk, 0)
+               else do let sz_i' = (sz_i * 12) `div` 10 -- add 20% for tabs
+                       chunk <- allocMem sz_i'
+                       trySlurp handle sz_i' chunk
    )
 
 trySlurp :: Handle -> Int -> Addr -> IO (Addr, Int)