loader: Prevent stack_alloc of 0
authorCharles Giessen <charles@lunarg.com>
Mon, 17 May 2021 22:09:27 +0000 (16:09 -0600)
committerCharles Giessen <46324611+charles-lunarg@users.noreply.github.com>
Thu, 9 Sep 2021 18:15:49 +0000 (12:15 -0600)
`loader_create_device_chain` would previously attempt to use stack allocations
when `expanded_activated_layer_list.count` was zero, which is undefined behavior

Change-Id: I56fab835a1728914191f1a7adc210dae58afa155

loader/loader.c

index 89adc8eb8f8041f3ac32ebb6ad150712ac3060e3..acd3f923cbc2982256d4379acaa68a86fac2e8cb 100644 (file)
@@ -4991,21 +4991,21 @@ VkResult loader_create_device_chain(const VkPhysicalDevice pd, const VkDeviceCre
             pNext = pNext->pNext;
         }
     }
+    if (dev->expanded_activated_layer_list.count > 0) {
+        layer_device_link_info = loader_stack_alloc(sizeof(VkLayerDeviceLink) * dev->expanded_activated_layer_list.count);
+        if (!layer_device_link_info) {
+            loader_log(inst, VULKAN_LOADER_ERROR_BIT, 0,
+                       "loader_create_device_chain: Failed to alloc Device objects for layer. Skipping Layer.");
+            return VK_ERROR_OUT_OF_HOST_MEMORY;
+        }
 
-    layer_device_link_info = loader_stack_alloc(sizeof(VkLayerDeviceLink) * dev->expanded_activated_layer_list.count);
-    if (!layer_device_link_info) {
-        loader_log(inst, VULKAN_LOADER_ERROR_BIT, 0,
-                   "loader_create_device_chain: Failed to alloc Device objects for layer. Skipping Layer.");
-        return VK_ERROR_OUT_OF_HOST_MEMORY;
-    }
-
-    activated_layer_names = loader_stack_alloc(sizeof(char *) * inst->expanded_activated_layer_list.count);
-    if (!activated_layer_names) {
-        loader_log(inst, VULKAN_LOADER_ERROR_BIT, 0, "loader_create_instance_chain: Failed to alloc activated layer names array");
-        return VK_ERROR_OUT_OF_HOST_MEMORY;
-    }
+        activated_layer_names = loader_stack_alloc(sizeof(char *) * inst->expanded_activated_layer_list.count);
+        if (!activated_layer_names) {
+            loader_log(inst, VULKAN_LOADER_ERROR_BIT, 0,
+                       "loader_create_instance_chain: Failed to alloc activated layer names array");
+            return VK_ERROR_OUT_OF_HOST_MEMORY;
+        }
 
-    if (dev->expanded_activated_layer_list.count > 0) {
         chain_info.sType = VK_STRUCTURE_TYPE_LOADER_DEVICE_CREATE_INFO;
         chain_info.function = VK_LAYER_LINK_INFO;
         chain_info.u.pLayerInfo = NULL;