Opened 11 years ago

Closed 11 years ago

#869 closed bug (fixed)

Foreign.C.String.withCStringLen doesn't terminate C string by NUL

Reported by: guest Owned by:
Priority: normal Milestone:
Component: libraries/base Version: 6.4.2
Keywords: Cc:
Operating System: Linux Architecture: x86
Type of failure: None/Unknown Test Case:
Blocked By: Blocking:
Related Tickets: Differential Rev(s):
Wiki Page:


Although the document of withCStringLen says "Marshal a Haskell string into a NUL terminated C string using temporary storage", it seems that C string is not terminated by NUL.

$ uname -a
Linux li9-154 2.4.29-linode39-1um #1 Wed Jan 19 12:22:14 EST 2005 i686 GNU/Linux
$ ghc -V
The Glorious Glasgow Haskell Compilation System, version 6.4.2

Change History (2)

comment:1 Changed 11 years ago by duncan

From my reading of the FFI spec CStringLen is not required to be nul terminated. So I think the code is correct and the documentation is wrong.

type CString = Ptr CChar
    A C string is a reference to an array of C characters
    terminated by NUL.

type CStringLen = (Ptr CChar, Int)
    In addition to NUL-terminated strings, the module CString also
    supports strings with explicit length information in bytes.

newCString :: String -> IO CString
newCStringLen :: String -> IO CStringLen
  Allocate a memory area for a Haskell string and marshal the
  string into its C representation. There are two variants of
  the routine, one for each supported string representation.

withCString :: String -> (CString -> IO a) -> IO a
withCStringLen :: String -> (CStringLen -> IO a) -> IO a
  These two routines operate as newCString and newCStringLen,
  respectively, but handle memory allocation and deallocation
  like MarshalAlloc.alloca (Section 5.8).

comment:2 Changed 11 years ago by ross

Resolution: fixed
Status: newclosed

That was my cut-and-paste in the docs -- now fixed.

Note: See TracTickets for help on using tickets.