Tag Archives: cffi
When SBCL Is Buggy, and CFFI Is Undocumented
There are at least two good ways to create C strings (or alien strings) in Lisp. The most often used is CFFI‘s foreign-string-alloc and the other is SBCL‘s make-alien-string.
The SBCL routine make-alien-string is documented to return both the alien pointer and the length of the string. However, it doesn’t.
Today, I reported this bug so by the time you read this the following may actually work; but as of SBCL 1.3.9 it doesn’t.
(multiple-value-bind (buffer length) (make-alien-string "foo") (format t "buffer: ~a~%length: ~a~%" buffer length))
And this will print something like
buffer: #<SB-ALIEN-INTERNALS:ALIEN-VALUE :SAP #X00400190 :TYPE (* (SB-ALIEN:SIGNED 8))> length: NIL
On the other hand, the CFFI routine foreign-string-alloc is not documented (as of this writing) to return an extra length value, but actually does.
(multiple-value-bind (buffer length) (cffi:foreign-string-alloc "foo") (format t "buffer: ~a~%length: ~a~%" buffer length))
Which will print something like
buffer: #.(SB-SYS:INT-SAP #X00600050) length: 4
Note that the result is by default zero terminated, and hence the four bytes.
Hopefully the CFFI documentation will be updated just as quickly as SBCL is patched.
Have fun, and enjoy the Lisp world because it’s full of weird stuff.