Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use proper signed/unsigned designations in struct format strings #30

Closed
dhalbert opened this issue Jan 12, 2019 · 0 comments
Closed

Use proper signed/unsigned designations in struct format strings #30

dhalbert opened this issue Jan 12, 2019 · 0 comments

Comments

@dhalbert
Copy link
Collaborator

from @ATMakersBill (part of adafruit/circuitpython#1451)

As we were trying to track down a problem w/the gamepad HID interface (or more likely the Microsoft XAC's implementation) @dhalbert and I saw that the format strings for pack_into() were not taking into consideration whether the variable was signed. For example in gamepad.py you'll find:

    struct.pack_into('<HBBBB', self._report, 0,
                     self._buttons_state,
                     self._joy_x, self._joy_y,
                     self._joy_z, self._joy_r_z)

The "<HBBBB" string says that it's expecting an unsigned 16-bit value followed by four unsigned bytes. However, we are sending signed bytes (-127 through 127) as defined in our descriptor.

In this case, circruitpython doesn't care because it would encode them the same way. However, CPython would error out after checking this and in general this is dangerous, because with types > 8 bit, we very much do care about endianness.

@dhalbert asked that I open an issue about this. The fix on gamepad.py is pretty simple, just change the string to "<Hbbbb", however, we should probably do a sanity check on this.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant