Skip to content

windows: make allocation failures survivable - #866

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

ebraminio wants to merge 4 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.

This started with two newly added allocations in the descriptor reconstructor. Once those were checked, leaving the remaining allocations unchecked would have misleadingly suggested the function was fully protected, so I extended the checks to the rest of it. I kept the diff as small as practical; some declaration moves were unavoidable to make cleanup reachable.

Key points for review:

  • hid_winapi_descriptor_reconstruct_pp_data() now returns -1 on allocation failure and frees partial state through one null-safe cleanup path.
  • 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.
  • Both CM property-buffer allocations are checked.
  • Unused HIDAPI_STRDUP was removed; the no-CRT hid_internal_wcsdup() return type was corrected.
  • The string getter now uses the existing MIN macro.

Builds verified: MSVC normal C, MSVC no-CRT C, and MSVC no-CRT C++20.

I audited every HIDAPI_MALLOC, HIDAPI_CALLOC, and HIDAPI_WCSDUP in the two Windows translation units. There is no allocation-fault injection testing, so the cleanup paths still deserve a careful review.

Remaining allocation issues

These were intentionally left for maintainer input rather than folded into this PR.

  • 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. Preserving the old error helps only when one exists; a proper fix likely needs a static owned fallback or an error-status API.

  • Device-info path conversion failure leaves a partial record.
    dev->path = hid_internal_UTF16toUTF8(path); is unchecked. On failure, enumeration can return a device-info record with a NULL path, which cannot be opened. Needs a decision: drop the record or report it as partially valid.

  • 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.
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. Check both CM property buffer
allocations, and cast the no-CRT WCSDUP result for C++ builds.

Assisted-by: deepseek-harness:deepseek-flash
@ebraminio ebraminio changed the title windows: handle descriptor reconstruction allocation failures windows: make allocation failures survivable Dec 3, 2026

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