diff --git a/mz_zip_rw.c b/mz_zip_rw.c index d6d19e3c..7834bdbe 100644 --- a/mz_zip_rw.c +++ b/mz_zip_rw.c @@ -845,20 +845,24 @@ int32_t mz_zip_reader_save_all(void *handle, const char *destination_dir) { resolved_name_size += (int)strlen(destination_dir) + 1; } - if (!path) { - path = (char *)malloc(resolved_name_size); - utf8_name = (char *)malloc(utf8_name_size); - resolved_name = (char *)malloc(resolved_name_size); - } else { - path = (char *)realloc(path, resolved_name_size); - utf8_name = (char *)realloc(utf8_name, utf8_name_size); - resolved_name = (char *)realloc(resolved_name, resolved_name_size); + char *new_path = (char *)realloc(path, resolved_name_size); + if (!new_path) { + err = MZ_MEM_ERROR; + goto save_all_cleanup; } - - if (!path || !utf8_name || !resolved_name) { + path = new_path; + char *new_utf8_name = (char *)realloc(utf8_name, utf8_name_size); + if (!new_utf8_name) { + err = MZ_MEM_ERROR; + goto save_all_cleanup; + } + utf8_name = new_utf8_name; + char *new_resolved_name = (char *)realloc(resolved_name, resolved_name_size); + if ( !new_resolved_name) { err = MZ_MEM_ERROR; goto save_all_cleanup; } + resolved_name = new_resolved_name; /* Construct output path */ path[0] = 0;