]> sigrok.org Git - libserialport.git/commitdiff
windows: Handle registry lookup failures correctly.
authorMartin Ling <redacted>
Mon, 20 Jan 2020 01:28:17 +0000 (01:28 +0000)
committerMartin Ling <redacted>
Thu, 23 Jan 2020 03:35:47 +0000 (03:35 +0000)
RegOpenKeyEx() and RegQueryInfoKey() return system error codes directly,
not by setting the thread-local errno equivalent that is returned by
GetLastError().

When returning SP_ERR_FAIL, our API specifies that sp_last_error_code()
may be called immediately afterwards to get the system error code. In
this case that would not work, as it would call GetLastError() and miss
the directly-returned result.

We therefore need to call SetLastError() with the error code before
returning with SP_ERR_FAIL.

windows.c

index ee15a11964a900e6224bb1ec93ac2e59dade22bf..a9d09279fdffa57228dae907955b6d7cf4be83e1 100644 (file)
--- a/windows.c
+++ b/windows.c
@@ -483,19 +483,22 @@ 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) {
+       if ((result = RegOpenKeyEx(HKEY_LOCAL_MACHINE, _T("HARDWARE\\DEVICEMAP\\SERIALCOMM"),
+                       0, KEY_QUERY_VALUE, &key)) != ERROR_SUCCESS) {
+               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;
        }