Skip to content

Commit

Permalink
fix potential (unlikely) mem leak in app/utils/dirs.c (#276)
Browse files Browse the repository at this point in the history
  • Loading branch information
liquidaty authored Nov 9, 2024
1 parent 731e553 commit 748d02a
Showing 1 changed file with 11 additions and 7 deletions.
18 changes: 11 additions & 7 deletions app/utils/dirs.c
Original file line number Diff line number Diff line change
Expand Up @@ -234,12 +234,12 @@ static int zsv_foreach_dirent_remove(struct zsv_foreach_dirent_handle *h, size_t
}
}
} else { // dir
struct dir_path *dn = calloc(1, sizeof(*dn));
if (!dn) {
fprintf(stderr, "Out of memory!\n");
return 1;
}
if (h->parent_and_entry) {
struct dir_path *dn = calloc(1, sizeof(*dn));
if (!dn) {
fprintf(stderr, "Out of memory!\n");
return 1;
}
dn->path = strdup(h->parent_and_entry);
dn->next = *((struct dir_path **)h->ctx);
*((struct dir_path **)h->ctx) = dn;
Expand Down Expand Up @@ -305,10 +305,14 @@ int zsv_remove_dir_recursive(const unsigned char *path) {
// delete directories in the reverse order we received them
struct dir_path *reverse_dirs = NULL;
int err = zsv_foreach_dirent((const char *)path, 0, zsv_foreach_dirent_remove, &reverse_dirs, 0);
// unlink and free each dir

// unlink each dir
for (struct dir_path *dn = reverse_dirs; !err && dn; dn = dn->next)
rmdir_w_msg(dn->path, &err);

// free each dir
for (struct dir_path *next, *dn = reverse_dirs; !err && dn; dn = next) {
next = dn->next;
rmdir_w_msg(dn->path, &err);
free(dn->path);
free(dn);
}
Expand Down

0 comments on commit 748d02a

Please sign in to comment.