Skip to content

Protect Against Potential Memory L eak and IWYU Cleanups#813

Open
JHartzer wants to merge 2 commits into
libusb:masterfrom
JHartzer:static-analysis
Open

Protect Against Potential Memory L eak and IWYU Cleanups#813
JHartzer wants to merge 2 commits into
libusb:masterfrom
JHartzer:static-analysis

Conversation

@JHartzer
Copy link
Copy Markdown

@JHartzer JHartzer commented May 21, 2026

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

  1. Linux Memory Leak: There is a potential memory leak in linux/hid.c parse_uevent_info where multiple matching HID_UNIQ or HID_NAME keys in a uevent file could overwrite previously allocated buffers without freeing them first.
  2. Transitive Include Fragility: Several source files (libusb/hid.c and mac/hid.c) relied on transitive inclusions for standard types like uint8_t, standard functions like calloc, and error definitions like errno. If parent headers are refactored, these sources will break.
  3. Unused Headers: Unused headers (such as <sys/utsname.h> and <linux/version.h>) were included in multiple places, unnecessarily increasing compile times.

Proposed Changes

  • Linux Memory Leak Fix
    • Updated the Linux backend linux/hid.c to 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.
  • IWYU (Include-What-You-Use) Header Cleanups
    • Added missing headers (like <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.
    • Removed unused headers (like <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:

cmake -B build -S . -DHIDAPI_WITH_LIBUSB=OFF -DHIDAPI_BUILD_HIDTEST=ON
cmake --build build
  • Result: All targets compiled with zero warnings and zero errors.

2. Static Analysis

A verification pass using clang-tidy was conducted on the Linux backend:

clang-tidy -p build-tidy linux/hid.c

  • Result: The Potential memory leak warning has been successfully resolved.

3. Functional Verification

The compiled test utility hidtest_hidraw was run successfully. It executed the hid_enumerate() discovery loop using our modified parse_uevent_info implementation, successfully extracting and displaying all connected USB device attributes

@mcuee mcuee added hidraw Related to Linux/hidraw backend libusb Related to libusb backend labels May 21, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

hidraw Related to Linux/hidraw backend libusb Related to libusb backend

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants