Protect Against Potential Memory L eak and IWYU Cleanups#813
Conversation
Youw
left a comment
There was a problem hiding this comment.
One comment, everything else looks good, thanks
|
Hey @Youw! I just pushed up a suggested fix for the windows-cmake build failures. It looks like |
|
Thanks. I'd like to keep these things separated. Let this PR be only about the code and not about the CI. Temporary failed MSVC build will not stop me from merging it (CI issue is not caused by the PR, so it is fine). Regarding MSVC build fix/improvement - I agree, your suggested fix is so much better than the hard-coded version that is now, but I'd rather make even one more step further, and use setup-msvc-dev Github Action. |
Resolve the builds.yml conflict in favour of master: the hard-coded vcvars path is now fixed on master by the setup-msvc-dev action plus a dedicated windows-mingw job (libusb#817), so this branch's vswhere-based CI change is dropped as redundant. The static-analysis code changes (the linux/hid.c memory-leak fix and the IWYU header cleanups) are unaffected. Assisted-by: claude-code:claude-opus-4-8
Description
This PR resolves a potential memory leak identified via static analysis in the Linux backend. It also implements several IWYU header fixes.
Problems Identified
linux/hid.cparse_uevent_infowhere multiple matchingHID_UNIQorHID_NAMEkeys in aueventfile could overwrite previously allocated buffers without freeing them first.libusb/hid.candmac/hid.c) relied on transitive inclusions for standard types likeuint8_t, standard functions likecalloc, and error definitions likeerrno. If parent headers are refactored, these sources will break.<sys/utsname.h>and<linux/version.h>) were included in multiple places, unnecessarily increasing compile times.Proposed Changes
linux/hid.cto safely free any previously allocated memory before saving a new device property. This stops memory from leaking if a device's info key (like name or serial) is parsed more than once.<stdint.h>,<stdlib.h>, and<errno.h>) across the test utility, macOS, and LibUSB backends so they don't crash or break when parent files are updated.<sys/utsname.h>and<linux/version.h>) that were cluttering the code and slowing down compile times.Verification & Testing
1. Automated Compilation
Clean out-of-source builds were configured and executed using CMake:
2. Static Analysis
A verification pass using clang-tidy was conducted on the Linux backend:
clang-tidy -p build-tidy linux/hid.c3. Functional Verification
The compiled test utility
hidtest_hidrawwas run successfully. It executed thehid_enumerate()discovery loop using our modifiedparse_uevent_infoimplementation, successfully extracting and displaying all connected USB device attributes