Skip to content

windows: make allocation failures survivable - #866

Open
ebraminio wants to merge 5 commits into
libusb:masterfrom
ebraminio:win-desc-alloc-fail
Open

ebraminio wants to merge 5 commits into
libusb:masterfrom
ebraminio:win-desc-alloc-fail

Conversation

@ebraminio

@ebraminio ebraminio commented Dec 3, 2026 •

Copy link
Copy Markdown
Contributor

Make descriptor reconstruction and critical hid.c allocation paths fail safely.

When I was asked on no-CRT PR to make sure two allocations I added to the code are failing safely I had to add a cleanup path and that may gave the impression the whole function is safe against alloc fails which it wasn't so I felt I should extend the fail checks to the rest of the code sometime after that PR.

I tried to keep the diff as small as possible, some declaration moves were unavoidable to make cleanup reachable.

Key points to look while review:

  • Lookup-table allocations were changed from malloc to calloc so partially allocated tables are NULL-initialized and can be safely freed by the shared cleanup path.
  • List insertion restores the detached next pointer if allocation fails.
  • hid_open_path() now returns NULL if the input read buffer cannot be allocated.
  • Failed device-string WCSDUP calls are treated as missing optional strings; USB/BLE enrichment no longer calls WCSLEN on NULL which previously was triggering an undefined behavior.
  • Moved hid_internal_wcsdup implementation to hid.c and made it explicitly use malloc/memcpy similar to this in order to make it's memory management more explicit and more suitable for clients who may want provide their own custom allocator.

Remaining concerns:

  • Error reporting can become "Success" after OOM.
    register_string_error_to_buffer() and register_winapi_error_to_buffer() free the old buffer before duplicating/formatting the new message. If that allocation fails, hid_error() returns L"Success" even though the operation failed. A proper fix likely needs static-owned strings.
    In the other words: HIDAPI_WCSDUP(string_error); in https://github.com/libusb/hidapi/blob/c580ede/windows/hid.c#L327-L335 can fail and no error will be reported.

  • Not all device-creation handles are checked.
    new_hid_device() checks interrupt_event, but not ol.hEvent or write_ol.hEvent. These are not heap allocations, but they are the same class of failure and can leave a device object that cannot perform reads or writes.

  • No allocation-fault injection tests.
    The cleanup paths are audited and compile-tested, but not exercised under forced allocator failure. That remains the main validation gap.

Check every allocation in the descriptor reconstructor and propagate list
node allocation failures. Add a single null-safe cleanup path for partial
allocations, including the previously leaked coll_last_written_child.

Keep list insertion small by reusing rd_append_main_item_node and restoring
the detached next pointer if allocation fails. Keep coll_parsed_flag,
coll_last_written_child and the last lookup arrays local, freeing them on
each failure and normal exit. Adapted to padwave's HIDAPI allocation
wrappers and heap-based lookup tables.

Assisted-by: deepseek-harness:deepseek-flash
@ebraminio
ebraminio force-pushed the win-desc-alloc-fail branch from e9e8cd5 to a479fb2 Compare December 3, 2026 09:31
MIN macro was actually raising unused macro warning so I was considering
removal of it but thought can used it on the just added code.

Also turn clang-cl in the comment to clang since it isn't a matter of
compiler driver.
Windows back never used it, weird that it's defined.
@ebraminio ebraminio changed the title windows: handle descriptor reconstruction allocation failures windows: make allocation failures survivable Dec 3, 2026
Fail hid_open_path when the input read buffer cannot be allocated instead
of returning a device that later crashes in hid_read_timeout.

Treat USB/BLE device strings as optional when WCSDUP fails, so the string
lookup fallbacks do not call WCSLEN on NULL. Treat a failed device-info
path conversion as a failed record. Check both CM property buffer
allocations.

Assisted-by: deepseek-harness:deepseek-flash
@ebraminio
ebraminio force-pushed the win-desc-alloc-fail branch 3 times, most recently from becf69e to 0bcff89 Compare December 3, 2026 21:55
Move hid_internal_wcsdup() into hid.c and implement it with HIDAPI_WCSLEN,
HIDAPI_MALLOC and HIDAPI_MEMCPY. wcsdup()/_wcsdup() allocate through the
CRT, so they bypass the library's allocator macros. That would be wrong if
a custom allocation hooks are added later.

Keeping the helper in hid.c makes the matching HIDAPI_FREE lifetime
explicit, gives CRT and no-CRT one implementation, and removes the Cygwin
specific wcsdup workaround. The semantics are unchanged: allocate room for
the string and its terminator, copy both, and return NULL on allocation
failure.

Assisted-by: deepseek-harness:deepseek-flash
@ebraminio
ebraminio force-pushed the win-desc-alloc-fail branch from 0bcff89 to 88d86b5 Compare December 3, 2026 21:56

This branch has not been deployed

No deployments
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.

1 participant