]> sigrok.org Git - libserialport.git/blobdiff - windows.c
windows: Use a fixed worst-case WRITEFILE_MAX_SIZE.
[libserialport.git] / windows.c
index 644a701e46ecc80e4a7a40a8edea39522c32a0a5..53c1b02333c5550a19d593d18fe3e1c4f296bafe 100644 (file)
--- a/windows.c
+++ b/windows.c
@@ -30,12 +30,13 @@ static void enumerate_hub(struct sp_port *port, const char *hub_name,
 
 static char *wc_to_utf8(PWCHAR wc_buffer, ULONG size)
 {
-       WCHAR wc_str[(size / sizeof(WCHAR)) + 1];
+       ULONG wc_length = size / sizeof(WCHAR);
+       WCHAR wc_str[wc_length + 1];
        char *utf8_str;
 
        /* Zero-terminate the wide char string. */
        memcpy(wc_str, wc_buffer, size);
-       wc_str[sizeof(wc_str) - 1] = 0;
+       wc_str[wc_length] = 0;
 
        /* Compute the size of the UTF-8 converted string. */
        if (!(size = WideCharToMultiByte(CP_ACP, WC_NO_BEST_FIT_CHARS, wc_str, -1,
@@ -81,7 +82,7 @@ static char *get_root_hub_name(HANDLE host_controller)
        }
 
        /* Convert the root hub name string to UTF-8. */
-       root_hub_name_utf8 = wc_to_utf8(root_hub_name_wc->RootHubName, size);
+       root_hub_name_utf8 = wc_to_utf8(root_hub_name_wc->RootHubName, size - offsetof(USB_ROOT_HUB_NAME, RootHubName));
        free(root_hub_name_wc);
        return root_hub_name_utf8;
 }
@@ -116,7 +117,7 @@ static char *get_external_hub_name(HANDLE hub, ULONG connection_index)
        }
 
        /* Convert the external hub name string to UTF-8. */
-       ext_hub_name_utf8 = wc_to_utf8(ext_hub_name_wc->NodeName, size);
+       ext_hub_name_utf8 = wc_to_utf8(ext_hub_name_wc->NodeName, size - offsetof(USB_NODE_CONNECTION_NAME, NodeName));
        free(ext_hub_name_wc);
        return ext_hub_name_utf8;
 }
@@ -145,7 +146,7 @@ static char *get_string_descriptor(HANDLE hub_device, ULONG connection_index,
            || desc->bLength % 2)
                return NULL;
 
-       return wc_to_utf8(desc->bString, desc->bLength);
+       return wc_to_utf8(desc->bString, desc->bLength - offsetof(USB_STRING_DESCRIPTOR, bString));
 }
 
 static void enumerate_hub_ports(struct sp_port *port, HANDLE hub_device,
@@ -219,7 +220,7 @@ static void enumerate_hub_ports(struct sp_port *port, HANDLE hub_device,
                        port->usb_pid = connection_info_ex->DeviceDescriptor.idProduct;
 
                        if (connection_info_ex->DeviceDescriptor.iManufacturer)
-                               port->usb_manufacturer = get_string_descriptor(hub_device,index,
+                               port->usb_manufacturer = get_string_descriptor(hub_device, index,
                                           connection_info_ex->DeviceDescriptor.iManufacturer);
                        if (connection_info_ex->DeviceDescriptor.iProduct)
                                port->usb_product = get_string_descriptor(hub_device, index,
@@ -380,7 +381,7 @@ SP_PRIV enum sp_return get_port_details(struct sp_port *port)
                        continue;
                }
                RegCloseKey(device_key);
-               value[sizeof(value)-1] = 0;
+               value[sizeof(value) - 1] = 0;
                if (strcmp(value, port->name))
                        continue;
 
@@ -482,19 +483,26 @@ SP_PRIV enum sp_return list_ports(struct sp_port ***list)
        DWORD max_value_len, max_data_size, max_data_len;
        DWORD value_len, data_size, data_len;
        DWORD type, index = 0;
+       LSTATUS result;
        char *name;
        int name_len;
        int ret = SP_OK;
 
        DEBUG("Opening registry key");
-       if (RegOpenKeyEx(HKEY_LOCAL_MACHINE, _T("HARDWARE\\DEVICEMAP\\SERIALCOMM"),
-                       0, KEY_QUERY_VALUE, &key) != ERROR_SUCCESS) {
-               SET_FAIL(ret, "RegOpenKeyEx() failed");
+       if ((result = RegOpenKeyEx(HKEY_LOCAL_MACHINE, _T("HARDWARE\\DEVICEMAP\\SERIALCOMM"),
+                       0, KEY_QUERY_VALUE, &key)) != ERROR_SUCCESS) {
+               /* It's possible for this key to not exist if there are no serial ports
+                * at all. In that case we're done. Return a failure for any other error. */
+               if (result != ERROR_FILE_NOT_FOUND) {
+                       SetLastError(result);
+                       SET_FAIL(ret, "RegOpenKeyEx() failed");
+               }
                goto out_done;
        }
        DEBUG("Querying registry key value and data sizes");
-       if (RegQueryInfoKey(key, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
-                               &max_value_len, &max_data_size, NULL, NULL) != ERROR_SUCCESS) {
+       if ((result = RegQueryInfoKey(key, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
+                               &max_value_len, &max_data_size, NULL, NULL)) != ERROR_SUCCESS) {
+               SetLastError(result);
                SET_FAIL(ret, "RegQueryInfoKey() failed");
                goto out_close;
        }