Bad conversion of an array contained in a C struct
Affects | Status | Importance | Assigned to | Milestone | |
---|---|---|---|---|---|
CFFI |
New
|
Undecided
|
Unassigned |
Bug Description
Reproduction steps:
1. Create
-------------- guid.h --------------
typedef unsigned char uint8_t;
typedef struct {
uint8_t data[16];
} GUID;
GUID getGUID();
----------- end of guid.h ----------
-------------- guid.c --------------
#include <string.h>
#include "guid.h"
GUID getGUID()
{
GUID emptyGUID;
memset(
return emptyGUID;
}
----------- end of guid.c ----------
2. compile
$ gcc -c -fPIC guid.c -o guid.o
$ gcc guid.o -shared -o libguid.so
3. on lisp side (I am using SBCL 2.1.1 on Arch Linux):
(cffi:define-
(:unix "./libguid.so"))
(cffi:load-
(cffi:defctype uint8 :unsigned-char)
(cffi:defcstruct (anon-struct-39 :size 16)
(data (:array uint8 16) :offset 0))
(cffi:defctype guid (:struct anon-struct-39))
(cffi:defcfun ("getGUID" get-guid)
guid)
Now,
(get-guid)
yields
=> (DATA #(0 0 0 0 0 0 0 0 132 36 194 2 0 0 0 0))
(except numbers and their places change)
instead of all zeros.
4. (optional) as a sanity check, add this to guid.c:
-------------- addition --------------
#include <stdio.h>
int main()
{
GUID guid = getGUID();
for (unsigned int i = 0; i < sizeof(guid); i++)
{
printf("%i", guid.data[i]);
}
}
----------- end of guid.c ----------
$ gcc guid.c -o guid
$ ./guid
0000000000000000%
(as expected)