Fix RegCloseKey exeption when double-closing hKeys
authorCharles Giessen <charles@lunarg.com>
Thu, 11 Jul 2024 17:13:18 +0000 (10:13 -0700)
committerCharles Giessen <46324611+charles-lunarg@users.noreply.github.com>
Thu, 11 Jul 2024 21:14:51 +0000 (16:14 -0500)
While the application still can run, RegCloseKey will throw an error when
key is double-closed. This was caused by the test framework not catching
when the double-close occurs.

loader/loader_windows.c
tests/framework/shim/windows_shim.cpp

index 43ecf66720190034dd6b3a97fbf7d0cae4253f88..74a9d7230509a128857b68baf853e92f58456875 100644 (file)
@@ -1215,41 +1215,37 @@ VkResult get_settings_path_if_exists_in_registry_key(const struct loader_instanc
 VkResult windows_get_loader_settings_file_path(const struct loader_instance *inst, char **out_path) {
     VkResult result = VK_SUCCESS;
     DWORD access_flags = KEY_QUERY_VALUE;
+    LONG rtn_value = 0;
     HKEY key = NULL;
-
     *out_path = NULL;
 
-    // if we are running with admin privileges, only check HKEY_LOCAL_MACHINE.
-    // Otherwise check HKEY_CURRENT_USER, and if nothing is there, look in HKEY_LOCAL_MACHINE
+    // Search in HKEY_CURRENT_USER first if we are running without admin privileges
+    // Exit if a settings file was found.
+    // Otherwise check in HKEY_LOCAL_MACHINE.
 
-    if (is_high_integrity()) {
-        LONG rtn_value = RegOpenKeyEx(HKEY_LOCAL_MACHINE, VK_SETTINGS_INFO_REGISTRY_LOC, 0, access_flags, &key);
-        if (ERROR_SUCCESS != rtn_value) {
-            result = VK_ERROR_FEATURE_NOT_PRESENT;
-            goto out;
-        }
-        result = get_settings_path_if_exists_in_registry_key(inst, out_path, key);
-    } else {
-        LONG rtn_value = RegOpenKeyEx(HKEY_CURRENT_USER, VK_SETTINGS_INFO_REGISTRY_LOC, 0, access_flags, &key);
+    if (!is_high_integrity()) {
+        rtn_value = RegOpenKeyEx(HKEY_CURRENT_USER, VK_SETTINGS_INFO_REGISTRY_LOC, 0, access_flags, &key);
         if (ERROR_SUCCESS == rtn_value) {
             result = get_settings_path_if_exists_in_registry_key(inst, out_path, key);
-            RegCloseKey(key);
+
             // Either we got OOM and *must* exit or we successfully found the settings file and can exit
             if (result == VK_ERROR_OUT_OF_HOST_MEMORY || result == VK_SUCCESS) {
                 goto out;
             }
+            RegCloseKey(key);
+            key = NULL;
         }
+    }
 
-        rtn_value = RegOpenKeyEx(HKEY_LOCAL_MACHINE, VK_SETTINGS_INFO_REGISTRY_LOC, 0, access_flags, &key);
-        if (ERROR_SUCCESS != rtn_value) {
-            result = VK_ERROR_FEATURE_NOT_PRESENT;
-            goto out;
-        }
+    rtn_value = RegOpenKeyEx(HKEY_LOCAL_MACHINE, VK_SETTINGS_INFO_REGISTRY_LOC, 0, access_flags, &key);
+    if (ERROR_SUCCESS != rtn_value) {
+        result = VK_ERROR_FEATURE_NOT_PRESENT;
+        goto out;
+    }
 
-        result = get_settings_path_if_exists_in_registry_key(inst, out_path, key);
-        if (result == VK_ERROR_OUT_OF_HOST_MEMORY) {
-            goto out;
-        }
+    result = get_settings_path_if_exists_in_registry_key(inst, out_path, key);
+    if (result == VK_ERROR_OUT_OF_HOST_MEMORY) {
+        goto out;
     }
 
 out:
index 6f59dc674ad8c3b5d23fb3da9e3ae01a6a664ff5..7c46046c4dc9338804a5b268829552857e54df84 100644 (file)
@@ -346,7 +346,8 @@ LSTATUS __stdcall ShimRegCloseKey(HKEY hKey) {
             return ERROR_SUCCESS;
         }
     }
-    return ERROR_SUCCESS;
+    // means that RegCloseKey was called with an invalid key value (one that doesn't exist or has already been closed)
+    exit(-1);
 }
 
 // Windows app package shims