Skip to content

hidapi: fix function pointer type mismatch in backend table#15825

Merged
slouken merged 1 commit into
libsdl-org:mainfrom
ArnauNau:fix-hidapi-function-sanitizer
Jun 13, 2026
Merged

hidapi: fix function pointer type mismatch in backend table#15825
slouken merged 1 commit into
libsdl-org:mainfrom
ArnauNau:fix-hidapi-function-sanitizer

Conversation

@ArnauNau

Copy link
Copy Markdown
Contributor

Summary

Fixes a Clang UBSan function sanitizer TRAP in the HIDAPI backend table.

The HIDAPI backend table used generic void * function pointers, but the PLATFORM, DRIVER, and LIBUSB backends are compiled into this translation unit with macro-renamed device types. Calling those backend functions directly through the generic table leaves an incompatible indirect function call.

To fix this, I added small backend wrappers so the backend table calls functions with the expected type, and the cast to the backend-specific device type happens inside a direct call.

I first tested the approach by using a direct wrapper approach like this:

#ifdef HAVE_PLATFORM_BACKEND
static int PLATFORM_hid_set_nonblocking_backend(hid_device *device, int nonblock)
{
    return PLATFORM_hid_set_nonblocking((PLATFORM_hid_device *)device, nonblock);
}


static const struct hidapi_backend PLATFORM_Backend = {
    PLATFORM_hid_write_backend,
    PLATFORM_hid_read_timeout_backend,
    ...
};
#endif

but after writing the PLATFORM ones and seeing DRIVER and LIBUSB would need their too, I went the macro direction. To me it looks simpler / cleaner; but I can go back to the other way if requested.

Testing

Built SDL with (LLVM) Clang 22.1.7 using:

-fsanitize=undefined,function -fsanitize-trap=undefined,function -fno-sanitize-recover=all

Verified:

  • SDL_Init(SDL_INIT_GAMEPAD) no longer traps with a PS5 DualSense connected.
  • SDL_GetGamepads reports the connected controller.

The HIDAPI backend table used function pointers that didn't match the
macro-renamed backend function types, which trips Clang's function sanitizer.

Adding small wrappers so the backend table has the correct function types and
the cast to the backend-specific device type happens inside a direct call.
fixes libsdl-org#15821.
@ArnauNau ArnauNau force-pushed the fix-hidapi-function-sanitizer branch from a75b5a9 to 19f6a14 Compare June 13, 2026 10:40
@ArnauNau ArnauNau marked this pull request as ready for review June 13, 2026 10:40
@slouken

slouken commented Jun 13, 2026

Copy link
Copy Markdown
Collaborator

Interesting. I'm not even sure how clang can detect this at runtime, but your change looks fine to me.

Thanks!

@slouken slouken added this to the 3.4.12 milestone Jun 13, 2026
@slouken slouken merged commit 46d0ece into libsdl-org:main Jun 13, 2026
46 checks passed
@slime73

slime73 commented Jun 13, 2026

Copy link
Copy Markdown
Contributor

Missing the code authorship confirmation?

@ArnauNau

Copy link
Copy Markdown
Contributor Author
  • I confirm that I am the author of this code and release it to the SDL project under the Zlib license. This contribution does not contain code from other sources, including code generated by a Large Language Model ("AI").

@slime73 sorry about that, I didn't see any contribution guidelines and last time I made a PR I don't remember needing to do that.

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

Successfully merging this pull request may close these issues.

3 participants