[project @ 2002-01-31 13:42:20 by simonmar]
authorsimonmar <unknown>
Thu, 31 Jan 2002 13:42:20 +0000 (13:42 +0000)
committersimonmar <unknown>
Thu, 31 Jan 2002 13:42:20 +0000 (13:42 +0000)
Fix a classic bug: copying a Haskell string with one of the C string
functions (in this case strncpy()) is wrong when the string contains
'\0' characters.

The symptom in this case is that Happy parsers created with -ag don't
work in GHCi, because the state tables are encoded as strings
containing lots of '\0' elements.

ghc/compiler/ghci/ByteCodeGen.lhs

index bc93d39..411f1ad 100644 (file)
@@ -1202,7 +1202,7 @@ pushAtom False d p (AnnLit lit)
                             in  ioToBc (mallocBytes (n+1)) `thenBc` \ (Ptr a#) ->
                                 recordMallocBc (A# a#)     `thenBc_`
                                 ioToBc (
-                                   do strncpy (Ptr a#) ba (fromIntegral n)
+                                   do memcpy (Ptr a#) ba (fromIntegral n)
                                       writeCharOffAddr (A# a#) n '\0'
                                       return (A# a#)
                                    )
@@ -1230,7 +1230,7 @@ pushAtom tagged d p other
    = pprPanic "ByteCodeGen.pushAtom" 
               (pprCoreExpr (deAnnotate (undefined, other)))
 
-foreign import "strncpy" strncpy :: Ptr a -> ByteArray# -> CInt -> IO ()
+foreign import "memcpy" memcpy :: Ptr a -> ByteArray# -> CInt -> IO ()
 
 
 -- Given a bunch of alts code and their discrs, do the donkey work