Skip to content

Commit 6f7d643

Browse files
committed
mkcomposefs: Fix memory leaks on failure
This quiets ASAN. (Obligatory "this wouldn't happen in Rust" comment; note the subtle change I needed to make to handle an error return in the middle of the hardlink fixup code to ensure the linked list was always in a valid state) Signed-off-by: Colin Walters <walters@verbum.org>
1 parent 1d7f32e commit 6f7d643

File tree

1 file changed

+17
-2
lines changed

1 file changed

+17
-2
lines changed

tools/mkcomposefs.c

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -394,11 +394,22 @@ static char *tree_resolve_hardlinks(dump_info *info)
394394
free(fixup->target_path);
395395
free(fixup);
396396

397-
fixup = next;
397+
fixup = info->hardlink_fixups = next;
398398
}
399399
return NULL;
400400
}
401401

402+
static void tree_destroy(dump_info *info)
403+
{
404+
if (info->root)
405+
lcfs_node_unref(info->root);
406+
hardlink_fixup *fixup = info->hardlink_fixups;
407+
while (fixup != NULL) {
408+
free(fixup->target_path);
409+
free(fixup);
410+
}
411+
}
412+
402413
static char *tree_from_dump_line(dump_info *info, const char *line,
403414
size_t line_len, bool strict_mode)
404415
{
@@ -683,6 +694,7 @@ static struct lcfs_node_s *tree_from_dump(FILE *input, char **out_err)
683694
&info, line, line_len, strict_mode);
684695
if (err != NULL) {
685696
*out_err = err;
697+
tree_destroy(&info);
686698
buffer_free(&buf);
687699
return NULL;
688700
}
@@ -700,11 +712,13 @@ static struct lcfs_node_s *tree_from_dump(FILE *input, char **out_err)
700712
char *err = tree_from_dump_line(&info, buf.buf, buf.size, strict_mode);
701713
if (err != NULL) {
702714
*out_err = err;
715+
tree_destroy(&info);
703716
buffer_free(&buf);
704717
return NULL;
705718
}
706719
} else if (buf.size > 0) {
707720
*out_err = make_error("Missing trailing newline");
721+
tree_destroy(&info);
708722
return NULL;
709723
}
710724

@@ -714,6 +728,7 @@ static struct lcfs_node_s *tree_from_dump(FILE *input, char **out_err)
714728
char *err = tree_resolve_hardlinks(&info);
715729
if (err) {
716730
*out_err = err;
731+
tree_destroy(&info);
717732
return NULL;
718733
}
719734

@@ -792,7 +807,7 @@ int main(int argc, char **argv)
792807
LLVMFuzzerTestOneInput((void *)buf, len);
793808
return 0;
794809
}
795-
extern void HF_ITER(uint8_t * *buf, size_t * len);
810+
extern void HF_ITER(uint8_t **buf, size_t *len);
796811
for (;;) {
797812
size_t len;
798813
uint8_t *buf;

0 commit comments

Comments
 (0)