Skip to content

Commit f3cc6f7

Browse files
kdaveadam900710
authored andcommitted
btrfs-progs: tests: fix fssum ASAN memory leak reports
Free memory after errors in sum(), this is reported by gcc 13 on the CI. This was reproduced by: $ make D=asan TEST=019-receive-clones-on-mounted-subvol test-misc Signed-off-by: David Sterba <[email protected]>
1 parent ea4ae56 commit f3cc6f7

File tree

1 file changed

+24
-9
lines changed

1 file changed

+24
-9
lines changed

tests/fssum.c

+24-9
Original file line numberDiff line numberDiff line change
@@ -524,6 +524,7 @@ sum(int dirfd, int level, sum_t *dircs, char *path_prefix, char *path_in)
524524
int ret;
525525
int fd;
526526
int excl;
527+
int error = 0;
527528
sum_file_data_t sum_file_data = flags[FLAG_STRUCTURE] ?
528529
sum_file_data_strict : sum_file_data_permissive;
529530
struct stat dir_st;
@@ -542,18 +543,22 @@ sum(int dirfd, int level, sum_t *dircs, char *path_prefix, char *path_in)
542543
if (!strcmp(de->d_name, ".") || !strcmp(de->d_name, ".."))
543544
continue;
544545
if (entries == alloclen) {
546+
void *tmp;
547+
545548
alloclen += CHUNKS;
546-
namelist = realloc(namelist,
547-
alloclen * sizeof(*namelist));
548-
if (!namelist) {
549-
fprintf(stderr, "malloc failed\n");
550-
exit(-1);
549+
tmp = realloc(namelist, alloclen * sizeof(*namelist));
550+
if (!tmp) {
551+
fprintf(stderr, "realloc failed\n");
552+
error = 1;
553+
goto free_namelist;
551554
}
555+
namelist = tmp;
552556
}
553557
namelist[entries] = strdup(de->d_name);
554558
if (!namelist[entries]) {
555-
fprintf(stderr, "malloc failed\n");
556-
exit(-1);
559+
fprintf(stderr, "stdup failed\n");
560+
error = 1;
561+
goto free_namelist;
557562
}
558563
++entries;
559564
}
@@ -577,13 +582,15 @@ sum(int dirfd, int level, sum_t *dircs, char *path_prefix, char *path_in)
577582
ret = fchdir(dirfd);
578583
if (ret == -1) {
579584
perror("fchdir");
580-
exit(-1);
585+
error = 1;
586+
goto free_namelist;
581587
}
582588
ret = lstat(namelist[i], &st);
583589
if (ret) {
584590
fprintf(stderr, "stat failed for %s/%s: %m\n",
585591
path_prefix, path);
586-
exit(-1);
592+
error = 1;
593+
goto free_namelist;
587594
}
588595

589596
/* We are crossing into a different subvol, skip this subtree. */
@@ -704,6 +711,14 @@ sum(int dirfd, int level, sum_t *dircs, char *path_prefix, char *path_in)
704711
next:
705712
free(path);
706713
}
714+
715+
free_namelist:
716+
closedir(d);
717+
for (i = 0; i < entries; i++)
718+
free(namelist[i]);
719+
free(namelist);
720+
if (error)
721+
exit(-1);
707722
}
708723

709724
int

0 commit comments

Comments
 (0)