]> sigrok.org Git - libserialport.git/blobdiff - windows.c
windows: wc_to_utf8: use some clearer variable names.
[libserialport.git] / windows.c
index ee15a11964a900e6224bb1ec93ac2e59dade22bf..357feed1e80009f5e19e8cbd750ee135051178ae 100644 (file)
--- a/windows.c
+++ b/windows.c
 static void enumerate_hub(struct sp_port *port, const char *hub_name,
                           const char *parent_path, DEVINST dev_inst);
 
-static char *wc_to_utf8(PWCHAR wc_buffer, ULONG size)
+static char *wc_to_utf8(PWCHAR wc_buffer, ULONG wc_bytes)
 {
-       ULONG wc_length = size / sizeof(WCHAR);
-       WCHAR wc_str[wc_length + 1];
-       char *utf8_str;
+       ULONG wc_length = wc_bytes / sizeof(WCHAR);
+       ULONG utf8_bytes;
+       WCHAR *wc_str = NULL;
+       char *utf8_str = NULL;
+
+       /* Allocate aligned wide char buffer */
+       if (!(wc_str = malloc((wc_length + 1) * sizeof(WCHAR))))
+               goto wc_to_utf8_end;
 
        /* Zero-terminate the wide char string. */
-       memcpy(wc_str, wc_buffer, size);
+       memcpy(wc_str, wc_buffer, wc_bytes);
        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,
+       if (!(utf8_bytes = WideCharToMultiByte(CP_ACP, WC_NO_BEST_FIT_CHARS, wc_str, -1,
                                         NULL, 0, NULL, NULL)))
-               return NULL;
+               goto wc_to_utf8_end;
 
        /* Allocate UTF-8 output buffer. */
-       if (!(utf8_str = malloc(size)))
-               return NULL;
+       if (!(utf8_str = malloc(utf8_bytes)))
+               goto wc_to_utf8_end;
 
        /* Actually converted to UTF-8. */
        if (!WideCharToMultiByte(CP_ACP, WC_NO_BEST_FIT_CHARS, wc_str, -1,
-                                utf8_str, size, NULL, NULL)) {
+                                utf8_str, utf8_bytes, NULL, NULL)) {
                free(utf8_str);
-               return NULL;
+               utf8_str = NULL;
+               goto wc_to_utf8_end;
        }
 
+wc_to_utf8_end:
+       if (wc_str)
+               free(wc_str);
+
        return utf8_str;
 }
 
@@ -483,19 +493,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;
        }