hidapi: fix function pointer type mismatch in backend table#15825
Merged
Conversation
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.
a75b5a9 to
19f6a14
Compare
Collaborator
|
Interesting. I'm not even sure how clang can detect this at runtime, but your change looks fine to me. Thanks! |
Contributor
|
Missing the code authorship confirmation? |
Contributor
Author
@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. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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:
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=allVerified:
SDL_Init(SDL_INIT_GAMEPAD)no longer traps with a PS5 DualSense connected.SDL_GetGamepadsreports the connected controller.