libusb: make hid.c compile cleanly as C++#811
Merged
Merged
Conversation
libusb/hid.c relied on three C-only patterns that produce errors when the file is built with a C++ compiler (as reported in #671): - void* from malloc/calloc/pthread-style params implicitly assigned to typed pointers (6 sites); - int libusb return codes implicitly stored in `enum libusb_error` struct fields and passed to a function taking `enum libusb_error` (13 sites); - `#define _GNU_SOURCE` without an #ifndef guard, which produces a "redefined" warning when the macro is also supplied via -D. Fix: - Guard _GNU_SOURCE with #ifndef. - Change `hidapi_error_ctx`'s error_code / last_error_code_cache fields and `register_libusb_error()`'s error parameter from `enum libusb_error` to plain int. The struct is internal, the values stored are already a mix of libusb_error codes and a `1` sentinel used by register_string_error(), and int removes the 13 call-site conversions in one go. - Add explicit casts on the 6 malloc/calloc/void* sites, matching the convention already used in windows/hid.c and mac/hid.c. - Drop a now-redundant `(enum libusb_error)` cast. After this, `g++ -xc++ -Wall -Wextra -c libusb/hid.c` and the equivalent gcc invocation both build cleanly, and the full CMake build of the libusb backend (including hidtest_libusb) links unchanged. Closes: #671 Assisted-by: Claude:claude-opus-4.7
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.
Problem
libusb/hid.cdoesn't compile cleanly as C++, as reported in #671. Reproduced on current master withg++ -xc++ -Wall -Wextra -c libusb/hid.c:Three C-only patterns are involved:
void*to typed pointer conversions frommalloc/calloc/ pthread-stylevoid *param(6 sites).inttoenum libusb_errorconversions when storing libusb return codes (typedint) into thehidapi_error_ctxfields or passing them toregister_libusb_error()(13 sites).#define _GNU_SOURCEwithout an#ifndefguard — emits a"_GNU_SOURCE" redefinedwarning when the macro is also supplied via-Don the command line.Change
Minimal fix, scoped to
libusb/hid.c:#define _GNU_SOURCEin#ifndef _GNU_SOURCE.hidapi_error_ctxstruct'serror_code/last_error_code_cacheand theregister_libusb_error()errorparameter fromenum libusb_errorto plainint. The struct is internal (not exported), the stored values are already a mix of libusb error codes and a1sentinel fromregister_string_error(), and usingintremoves 13 call-site conversions in a single structural change.malloc/calloc/void*sites. This matches the convention already inwindows/hid.candmac/hid.c.(enum libusb_error)cast atlibusb_get_device_list's error path.Net diff: 17 insertions, 10 deletions in one file.
Verification
On WSL Ubuntu (gcc 11.4, libusb-1.0):
g++ -xc++ -Wall -Wextra -c libusb/hid.cgcc -Wall -Wextra -c libusb/hid.clibhidapi-libusb.so,hidtest_libusb)Closes #671.
Drafted with Claude Code.