loader: Override layer load when custom allocator used.
authorDorian Apanel <dorian.apanel@gmail.com>
Wed, 7 Oct 2020 09:18:51 +0000 (11:18 +0200)
committerLenny Komow <lenny@lunarg.com>
Thu, 8 Oct 2020 22:19:21 +0000 (16:19 -0600)
When custom allocator is used, and is requested to allocate
zero size allocation, it can return NULL, which is then
interpreted as VK_ERROR_OUT_OF_HOST_MEMORY.

When such allocator was used, override layer was not loaded.

fix #485

loader/loader.c

index 0b91cc4bda8925ea24b769b4b8f6739051b82c0f..a29550119aea97e9aa22c0fb1a91eede2e959c99 100644 (file)
@@ -3189,30 +3189,31 @@ static VkResult loaderReadLayerJson(const struct loader_instance *inst, struct l
                        name);
         } else {
             props->num_blacklist_layers = cJSON_GetArraySize(blacklisted_layers);
-
-            // Allocate the blacklist array
-            props->blacklist_layer_names = loader_instance_heap_alloc(
-                inst, sizeof(char[MAX_STRING_SIZE]) * props->num_blacklist_layers, VK_SYSTEM_ALLOCATION_SCOPE_INSTANCE);
-            if (props->blacklist_layer_names == NULL) {
-                result = VK_ERROR_OUT_OF_HOST_MEMORY;
-                goto out;
-            }
-
-            // Copy the blacklisted layers into the array
-            for (i = 0; i < (int)props->num_blacklist_layers; ++i) {
-                cJSON *black_layer = cJSON_GetArrayItem(blacklisted_layers, i);
-                if (black_layer == NULL) {
-                    continue;
-                }
-                temp = cJSON_Print(black_layer);
-                if (temp == NULL) {
+            if (props->num_blacklist_layers > 0) {
+                // Allocate the blacklist array
+                props->blacklist_layer_names = loader_instance_heap_alloc(
+                    inst, sizeof(char[MAX_STRING_SIZE]) * props->num_blacklist_layers, VK_SYSTEM_ALLOCATION_SCOPE_INSTANCE);
+                if (props->blacklist_layer_names == NULL) {
                     result = VK_ERROR_OUT_OF_HOST_MEMORY;
                     goto out;
                 }
-                temp[strlen(temp) - 1] = '\0';
-                strncpy(props->blacklist_layer_names[i], temp + 1, MAX_STRING_SIZE - 1);
-                props->blacklist_layer_names[i][MAX_STRING_SIZE - 1] = '\0';
-                cJSON_Free(temp);
+
+                // Copy the blacklisted layers into the array
+                for (i = 0; i < (int)props->num_blacklist_layers; ++i) {
+                    cJSON *black_layer = cJSON_GetArrayItem(blacklisted_layers, i);
+                    if (black_layer == NULL) {
+                        continue;
+                    }
+                    temp = cJSON_Print(black_layer);
+                    if (temp == NULL) {
+                        result = VK_ERROR_OUT_OF_HOST_MEMORY;
+                        goto out;
+                    }
+                    temp[strlen(temp) - 1] = '\0';
+                    strncpy(props->blacklist_layer_names[i], temp + 1, MAX_STRING_SIZE - 1);
+                    props->blacklist_layer_names[i][MAX_STRING_SIZE - 1] = '\0';
+                    cJSON_Free(temp);
+                }
             }
         }
     }