Added error handling for malloc and strdup

This commit is contained in:
norgera 2024-07-27 12:30:07 -04:00
parent 5e2727fe03
commit a59cae21c4
2 changed files with 17 additions and 0 deletions

View file

@ -60,6 +60,12 @@ GGML_CALL ggml_backend_buffer_t ggml_backend_buffer_init(
size_t size) {
ggml_backend_buffer_t buffer = malloc(sizeof(struct ggml_backend_buffer));
if (buffer == NULL) {
// Log the error and handle appropriately
fprintf(stderr, "Memory allocation failed in ggml_backend_buffer_init\n");
return NULL; // or handle the error as appropriate
}
(*buffer) = (struct ggml_backend_buffer) {
/* .interface = */ iface,
/* .buft = */ buft,

View file

@ -238,6 +238,11 @@ static std::vector<ggml_vk_device> ggml_vk_available_devices_internal(size_t mem
d.type = dev_props.deviceType;
d.heapSize = heapSize;
d.vendor = strdup(ggml_vk_getVendorName(dev_props.vendorID));
if (d.vendor == NULL) {
// Handle strdup failure
std::cerr << __func__ << ": strdup failed for vendor name\n";
continue; // or handle the error as appropriate
}
d.subgroupSize = subgroup_props.subgroupSize;
d.bufferAlignment = dev_props.limits.minStorageBufferOffsetAlignment;
@ -253,6 +258,12 @@ static std::vector<ggml_vk_device> ggml_vk_available_devices_internal(size_t mem
name += " (" + std::to_string(n_idx) + ")";
}
d.name = strdup(name.c_str());
if (d.name == NULL) {
// Handle strdup failure
std::cerr << __func__ << ": strdup failed for device name\n";
free(d.vendor); // Clean up the previously allocated vendor name
continue; // or handle the error as appropriate
}
results.push_back(d);
}