Bad conversion of an array contained in a C struct

Bug #1924874 reported by Jan Igger
6
This bug affects 1 person
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(&emptyGUID, 0, sizeof(emptyGUID));
  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-foreign-library guid-lib
  (:unix "./libguid.so"))

(cffi:load-foreign-library 'guid-lib)

(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)

To post a comment you must log in.
This report contains Public information  
Everyone can see this information.

Other bug subscribers

Remote bug watches

Bug watches keep track of this bug in other bug trackers.