]> sigrok.org Git - libserialport.git/blobdiff - windows.c
windows: Don't try to include <unistd.h>.
[libserialport.git] / windows.c
index a9d09279fdffa57228dae907955b6d7cf4be83e1..a951378b4791782c628c3c850a4d2f6b1bbe02de 100644 (file)
--- a/windows.c
+++ b/windows.c
@@ -31,8 +31,12 @@ static void enumerate_hub(struct sp_port *port, const char *hub_name,
 static char *wc_to_utf8(PWCHAR wc_buffer, ULONG size)
 {
        ULONG wc_length = size / sizeof(WCHAR);
-       WCHAR wc_str[wc_length + 1];
-       char *utf8_str;
+       WCHAR *wc_str = NULL;
+       char *utf8_str = NULL;
+
+       /* Allocate aligned wide char buffer */
+       if (!(wc_str = malloc(size + sizeof(WCHAR))))
+               goto wc_to_utf8_end;
 
        /* Zero-terminate the wide char string. */
        memcpy(wc_str, wc_buffer, size);
@@ -41,19 +45,24 @@ static char *wc_to_utf8(PWCHAR wc_buffer, ULONG size)
        /* Compute the size of the UTF-8 converted string. */
        if (!(size = 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;
+               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)) {
                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;
 }
 
@@ -491,8 +500,12 @@ SP_PRIV enum sp_return list_ports(struct sp_port ***list)
        DEBUG("Opening registry key");
        if ((result = RegOpenKeyEx(HKEY_LOCAL_MACHINE, _T("HARDWARE\\DEVICEMAP\\SERIALCOMM"),
                        0, KEY_QUERY_VALUE, &key)) != ERROR_SUCCESS) {
-               SetLastError(result);
-               SET_FAIL(ret, "RegOpenKeyEx() failed");
+               /* 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");