chicken-users
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [Chicken-users] FFI with callbacks for gnu readline


From: Zbigniew
Subject: Re: [Chicken-users] FFI with callbacks for gnu readline
Date: Wed, 18 Jan 2006 20:11:38 -0600

It looks like Thomas is right about the callback, although I can't get
your example to crash.  I'm running Mac OS X PPC, which sometimes
allows memory accesses that would cause segfaults elsewhere, so I have
another possibility if you still get crashes.  Before testing for the
scheme string length in completion_func, you do a strcmp against "". 
Here's the thing--scheme strings are never null terminated at all,
even when returned by C_c_string (which after all just returns a
pointer to the raw scheme data).  In fact, a NULL string will be 0
bytes long.  This strcmp will probably access the first byte of your
result string, which is an illegal memory access.  Instead, you should
test that the scheme string size is 0 and return NULL if so.

I think C_c_string should be documented to return a
non-null-terminated string.  This bit me hard once; I have since
created a C_c_string0 utility function which does a strncpy like you
did.

Try the attached patch if you need it.

On 1/18/06, Thomas Chust <address@hidden> wrote:
> On Wed, 18 Jan 2006, LeviPearson wrote:
>
> > The readline library expects to be able to call a generator function
> > with a string and an index and get back a newly-allocated string that it
> > can use and then free().  To accommodate this, I wrote a C function that
> > calls a define-external'd scheme function that returns a scheme-object
> > containing the string.  I found that returning a c-string would
> > sometimes give me some garbage characters as the strings were not always
> > null-terminated at the right spot.
>
> Hello,
>
> actually I doubt that the termination of strings with a '\0' character is
> broken. Maybe the problem is rather related to garbage collection
> somehow, if the string is used for a longer time by the readline
> library. But nevertheless your approach here looks sensible.
>
> > [...] This all seems to work well, but after a bit of use of the
> > tab-completion functionality, the program segfaults or otherwise dies on
> > a call to the readline interface.
>
> Well, this is no wonder, because your function gnu-readline is a
> foreign-lambda* and not a foreign-safe-lambda*. Callbacks into Scheme are
> only safe inside foreign-safe-lambda and friends, otherwise your stack may
> unexpectedly be sweeped clean by a minor garbage collection and your C
> code won't like that very much.
>
> So just replace the foreign-lambda* by foreign-safe-lambda* in your
> readline.scm for every function that may eventually cause a callback intro
> Scheme and everything should be fine.
>
> > [...]
>
> cu,
> Thomas
>
>
> _______________________________________________
> Chicken-users mailing list
> address@hidden
> http://lists.nongnu.org/mailman/listinfo/chicken-users
>

Attachment: crasher.diff
Description: Binary data


reply via email to

[Prev in Thread] Current Thread [Next in Thread]