From a479fb21d61813aec165eb207a6cfc306633863b Mon Sep 17 00:00:00 2001 From: Ebrahim Date: Thu, 3 Dec 2026 01:07:03 +0330 Subject: [PATCH 1/5] windows: handle descriptor reconstruction allocation failures 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 --- windows/hidapi_descriptor_reconstruct.c | 154 ++++++++++++++++++++---- 1 file changed, 129 insertions(+), 25 deletions(-) diff --git a/windows/hidapi_descriptor_reconstruct.c b/windows/hidapi_descriptor_reconstruct.c index 0e8d87469..84462b4d4 100644 --- a/windows/hidapi_descriptor_reconstruct.c +++ b/windows/hidapi_descriptor_reconstruct.c @@ -143,6 +143,8 @@ static struct rd_main_item_node * rd_append_main_item_node(int first_bit, int la } new_list_node = (struct rd_main_item_node *)HIDAPI_MALLOC(sizeof(*new_list_node)); // Create new list entry + if (new_list_node == NULL) + return NULL; new_list_node->FirstBit = first_bit; new_list_node->LastBit = last_bit; new_list_node->TypeOfNode = type_of_node; @@ -161,6 +163,10 @@ static struct rd_main_item_node * rd_insert_main_item_node(int first_bit, int l struct rd_main_item_node *next_item = (*list)->next; (*list)->next = NULL; rd_append_main_item_node(first_bit, last_bit, type_of_node, caps_index, collection_index, main_item_type, report_id, list); + if ((*list)->next == NULL) { + (*list)->next = next_item; + return NULL; + } (*list)->next->next = next_item; return (*list)->next; } @@ -183,10 +189,11 @@ static struct rd_main_item_node * rd_search_main_item_list_for_bit_position(int int hid_winapi_descriptor_reconstruct_pp_data(void *preparsed_data, unsigned char *buf, size_t buf_size) { hidp_preparsed_data *pp_data = (hidp_preparsed_data *) preparsed_data; + int result = -1; // Check if MagicKey is correct, to ensure that pp_data points to an valid preparse data structure if (HIDAPI_MEMCMP(pp_data->MagicKey, "HidP KDR", 8) != 0) { - return -1; + return result; } struct rd_buffer rpt_desc = { @@ -195,6 +202,15 @@ int hid_winapi_descriptor_reconstruct_pp_data(void *preparsed_data, unsigned cha .byte_idx = 0 }; + rd_bit_range ****coll_bit_range = NULL; + int *coll_levels = NULL; + int *coll_number_of_direct_childs = NULL; + USHORT **coll_child_order = NULL; + struct rd_main_item_node **coll_begin_lookup = NULL; + struct rd_main_item_node **coll_end_lookup = NULL; + struct rd_main_item_node *main_item_list = NULL; // List root + + { // Set pointer to the first node of link_collection_nodes phid_pp_link_collection_node link_collection_nodes = (phid_pp_link_collection_node)(((unsigned char*)&pp_data->caps[0]) + pp_data->FirstByteOfLinkCollectionArray); @@ -204,14 +220,21 @@ int hid_winapi_descriptor_reconstruct_pp_data(void *preparsed_data, unsigned cha // **************************************************************************************************************************** // Allocate memory and initialize lookup table - rd_bit_range ****coll_bit_range; - coll_bit_range = (rd_bit_range ****)HIDAPI_MALLOC(pp_data->NumberLinkCollectionNodes * sizeof(*coll_bit_range)); + coll_bit_range = (rd_bit_range ****)HIDAPI_CALLOC(pp_data->NumberLinkCollectionNodes, sizeof(*coll_bit_range)); + if (coll_bit_range == NULL) + goto cleanup; for (USHORT collection_node_idx = 0; collection_node_idx < pp_data->NumberLinkCollectionNodes; collection_node_idx++) { - coll_bit_range[collection_node_idx] = (rd_bit_range ***)HIDAPI_MALLOC(256 * sizeof(*coll_bit_range[0])); // 256 possible report IDs (incl. 0x00) + coll_bit_range[collection_node_idx] = (rd_bit_range ***)HIDAPI_CALLOC(256, sizeof(*coll_bit_range[0])); // 256 possible report IDs (incl. 0x00) + if (coll_bit_range[collection_node_idx] == NULL) + goto cleanup; for (int reportid_idx = 0; reportid_idx < 256; reportid_idx++) { - coll_bit_range[collection_node_idx][reportid_idx] = (rd_bit_range **)HIDAPI_MALLOC(NUM_OF_HIDP_REPORT_TYPES * sizeof(*coll_bit_range[0][0])); + coll_bit_range[collection_node_idx][reportid_idx] = (rd_bit_range **)HIDAPI_CALLOC(NUM_OF_HIDP_REPORT_TYPES, sizeof(*coll_bit_range[0][0])); + if (coll_bit_range[collection_node_idx][reportid_idx] == NULL) + goto cleanup; for (int rt_idx = 0; rt_idx < NUM_OF_HIDP_REPORT_TYPES; rt_idx++) { coll_bit_range[collection_node_idx][reportid_idx][rt_idx] = (rd_bit_range *)HIDAPI_MALLOC(sizeof(rd_bit_range)); + if (coll_bit_range[collection_node_idx][reportid_idx][rt_idx] == NULL) + goto cleanup; coll_bit_range[collection_node_idx][reportid_idx][rt_idx]->FirstBit = -1; coll_bit_range[collection_node_idx][reportid_idx][rt_idx]->LastBit = -1; } @@ -243,8 +266,10 @@ int hid_winapi_descriptor_reconstruct_pp_data(void *preparsed_data, unsigned cha // coll_number_of_direct_childs[COLLECTION_INDEX] // ************************************************************************* int max_coll_level = 0; - int *coll_levels = (int *)HIDAPI_MALLOC(pp_data->NumberLinkCollectionNodes * sizeof(coll_levels[0])); - int *coll_number_of_direct_childs = (int *)HIDAPI_MALLOC(pp_data->NumberLinkCollectionNodes * sizeof(coll_number_of_direct_childs[0])); + coll_levels = (int *)HIDAPI_MALLOC(pp_data->NumberLinkCollectionNodes * sizeof(coll_levels[0])); + coll_number_of_direct_childs = (int *)HIDAPI_MALLOC(pp_data->NumberLinkCollectionNodes * sizeof(coll_number_of_direct_childs[0])); + if (coll_levels == NULL || coll_number_of_direct_childs == NULL) + goto cleanup; HIDAPI_MEMSET(coll_levels, -1, pp_data->NumberLinkCollectionNodes * sizeof(coll_levels[0])); HIDAPI_MEMSET(coll_number_of_direct_childs, 0, @@ -309,9 +334,14 @@ int hid_winapi_descriptor_reconstruct_pp_data(void *preparsed_data, unsigned cha // Determine child collection order of the whole hierarchy, based on previously determined bit ranges // and store it this index coll_child_order[COLLECTION_INDEX][DIRECT_CHILD_INDEX] // ************************************************************************************************** - USHORT **coll_child_order = (USHORT **)HIDAPI_MALLOC(pp_data->NumberLinkCollectionNodes * sizeof(*coll_child_order)); + coll_child_order = (USHORT **)HIDAPI_CALLOC(pp_data->NumberLinkCollectionNodes, sizeof(*coll_child_order)); + if (coll_child_order == NULL) { + goto cleanup; + } { BOOLEAN *coll_parsed_flag = (BOOLEAN *)HIDAPI_MALLOC(pp_data->NumberLinkCollectionNodes * sizeof(coll_parsed_flag[0])); + if (coll_parsed_flag == NULL) + goto cleanup; for (USHORT collection_node_idx = 0; collection_node_idx < pp_data->NumberLinkCollectionNodes; collection_node_idx++) { coll_parsed_flag[collection_node_idx] = FALSE; } @@ -322,6 +352,10 @@ int hid_winapi_descriptor_reconstruct_pp_data(void *preparsed_data, unsigned cha (coll_parsed_flag[link_collection_nodes[collection_node_idx].FirstChild] == FALSE)) { coll_parsed_flag[link_collection_nodes[collection_node_idx].FirstChild] = TRUE; coll_child_order[collection_node_idx] = (USHORT *)HIDAPI_MALLOC((coll_number_of_direct_childs[collection_node_idx]) * sizeof(*coll_child_order[0])); + if (coll_child_order[collection_node_idx] == NULL) { + HIDAPI_FREE(coll_parsed_flag); + goto cleanup; + } { // Create list of child collection indices @@ -378,12 +412,16 @@ int hid_winapi_descriptor_reconstruct_pp_data(void *preparsed_data, unsigned cha // *************************************************************************************** // Create sorted main_item_list containing all the Collection and CollectionEnd main items // *************************************************************************************** - struct rd_main_item_node *main_item_list = NULL; // List root // Lookup table to find the Collection items in the list by index - struct rd_main_item_node **coll_begin_lookup = (struct rd_main_item_node **)HIDAPI_MALLOC(pp_data->NumberLinkCollectionNodes * sizeof(*coll_begin_lookup)); - struct rd_main_item_node **coll_end_lookup = (struct rd_main_item_node **)HIDAPI_MALLOC(pp_data->NumberLinkCollectionNodes * sizeof(*coll_end_lookup)); + coll_begin_lookup = (struct rd_main_item_node **)HIDAPI_CALLOC(pp_data->NumberLinkCollectionNodes, sizeof(*coll_begin_lookup)); + coll_end_lookup = (struct rd_main_item_node **)HIDAPI_CALLOC(pp_data->NumberLinkCollectionNodes, sizeof(*coll_end_lookup)); + if (coll_begin_lookup == NULL || coll_end_lookup == NULL) + goto cleanup; { int *coll_last_written_child = (int *)HIDAPI_MALLOC(pp_data->NumberLinkCollectionNodes * sizeof(coll_last_written_child[0])); + if (coll_last_written_child == NULL) { + goto cleanup; + } HIDAPI_MEMSET(coll_last_written_child, -1, pp_data->NumberLinkCollectionNodes * sizeof(coll_last_written_child[0])); @@ -392,6 +430,10 @@ int hid_winapi_descriptor_reconstruct_pp_data(void *preparsed_data, unsigned cha struct rd_main_item_node *firstDelimiterNode = NULL; struct rd_main_item_node *delimiterCloseNode = NULL; coll_begin_lookup[0] = rd_append_main_item_node(0, 0, rd_item_node_collection, 0, collection_node_idx, rd_collection, 0, &main_item_list); + if (coll_begin_lookup[0] == NULL) { + HIDAPI_FREE(coll_last_written_child); + goto cleanup; + } while (actual_coll_level >= 0) { if ((coll_number_of_direct_childs[collection_node_idx] != 0) && (coll_last_written_child[collection_node_idx] == -1)) { @@ -408,12 +450,24 @@ int hid_winapi_descriptor_reconstruct_pp_data(void *preparsed_data, unsigned cha // Aliased Collection (First node in link_collection_nodes -> Last entry in report descriptor output) firstDelimiterNode = main_item_list; coll_begin_lookup[collection_node_idx] = rd_append_main_item_node(0, 0, rd_item_node_collection, 0, collection_node_idx, rd_delimiter_usage, 0, &main_item_list); + if (coll_begin_lookup[collection_node_idx] == NULL) { + HIDAPI_FREE(coll_last_written_child); + goto cleanup; + } coll_begin_lookup[collection_node_idx] = rd_append_main_item_node(0, 0, rd_item_node_collection, 0, collection_node_idx, rd_delimiter_close, 0, &main_item_list); + if (coll_begin_lookup[collection_node_idx] == NULL) { + HIDAPI_FREE(coll_last_written_child); + goto cleanup; + } delimiterCloseNode = main_item_list; } else { // Normal not aliased collection coll_begin_lookup[collection_node_idx] = rd_append_main_item_node(0, 0, rd_item_node_collection, 0, collection_node_idx, rd_collection, 0, &main_item_list); + if (coll_begin_lookup[collection_node_idx] == NULL) { + HIDAPI_FREE(coll_last_written_child); + goto cleanup; + } actual_coll_level++; } @@ -434,27 +488,55 @@ int hid_winapi_descriptor_reconstruct_pp_data(void *preparsed_data, unsigned cha // Aliased Collection (First node in link_collection_nodes -> Last entry in report descriptor output) firstDelimiterNode = main_item_list; coll_begin_lookup[collection_node_idx] = rd_append_main_item_node(0, 0, rd_item_node_collection, 0, collection_node_idx, rd_delimiter_usage, 0, &main_item_list); + if (coll_begin_lookup[collection_node_idx] == NULL) { + HIDAPI_FREE(coll_last_written_child); + goto cleanup; + } coll_begin_lookup[collection_node_idx] = rd_append_main_item_node(0, 0, rd_item_node_collection, 0, collection_node_idx, rd_delimiter_close, 0, &main_item_list); + if (coll_begin_lookup[collection_node_idx] == NULL) { + HIDAPI_FREE(coll_last_written_child); + goto cleanup; + } delimiterCloseNode = main_item_list; } else if (link_collection_nodes[collection_node_idx].IsAlias && (firstDelimiterNode != NULL)) { coll_begin_lookup[collection_node_idx] = rd_insert_main_item_node(0, 0, rd_item_node_collection, 0, collection_node_idx, rd_delimiter_usage, 0, &firstDelimiterNode); + if (coll_begin_lookup[collection_node_idx] == NULL) { + HIDAPI_FREE(coll_last_written_child); + goto cleanup; + } } else if (!link_collection_nodes[collection_node_idx].IsAlias && (firstDelimiterNode != NULL)) { coll_begin_lookup[collection_node_idx] = rd_insert_main_item_node(0, 0, rd_item_node_collection, 0, collection_node_idx, rd_delimiter_usage, 0, &firstDelimiterNode); + if (coll_begin_lookup[collection_node_idx] == NULL) { + HIDAPI_FREE(coll_last_written_child); + goto cleanup; + } coll_begin_lookup[collection_node_idx] = rd_insert_main_item_node(0, 0, rd_item_node_collection, 0, collection_node_idx, rd_delimiter_open, 0, &firstDelimiterNode); + if (coll_begin_lookup[collection_node_idx] == NULL) { + HIDAPI_FREE(coll_last_written_child); + goto cleanup; + } firstDelimiterNode = NULL; main_item_list = delimiterCloseNode; delimiterCloseNode = NULL; // Last entry of alias has .IsAlias == FALSE } if (!link_collection_nodes[collection_node_idx].IsAlias) { coll_begin_lookup[collection_node_idx] = rd_append_main_item_node(0, 0, rd_item_node_collection, 0, collection_node_idx, rd_collection, 0, &main_item_list); + if (coll_begin_lookup[collection_node_idx] == NULL) { + HIDAPI_FREE(coll_last_written_child); + goto cleanup; + } actual_coll_level++; } } else { actual_coll_level--; coll_end_lookup[collection_node_idx] = rd_append_main_item_node(0, 0, rd_item_node_collection, 0, collection_node_idx, rd_collection_end, 0, &main_item_list); + if (coll_end_lookup[collection_node_idx] == NULL) { + HIDAPI_FREE(coll_last_written_child); + goto cleanup; + } collection_node_idx = link_collection_nodes[collection_node_idx].Parent; } } @@ -497,27 +579,32 @@ int hid_winapi_descriptor_reconstruct_pp_data(void *preparsed_data, unsigned cha if (pp_data->caps[caps_idx].IsAlias && (firstDelimiterNode == NULL)) { // Aliased Usage (First node in pp_data->caps -> Last entry in report descriptor output) firstDelimiterNode = list_node; - rd_insert_main_item_node(first_bit, last_bit, rd_item_node_cap, caps_idx, pp_data->caps[caps_idx].LinkCollection, rd_delimiter_usage, pp_data->caps[caps_idx].ReportID, &list_node); - rd_insert_main_item_node(first_bit, last_bit, rd_item_node_cap, caps_idx, pp_data->caps[caps_idx].LinkCollection, rd_delimiter_close, pp_data->caps[caps_idx].ReportID, &list_node); + if (rd_insert_main_item_node(first_bit, last_bit, rd_item_node_cap, caps_idx, pp_data->caps[caps_idx].LinkCollection, rd_delimiter_usage, pp_data->caps[caps_idx].ReportID, &list_node) == NULL) + goto cleanup; + if (rd_insert_main_item_node(first_bit, last_bit, rd_item_node_cap, caps_idx, pp_data->caps[caps_idx].LinkCollection, rd_delimiter_close, pp_data->caps[caps_idx].ReportID, &list_node) == NULL) + goto cleanup; delimiterCloseNode = list_node; } else if (pp_data->caps[caps_idx].IsAlias && (firstDelimiterNode != NULL)) { - rd_insert_main_item_node(first_bit, last_bit, rd_item_node_cap, caps_idx, pp_data->caps[caps_idx].LinkCollection, rd_delimiter_usage, pp_data->caps[caps_idx].ReportID, &list_node); + if (rd_insert_main_item_node(first_bit, last_bit, rd_item_node_cap, caps_idx, pp_data->caps[caps_idx].LinkCollection, rd_delimiter_usage, pp_data->caps[caps_idx].ReportID, &list_node) == NULL) + goto cleanup; } else if (!pp_data->caps[caps_idx].IsAlias && (firstDelimiterNode != NULL)) { // Aliased Collection (Last node in pp_data->caps -> First entry in report descriptor output) - rd_insert_main_item_node(first_bit, last_bit, rd_item_node_cap, caps_idx, pp_data->caps[caps_idx].LinkCollection, rd_delimiter_usage, pp_data->caps[caps_idx].ReportID, &list_node); - rd_insert_main_item_node(first_bit, last_bit, rd_item_node_cap, caps_idx, pp_data->caps[caps_idx].LinkCollection, rd_delimiter_open, pp_data->caps[caps_idx].ReportID, &list_node); + if (rd_insert_main_item_node(first_bit, last_bit, rd_item_node_cap, caps_idx, pp_data->caps[caps_idx].LinkCollection, rd_delimiter_usage, pp_data->caps[caps_idx].ReportID, &list_node) == NULL) + goto cleanup; + if (rd_insert_main_item_node(first_bit, last_bit, rd_item_node_cap, caps_idx, pp_data->caps[caps_idx].LinkCollection, rd_delimiter_open, pp_data->caps[caps_idx].ReportID, &list_node) == NULL) + goto cleanup; firstDelimiterNode = NULL; list_node = delimiterCloseNode; delimiterCloseNode = NULL; // Last entry of alias has .IsAlias == FALSE } if (!pp_data->caps[caps_idx].IsAlias) { - rd_insert_main_item_node(first_bit, last_bit, rd_item_node_cap, caps_idx, pp_data->caps[caps_idx].LinkCollection, (rd_main_items) rt_idx, pp_data->caps[caps_idx].ReportID, &list_node); + if (rd_insert_main_item_node(first_bit, last_bit, rd_item_node_cap, caps_idx, pp_data->caps[caps_idx].LinkCollection, (rd_main_items) rt_idx, pp_data->caps[caps_idx].ReportID, &list_node) == NULL) + goto cleanup; } } } - int result = -1; // *********************************************************** // Add const main items for padding to main_item_list @@ -556,7 +643,11 @@ int hid_winapi_descriptor_reconstruct_pp_data(void *preparsed_data, unsigned cha (last_report_item_lookup[list->MainItemType][list->ReportID]->FirstBit != list->FirstBit) // Happens in case of IsMultipleItemsForArray for multiple dedicated usages for a multi-button array ) { struct rd_main_item_node *list_node = rd_search_main_item_list_for_bit_position(last_bit_position[list->MainItemType][list->ReportID], list->MainItemType, list->ReportID, &last_report_item_lookup[list->MainItemType][list->ReportID]); - rd_insert_main_item_node(last_bit_position[list->MainItemType][list->ReportID] + 1, list->FirstBit - 1, rd_item_node_padding, -1, 0, list->MainItemType, list->ReportID, &list_node); + if (rd_insert_main_item_node(last_bit_position[list->MainItemType][list->ReportID] + 1, list->FirstBit - 1, rd_item_node_padding, -1, 0, list->MainItemType, list->ReportID, &list_node) == NULL) { + HIDAPI_FREE(last_report_item_lookup); + HIDAPI_FREE(last_bit_position); + goto cleanup; + } } if (list->ReportID != 0) { devicehasReportIDs = TRUE; @@ -579,7 +670,11 @@ int hid_winapi_descriptor_reconstruct_pp_data(void *preparsed_data, unsigned cha int padding = 8 - ((last_bit_position[rt_idx][reportid_idx] + 1) % 8); if (padding < 8) { // Insert padding item after item referenced in last_report_item_lookup - rd_insert_main_item_node(last_bit_position[rt_idx][reportid_idx] + 1, last_bit_position[rt_idx][reportid_idx] + padding, rd_item_node_padding, -1, 0, (rd_main_items) rt_idx, (unsigned char) reportid_idx, &last_report_item_lookup[rt_idx][reportid_idx]); + if (rd_insert_main_item_node(last_bit_position[rt_idx][reportid_idx] + 1, last_bit_position[rt_idx][reportid_idx] + padding, rd_item_node_padding, -1, 0, (rd_main_items) rt_idx, (unsigned char) reportid_idx, &last_report_item_lookup[rt_idx][reportid_idx]) == NULL) { + HIDAPI_FREE(last_report_item_lookup); + HIDAPI_FREE(last_bit_position); + goto cleanup; + } if (last_report_item_lookup[rt_idx][reportid_idx] == node_before_top_level_coll_end) { // If this padding item is at the end of the top level collection, update node_before_top_level_coll_end node_before_top_level_coll_end = last_report_item_lookup[rt_idx][reportid_idx]->next; @@ -601,7 +696,11 @@ int hid_winapi_descriptor_reconstruct_pp_data(void *preparsed_data, unsigned cha if (padding > 0) { // Insert padding item after item referenced in last_report_item_lookup - rd_insert_main_item_node(last_bit_position[rt_idx][reportId] + 1, last_bit_position[rt_idx][reportId] + padding, rd_item_node_padding, -1, 0, (rd_main_items)rt_idx, reportId, &node_before_top_level_coll_end); + if (rd_insert_main_item_node(last_bit_position[rt_idx][reportId] + 1, last_bit_position[rt_idx][reportId] + padding, rd_item_node_padding, -1, 0, (rd_main_items)rt_idx, reportId, &node_before_top_level_coll_end) == NULL) { + HIDAPI_FREE(last_report_item_lookup); + HIDAPI_FREE(last_bit_position); + goto cleanup; + } } } } @@ -614,7 +713,6 @@ int hid_winapi_descriptor_reconstruct_pp_data(void *preparsed_data, unsigned cha // *********************************** // Encode the report descriptor output // *********************************** - { UCHAR last_report_id = 0; USAGE last_usage_page = 0; LONG last_physical_min = 0;// If both, Physical Minimum and Physical Maximum are 0, the logical limits should be taken as physical limits according USB HID spec 1.11 chapter 6.2.2.7 @@ -1024,18 +1122,24 @@ int hid_winapi_descriptor_reconstruct_pp_data(void *preparsed_data, unsigned cha } // Free multidimensionable array: coll_bit_range[COLLECTION_INDEX][REPORT_ID][INPUT/OUTPUT/FEATURE] - // Free multidimensionable array: coll_child_order[COLLECTION_INDEX][DIRECT_CHILD_INDEX] - for (USHORT collection_node_idx = 0; collection_node_idx < pp_data->NumberLinkCollectionNodes; collection_node_idx++) { + for (USHORT collection_node_idx = 0; coll_bit_range != NULL && collection_node_idx < pp_data->NumberLinkCollectionNodes; collection_node_idx++) { + if (coll_bit_range[collection_node_idx] == NULL) + continue; for (int reportid_idx = 0; reportid_idx < 256; reportid_idx++) { + if (coll_bit_range[collection_node_idx][reportid_idx] == NULL) + continue; for (int rt_idx = 0; rt_idx < NUM_OF_HIDP_REPORT_TYPES; rt_idx++) { HIDAPI_FREE(coll_bit_range[collection_node_idx][reportid_idx][rt_idx]); } HIDAPI_FREE(coll_bit_range[collection_node_idx][reportid_idx]); } HIDAPI_FREE(coll_bit_range[collection_node_idx]); - if (coll_number_of_direct_childs[collection_node_idx] != 0) HIDAPI_FREE(coll_child_order[collection_node_idx]); } HIDAPI_FREE(coll_bit_range); + + // Free multidimensionable array: coll_child_order[COLLECTION_INDEX][DIRECT_CHILD_INDEX] + for (USHORT collection_node_idx = 0; coll_child_order != NULL && collection_node_idx < pp_data->NumberLinkCollectionNodes; collection_node_idx++) + HIDAPI_FREE(coll_child_order[collection_node_idx]); HIDAPI_FREE(coll_child_order); // Free one dimensional arrays From 1455071b7b3091cdff9a2fe6a1930054854e5f4c Mon Sep 17 00:00:00 2001 From: Ebrahim Date: Thu, 3 Dec 2026 14:43:42 +0330 Subject: [PATCH 2/5] windows: actually use MIN macro 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/hid.c | 2 +- windows/hidapi_winapi_crt_compat.h | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/windows/hid.c b/windows/hid.c index 7bdcb9c3a..3521a56ab 100644 --- a/windows/hid.c +++ b/windows/hid.c @@ -1505,7 +1505,7 @@ static int hid_internal_get_device_string(hid_device *dev, wchar_t *string, size } size_t srclen = HIDAPI_WCSLEN(src); - size_t len = srclen < maxlen - 1 ? srclen : maxlen - 1; + size_t len = MIN(srclen, maxlen - 1); HIDAPI_MEMCPY(string, src, len * sizeof(wchar_t)); string[len] = L'\0'; diff --git a/windows/hidapi_winapi_crt_compat.h b/windows/hidapi_winapi_crt_compat.h index 8db6375a7..610f99576 100644 --- a/windows/hidapi_winapi_crt_compat.h +++ b/windows/hidapi_winapi_crt_compat.h @@ -108,7 +108,7 @@ static inline void *hid_internal_memcpy(void *dest, const void *src, size_t byte static inline void *hid_internal_memset(void *dest, int value, size_t bytes) { -/* clang-cl lowers __stosb to external memset, which no-CRT builds cannot use. +/* clang lowers __stosb to external memset, which no-CRT builds cannot use. * See also: https://github.com/llvm/llvm-project/issues/33522 */ #if defined(HIDAPI_WINAPI_HAVE_X86_MEM_INTRIN) && !defined(__clang__) __stosb((unsigned char *)dest, (unsigned char)value, bytes); From b8a652968748e8287914d914ab22c67f6000e58a Mon Sep 17 00:00:00 2001 From: Ebrahim Date: Thu, 3 Dec 2026 15:02:44 +0330 Subject: [PATCH 3/5] windows: Remove unused HIDAPI_STRDUP Windows back never used it, weird that it's defined. --- windows/hidapi_winapi_crt_compat.h | 17 +++-------------- 1 file changed, 3 insertions(+), 14 deletions(-) diff --git a/windows/hidapi_winapi_crt_compat.h b/windows/hidapi_winapi_crt_compat.h index 610f99576..7829eeb90 100644 --- a/windows/hidapi_winapi_crt_compat.h +++ b/windows/hidapi_winapi_crt_compat.h @@ -50,7 +50,6 @@ static inline void hid_internal_towupper(wchar_t *string) #else #define HIDAPI_WCSDUP(s) _wcsdup((s)) #endif -#define HIDAPI_STRDUP(s) _strdup((s)) #else /* HIDAPI_NO_CRT */ @@ -135,24 +134,15 @@ static inline int hid_internal_memcmp(const void *lhs, const void *rhs, size_t b return 0; } -static inline void *hid_internal_dup(const void *src, size_t bytes) +static inline wchar_t *hid_internal_wcsdup(const wchar_t *src) { - void *dest = hid_internal_malloc(bytes); + size_t bytes = ((size_t)lstrlenW(src) + 1) * sizeof(wchar_t); + wchar_t *dest = (wchar_t *)hid_internal_malloc(bytes); if (dest) hid_internal_memcpy(dest, src, bytes); return dest; } -static inline char *hid_internal_strdup(const char *src) -{ - return (char *)hid_internal_dup(src, (size_t)lstrlenA(src) + 1); -} - -static inline wchar_t *hid_internal_wcsdup(const wchar_t *src) -{ - return (wchar_t *)hid_internal_dup(src, ((size_t)lstrlenW(src) + 1) * sizeof(wchar_t)); -} - static inline int hid_internal_wcscmp(const wchar_t *lhs, const wchar_t *rhs) { int result = CompareStringOrdinal(lhs, -1, rhs, -1, FALSE); @@ -193,7 +183,6 @@ static inline long hid_internal_wcstol_hex(const wchar_t *string, wchar_t **endp #define HIDAPI_WCSSTR(haystack, needle) StrStrW((haystack), (needle)) #define HIDAPI_WCSTOL_HEX(s, end) hid_internal_wcstol_hex((s), (end)) #define HIDAPI_WCSDUP(s) hid_internal_wcsdup((s)) -#define HIDAPI_STRDUP(s) hid_internal_strdup((s)) #endif /* HIDAPI_NO_CRT */ From d2cbc35da7821bed4a2ea22f033396f827ec623b Mon Sep 17 00:00:00 2001 From: Ebrahim Date: Thu, 3 Dec 2026 15:37:42 +0330 Subject: [PATCH 4/5] windows: handle critical hid.c allocation failures 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 --- windows/hid.c | 26 +++++++++++++++++++++----- 1 file changed, 21 insertions(+), 5 deletions(-) diff --git a/windows/hid.c b/windows/hid.c index 3521a56ab..edb847e66 100644 --- a/windows/hid.c +++ b/windows/hid.c @@ -424,6 +424,9 @@ static void* hid_internal_get_devnode_property(DEVINST dev_node, const DEVPROPKE return NULL; property_value = (PBYTE)HIDAPI_CALLOC(len, sizeof(BYTE)); + if (property_value == NULL) { + return NULL; + } cr = CM_Get_DevNode_PropertyW(dev_node, property_key, &property_type, property_value, &len, 0); if (cr != CR_SUCCESS) { HIDAPI_FREE(property_value); @@ -445,6 +448,9 @@ static void* hid_internal_get_device_interface_property(const wchar_t* interface return NULL; property_value = (PBYTE)HIDAPI_CALLOC(len, sizeof(BYTE)); + if (property_value == NULL) { + return NULL; + } cr = CM_Get_Device_Interface_PropertyW(interface_path, property_key, &property_type, property_value, &len, 0); if (cr != CR_SUCCESS) { HIDAPI_FREE(property_value); @@ -524,7 +530,7 @@ static void hid_internal_get_usb_info(struct hid_device_info* dev, DEVINST dev_n } /* Try to get USB device manufacturer string if not provided by HidD_GetManufacturerString. */ - if (HIDAPI_WCSLEN(dev->manufacturer_string) == 0) { + if (dev->manufacturer_string == NULL || HIDAPI_WCSLEN(dev->manufacturer_string) == 0) { wchar_t* manufacturer_string = (wchar_t *)hid_internal_get_devnode_property(dev_node, &DEVPKEY_Device_Manufacturer, DEVPROP_TYPE_STRING); if (manufacturer_string) { HIDAPI_FREE(dev->manufacturer_string); @@ -533,7 +539,7 @@ static void hid_internal_get_usb_info(struct hid_device_info* dev, DEVINST dev_n } /* Try to get USB device serial number if not provided by HidD_GetSerialNumberString. */ - if (HIDAPI_WCSLEN(dev->serial_number) == 0) { + if (dev->serial_number == NULL || HIDAPI_WCSLEN(dev->serial_number) == 0) { DEVINST usb_dev_node = dev_node; if (dev->interface_number != -1) { /* Get devnode parent to reach out composite parent USB device. @@ -582,7 +588,7 @@ static void hid_internal_get_usb_info(struct hid_device_info* dev, DEVINST dev_n */ static void hid_internal_get_ble_info(struct hid_device_info* dev, DEVINST dev_node) { - if (HIDAPI_WCSLEN(dev->manufacturer_string) == 0) { + if (dev->manufacturer_string == NULL || HIDAPI_WCSLEN(dev->manufacturer_string) == 0) { /* Manufacturer Name String (UUID: 0x2A29) */ wchar_t* manufacturer_string = (wchar_t *)hid_internal_get_devnode_property(dev_node, (const DEVPROPKEY*)&PKEY_DeviceInterface_Bluetooth_Manufacturer, DEVPROP_TYPE_STRING); if (manufacturer_string) { @@ -591,7 +597,7 @@ static void hid_internal_get_ble_info(struct hid_device_info* dev, DEVINST dev_n } } - if (HIDAPI_WCSLEN(dev->serial_number) == 0) { + if (dev->serial_number == NULL || HIDAPI_WCSLEN(dev->serial_number) == 0) { /* Serial Number String (UUID: 0x2A25) */ wchar_t* serial_number = (wchar_t *)hid_internal_get_devnode_property(dev_node, (const DEVPROPKEY*)&PKEY_DeviceInterface_Bluetooth_DeviceAddress, DEVPROP_TYPE_STRING); if (serial_number) { @@ -600,7 +606,7 @@ static void hid_internal_get_ble_info(struct hid_device_info* dev, DEVINST dev_n } } - if (HIDAPI_WCSLEN(dev->product_string) == 0) { + if (dev->product_string == NULL || HIDAPI_WCSLEN(dev->product_string) == 0) { /* Model Number String (UUID: 0x2A24) */ wchar_t* product_string = (wchar_t *)hid_internal_get_devnode_property(dev_node, (const DEVPROPKEY*)&PKEY_DeviceInterface_Bluetooth_ModelNumber, DEVPROP_TYPE_STRING); if (!product_string) { @@ -759,6 +765,10 @@ static struct hid_device_info *hid_internal_get_device_info(const wchar_t *path, /* Fill out the record */ dev->next = NULL; dev->path = hid_internal_UTF16toUTF8(path); + if (dev->path == NULL) { + HIDAPI_FREE(dev); + return NULL; + } dev->interface_number = -1; attrib.Size = sizeof(HIDD_ATTRIBUTES); @@ -1060,6 +1070,12 @@ HID_API_EXPORT hid_device * HID_API_CALL hid_open_path(const char *path) dev->input_report_length = caps.InputReportByteLength; dev->feature_report_length = caps.FeatureReportByteLength; dev->read_buf = (char*) HIDAPI_MALLOC(dev->input_report_length); + if (dev->read_buf == NULL) { + register_global_error(L"read buffer allocation error"); + free_hid_device(dev); + dev = NULL; + goto end_of_function; + } dev->device_info = hid_internal_get_device_info(interface_path, dev->device_handle); end_of_function: From 88d86b5261c5870df57ab4c7e7eee84582e48fa9 Mon Sep 17 00:00:00 2001 From: Ebrahim Date: Thu, 3 Dec 2026 23:27:10 +0330 Subject: [PATCH 5/5] windows: duplicate device strings with HIDAPI allocator 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 --- windows/hid.c | 19 ++++++++++++++----- windows/hidapi_winapi_crt_compat.h | 16 +--------------- 2 files changed, 15 insertions(+), 20 deletions(-) diff --git a/windows/hid.c b/windows/hid.c index edb847e66..e39df409d 100644 --- a/windows/hid.c +++ b/windows/hid.c @@ -315,6 +315,15 @@ static void register_winapi_error_to_buffer(wchar_t **error_buffer, const WCHAR } } +static wchar_t *hid_internal_wcsdup(const wchar_t *src) +{ + size_t bytes = ((size_t)HIDAPI_WCSLEN(src) + 1) * sizeof(wchar_t); + wchar_t *dest = (wchar_t *)HIDAPI_MALLOC(bytes); + if (dest) + HIDAPI_MEMCPY(dest, src, bytes); + return dest; +} + #if defined(__GNUC__) # pragma GCC diagnostic push # pragma GCC diagnostic ignored "-Warray-bounds" @@ -330,7 +339,7 @@ static void register_string_error_to_buffer(wchar_t **error_buffer, const WCHAR *error_buffer = NULL; if (string_error) { - *error_buffer = HIDAPI_WCSDUP(string_error); + *error_buffer = hid_internal_wcsdup(string_error); } } @@ -567,7 +576,7 @@ static void hid_internal_get_usb_info(struct hid_device_info* dev, DEVINST dev_n if (*ptr == L'\\') { HIDAPI_FREE(dev->serial_number); - dev->serial_number = HIDAPI_WCSDUP(ptr + 1); + dev->serial_number = hid_internal_wcsdup(ptr + 1); break; } } @@ -802,17 +811,17 @@ static struct hid_device_info *hid_internal_get_device_info(const wchar_t *path, /* Serial Number */ string[0] = L'\0'; HidD_GetSerialNumberString(handle, string, size); - dev->serial_number = HIDAPI_WCSDUP(string); + dev->serial_number = hid_internal_wcsdup(string); /* Manufacturer String */ string[0] = L'\0'; HidD_GetManufacturerString(handle, string, size); - dev->manufacturer_string = HIDAPI_WCSDUP(string); + dev->manufacturer_string = hid_internal_wcsdup(string); /* Product String */ string[0] = L'\0'; HidD_GetProductString(handle, string, size); - dev->product_string = HIDAPI_WCSDUP(string); + dev->product_string = hid_internal_wcsdup(string); /* now, the portion that depends on string descriptors */ switch (dev->bus_type) { diff --git a/windows/hidapi_winapi_crt_compat.h b/windows/hidapi_winapi_crt_compat.h index 7829eeb90..4f59c433e 100644 --- a/windows/hidapi_winapi_crt_compat.h +++ b/windows/hidapi_winapi_crt_compat.h @@ -45,11 +45,7 @@ static inline void hid_internal_towupper(wchar_t *string) #define HIDAPI_WCSCMP(lhs, rhs) wcscmp((lhs), (rhs)) #define HIDAPI_WCSSTR(haystack, needle) wcsstr((haystack), (needle)) #define HIDAPI_WCSTOL_HEX(s, end) wcstol((s), (end), 16) -#ifdef __CYGWIN__ -#define HIDAPI_WCSDUP(s) wcsdup((s)) -#else -#define HIDAPI_WCSDUP(s) _wcsdup((s)) -#endif + #else /* HIDAPI_NO_CRT */ @@ -134,15 +130,6 @@ static inline int hid_internal_memcmp(const void *lhs, const void *rhs, size_t b return 0; } -static inline wchar_t *hid_internal_wcsdup(const wchar_t *src) -{ - size_t bytes = ((size_t)lstrlenW(src) + 1) * sizeof(wchar_t); - wchar_t *dest = (wchar_t *)hid_internal_malloc(bytes); - if (dest) - hid_internal_memcpy(dest, src, bytes); - return dest; -} - static inline int hid_internal_wcscmp(const wchar_t *lhs, const wchar_t *rhs) { int result = CompareStringOrdinal(lhs, -1, rhs, -1, FALSE); @@ -182,7 +169,6 @@ static inline long hid_internal_wcstol_hex(const wchar_t *string, wchar_t **endp #define HIDAPI_WCSCMP(lhs, rhs) hid_internal_wcscmp((lhs), (rhs)) #define HIDAPI_WCSSTR(haystack, needle) StrStrW((haystack), (needle)) #define HIDAPI_WCSTOL_HEX(s, end) hid_internal_wcstol_hex((s), (end)) -#define HIDAPI_WCSDUP(s) hid_internal_wcsdup((s)) #endif /* HIDAPI_NO_CRT */