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 2a5543a
Showing 1 changed file with 15 additions and 10 deletions.
25 changes: 15 additions & 10 deletions mz_zip_rw.c
Original file line number Diff line number Diff line change
Expand Up @@ -829,6 +829,7 @@ int32_t mz_zip_reader_save_all(void *handle, const char *destination_dir) {
char *path = NULL;
char *utf8_name = NULL;
char *resolved_name = NULL;
char* new_alloc = NULL;

err = mz_zip_reader_goto_first_entry(handle);

Expand All @@ -845,20 +846,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);
new_alloc = (char *)realloc(path, resolved_name_size);
if (!new_alloc) {
err = MZ_MEM_ERROR;
goto save_all_cleanup;
}

if (!path || !utf8_name || !resolved_name) {
path = new_alloc;
new_alloc = (char *)realloc(utf8_name, utf8_name_size);
if (!new_alloc) {
err = MZ_MEM_ERROR;
goto save_all_cleanup;
}
utf8_name = new_alloc;
new_alloc = (char *)realloc(resolved_name, resolved_name_size);
if ( !new_alloc) {
err = MZ_MEM_ERROR;
goto save_all_cleanup;
}
resolved_name = new_alloc;

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

0 comments on commit 2a5543a

Please sign in to comment.