[Ecls-list] FFI and null C strings

Goffioul Michael goffioul at imec.be
Wed Nov 24 07:19:00 UTC 2004

> Goffioul Michael wrote:
> >I found an annoying problem while fixing FFI issues. This is related
> >to null C strings. One example is for example:
> >
> >(ffi:def-function ("getenv" c-getenv)
> >  ((name :cstring))
> >  :returning cstring)
> >
> >If the variable doesn't exist, "getenv" returns NULL. However, the
> >generated C code convert the returned value to a LISP string using
> >make_string_copy, which doesn't support NULL as input argument.
> >Would it be OK to change make_string_copy such that it returns NIL
> >when the input is NULL?
> >
> Does this issue pop very often? I do not remember many 
> functions which 
> return a null pointer as a string.

Well, it can happen for any function declared returning "char*" and
potentially returning NULL. It is common to return NULL in a such a
function to signaled an error or a non-existing value.
The problem with the current situation is that if you C function
returns NULL, ECL simply crashes...

> However, if this is so 
> often that it 
> might be worth using, then I would vote for a new function 
> ecl_cstring_to_string_or_nil() The name is more clear and it does not 
> pervert an existing function which eventually should be replaced by a 
> ecl_string_to_string() that fails for NULL pointers.

Fine for me. But then, you should also adapt cmpffi.lsp.


More information about the ecl-devel mailing list