Skip to content

Commit

Permalink
Fixed possible memory leak if realloc fails.
Browse files Browse the repository at this point in the history
  • Loading branch information
nmoinvaz committed Oct 26, 2023
1 parent 4f670cc commit b34745d
Showing 1 changed file with 14 additions and 10 deletions.
24 changes: 14 additions & 10 deletions mz_zip_rw.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down

0 comments on commit b34745d

Please sign in to comment.