Skip to content

Commit 208e174

Browse files
Make realloc usage consistent
Makes sure all calls to realloc have their return value put into a new variable. That way we don't lose the pointer to the original allocation in case realloc returns NULL and we need to free the original allocation.
1 parent bc0a957 commit 208e174

File tree

2 files changed

+16
-12
lines changed

2 files changed

+16
-12
lines changed

loader/loader.c

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -317,11 +317,12 @@ VkResult increase_str_capacity_by_at_least_one(const struct loader_instance *ins
317317
}
318318
} else if (string_list->count + 1 > string_list->allocated_count) {
319319
uint32_t new_allocated_count = string_list->allocated_count * 2;
320-
string_list->list = loader_instance_heap_realloc(inst, string_list->list, sizeof(char *) * string_list->allocated_count,
321-
sizeof(char *) * new_allocated_count, VK_SYSTEM_ALLOCATION_SCOPE_INSTANCE);
322-
if (NULL == string_list->list) {
320+
void *new_ptr = loader_instance_heap_realloc(inst, string_list->list, sizeof(char *) * string_list->allocated_count,
321+
sizeof(char *) * new_allocated_count, VK_SYSTEM_ALLOCATION_SCOPE_INSTANCE);
322+
if (NULL == new_ptr) {
323323
return VK_ERROR_OUT_OF_HOST_MEMORY;
324324
}
325+
string_list->list = new_ptr;
325326
string_list->allocated_count *= 2;
326327
}
327328
return VK_SUCCESS;
@@ -1045,12 +1046,13 @@ VkResult loader_init_generic_list(const struct loader_instance *inst, struct loa
10451046
}
10461047

10471048
VkResult loader_resize_generic_list(const struct loader_instance *inst, struct loader_generic_list *list_info) {
1048-
list_info->list = loader_instance_heap_realloc(inst, list_info->list, list_info->capacity, list_info->capacity * 2,
1049-
VK_SYSTEM_ALLOCATION_SCOPE_INSTANCE);
1050-
if (list_info->list == NULL) {
1049+
void *new_ptr = loader_instance_heap_realloc(inst, list_info->list, list_info->capacity, list_info->capacity * 2,
1050+
VK_SYSTEM_ALLOCATION_SCOPE_INSTANCE);
1051+
if (new_ptr == NULL) {
10511052
loader_log(inst, VULKAN_LOADER_ERROR_BIT, 0, "loader_resize_generic_list: Failed to allocate space for generic list");
10521053
return VK_ERROR_OUT_OF_HOST_MEMORY;
10531054
}
1055+
list_info->list = new_ptr;
10541056
list_info->capacity = list_info->capacity * 2;
10551057
return VK_SUCCESS;
10561058
}

loader/loader_windows.c

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -817,12 +817,13 @@ VkResult enumerate_adapter_physical_devices(struct loader_instance *inst, struct
817817

818818
// Get the actual physical devices
819819
do {
820-
next_icd_phys_devs->physical_devices = loader_instance_heap_realloc(
821-
inst, next_icd_phys_devs->physical_devices, next_icd_phys_devs->device_count * sizeof(VkPhysicalDevice),
822-
count * sizeof(VkPhysicalDevice), VK_SYSTEM_ALLOCATION_SCOPE_COMMAND);
823-
if (next_icd_phys_devs->physical_devices == NULL) {
820+
void *new_ptr = loader_instance_heap_realloc(inst, next_icd_phys_devs->physical_devices,
821+
next_icd_phys_devs->device_count * sizeof(VkPhysicalDevice),
822+
count * sizeof(VkPhysicalDevice), VK_SYSTEM_ALLOCATION_SCOPE_COMMAND);
823+
if (new_ptr == NULL) {
824824
return VK_ERROR_OUT_OF_HOST_MEMORY;
825825
}
826+
next_icd_phys_devs->physical_devices = new_ptr;
826827
next_icd_phys_devs->device_count = count;
827828
} while ((res = icd_term->scanned_icd->EnumerateAdapterPhysicalDevices(icd_term->instance, luid, &count,
828829
next_icd_phys_devs->physical_devices)) == VK_INCOMPLETE);
@@ -978,13 +979,14 @@ VkResult windows_read_sorted_physical_devices(struct loader_instance *inst, uint
978979

979980
if (icd_phys_devs_array_size <= *icd_phys_devs_array_count) {
980981
uint32_t old_size = icd_phys_devs_array_size * sizeof(struct loader_icd_physical_devices);
981-
*icd_phys_devs_array = loader_instance_heap_realloc(inst, *icd_phys_devs_array, old_size, 2 * old_size,
982-
VK_SYSTEM_ALLOCATION_SCOPE_COMMAND);
982+
void *new_ptr = loader_instance_heap_realloc(inst, *icd_phys_devs_array, old_size, 2 * old_size,
983+
VK_SYSTEM_ALLOCATION_SCOPE_COMMAND);
983984
if (*icd_phys_devs_array == NULL) {
984985
adapter->lpVtbl->Release(adapter);
985986
res = VK_ERROR_OUT_OF_HOST_MEMORY;
986987
goto out;
987988
}
989+
*icd_phys_devs_array = new_ptr;
988990
icd_phys_devs_array_size *= 2;
989991
}
990992
(*icd_phys_devs_array)[*icd_phys_devs_array_count].device_count = 0;

0 commit comments

Comments
 (0)