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 31e13f4
Showing 1 changed file with 8 additions and 10 deletions.
18 changes: 8 additions & 10 deletions mz_zip_rw.c
Original file line number Diff line number Diff line change
Expand Up @@ -845,21 +845,19 @@ 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);
char *new_utf8_name = (char *)realloc(utf8_name, utf8_name_size);
char *new_resolved_name = (char *)realloc(resolved_name, resolved_name_size);

if (!path || !utf8_name || !resolved_name) {
if (!new_path || !new_utf8_name || !new_resolved_name) {
err = MZ_MEM_ERROR;
goto save_all_cleanup;
}

path = new_path;
utf8_name = new_utf8_name;
resolved_name = new_resolved_name;

/* Construct output path */
path[0] = 0;

Expand Down

0 comments on commit 31e13f4

Please sign in to comment.