DEFCTYPE alias for struct skips type translations

Bug #1924764 reported by Jan Igger
6
This bug affects 1 person
Affects Status Importance Assigned to Milestone
CFFI
Triaged
Medium
Unassigned

Bug Description

This is more a question than a bug probably, but I don't know where else to ask, so why does this work as follows?

```
(cffi:defctype uint8 :unsigned-char)

(cffi:defcstruct (anon-struct-39 :size 16)
  (data (:array uint8 16) :offset 0))

(cffi:defctype joystick-guid (:struct anon-struct-39))

(cffi:convert-from-foreign
 (foreign-alloc 'joystick-guid)
 '(:struct anon-struct-39))
;; => (DATA #(0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0))

(cffi:convert-from-foreign
 (foreign-alloc 'joystick-guid)
 'guid)
;; => #.(SB-SYS:INT-SAP #X7FC1E8001950)
```

Why can't I just use 'guid in convert-from-foreign?

Revision history for this message
Jan Igger (le-perv) wrote :

Sorry, here's a cleaned-up version:

(cffi:defctype uint8 :unsigned-char)

(cffi:defcstruct (anon-struct :size 16)
  (data (:array uint8 16) :offset 0))

(cffi:defctype guid (:struct anon-struct))

(cffi:convert-from-foreign
 (foreign-alloc 'guid)
 '(:struct anon-struct))
;; => (DATA #(0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0))

(cffi:convert-from-foreign
 (foreign-alloc 'guid)
 'guid)
;; => #.(SB-SYS:INT-SAP #X7FC1E8001950)

Revision history for this message
Luís Oliveira (luismbo) wrote :

Someone tried to fix this before but we had to revert it because it caused other problems: https://github.com/cffi/cffi/pull/152

There are some inconsistencies in the struct-as-values code is what I recall. Definitely a bug.

Changed in cffi:
status: New → Triaged
importance: Undecided → Medium
Revision history for this message
Jan Igger (le-perv) wrote :

Hm, alright, thanks for the fast response!

Also, in SDL, with the definitions as in the first post held in mind, there's a function:

SDL_JoystickGUID SDL_JoystickGetGUID(SDL_Joystick *joystick)
{
    if (!SDL_PrivateJoystickValid(joystick)) {
        SDL_JoystickGUID emptyGUID;
        SDL_zero(emptyGUID);
        return emptyGUID;
    }
    return joystick->guid;
}

where SDL_zero is:

#define SDL_zero(x) SDL_memset(&(x), 0, sizeof((x)))

and with the code:

(cffi:defcfun ("SDL_JoystickGetGUID" joystick-get-guid)
    joystick-guid
  (joystick (:pointer joystick)))

The plist that

(joystick-get-guid (cffi:null-pointer))

returns, for some reason, consistenly contains non-zero values:

(DATA #(0 0 0 0 0 0 0 0 195 28 0 0 0 0 0 0))

What's interesting, memset'ing a manually allocated joystick-guid and doing convert-from-foreign on it yields the correct result with all zeroes.

Should I file a seperate issue for this?

Revision history for this message
Luís Oliveira (luismbo) wrote :

Yes, that seems like a separate issue. If you could reduce the test case to something that doesn't depend on SDL, that'd be nice. BTW, the cffi-devel mailing list is a good place (if out of fashion) to have preliminary discussions like this.

Revision history for this message
Jan Igger (le-perv) wrote :

OK, will do!

Concerning the automatic conversion of structs to plists on function return, is this a part of stable API at this point, or can this still change in the future? If you want, I can ask this on the mailing list instead.

Revision history for this message
Luís Oliveira (luismbo) wrote :
summary: - conversion from struct
+ DEFCTYPE alias for struct skips type translations
Revision history for this message
Luís Oliveira (luismbo) wrote :

Jan, https://github.com/cffi/cffi/pull/181 may fix this. Can you take it for a spin?

Revision history for this message
Jan Igger (le-perv) wrote :

Works as expected now! Thank you!

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.