loader: Fix stack overflow with large manifests
authorLenny Komow <lenny@lunarg.com>
Fri, 23 Apr 2021 21:02:18 +0000 (15:02 -0600)
committerLenny Komow <lenny@lunarg.com>
Sat, 24 Apr 2021 19:54:27 +0000 (13:54 -0600)
If the json manifest file was too large, the loader would do a stack
allocation that would fail, throwing an exception or creating
undefined behavior. This changes to a heap allocation.

loader/loader.c

index 2a9fe88690c90dc13805fc5a3b9632734df5315c..2ddb8e1093cffe4f634d0f8c50599ef3fa97b2c5 100644 (file)
@@ -2621,7 +2621,7 @@ static void loader_get_fullpath(const char *file, const char *dirs, size_t out_s
 //            This returned buffer should be freed by caller.
 static VkResult loader_get_json(const struct loader_instance *inst, const char *filename, cJSON **json) {
     FILE *file = NULL;
-    char *json_buf;
+    char *json_buf = NULL;
     size_t len;
     VkResult res = VK_SUCCESS;
 
@@ -2647,7 +2647,7 @@ static VkResult loader_get_json(const struct loader_instance *inst, const char *
     } while (fread_ret_count == 256 && !feof(file));
     len = ftell(file);
     fseek(file, 0, SEEK_SET);
-    json_buf = (char *)loader_stack_alloc(len + 1);
+    json_buf = (char *)loader_instance_heap_alloc(inst, len + 1, VK_SYSTEM_ALLOCATION_SCOPE_COMMAND);
     if (json_buf == NULL) {
         loader_log(inst, VK_DEBUG_REPORT_ERROR_BIT_EXT, 0,
                    "loader_get_json: Failed to allocate space for "
@@ -2681,6 +2681,9 @@ static VkResult loader_get_json(const struct loader_instance *inst, const char *
     }
 
 out:
+    if (NULL != json_buf) {
+        loader_instance_heap_free(inst, json_buf);
+    }
     if (NULL != file) {
         fclose(file);
     }