Repository navigation
Conversation
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
force-pushed
the
win-desc-alloc-fail
branch
from
December 3, 2026 09:31
e9e8cd5 to
a479fb2
Compare
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. Treat a failed device-info path conversion as a failed record. Check both CM property buffer allocations. Assisted-by: deepseek-harness:deepseek-flash
ebraminio
force-pushed
the
win-desc-alloc-fail
branch
3 times, most recently
from
December 3, 2026 21:55
becf69e to
0bcff89
Compare
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
force-pushed
the
win-desc-alloc-fail
branch
from
December 3, 2026 21:56
0bcff89 to
88d86b5
Compare
This branch has not been deployed
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.
Make descriptor reconstruction and critical
hid.callocation 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:
malloctocallocso partially allocated tables are NULL-initialized and can be safely freed by the shared cleanup path.nextpointer if allocation fails.hid_open_path()now returnsNULLif the input read buffer cannot be allocated.WCSDUPcalls are treated as missing optional strings; USB/BLE enrichment no longer callsWCSLENon NULL which previously was triggering an undefined behavior.hid_internal_wcsdupimplementation 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()andregister_winapi_error_to_buffer()free the old buffer before duplicating/formatting the new message. If that allocation fails,hid_error()returnsL"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()checksinterrupt_event, but notol.hEventorwrite_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.