From: Zhihao Cheng <chengzhihao1@huawei.com>
Date: Thu, 22 Feb 2024 12:37:40 +0000 (+0800)
Subject: mkfs.ubifs: Fix memleak for 'output' in error paths
X-Git-Tag: v2.2.1~4
X-Git-Url: https://www.infradead.org/git/?a=commitdiff_plain;h=6ff8739a6bf5fce30e9d99ee4b35322313300dcb;p=mtd-utils.git

mkfs.ubifs: Fix memleak for 'output' in error paths

The 'output' is allocated in get_options(), don't forget to free it
in error paths, move 'output' freeing out of close_target(), which
simplifies the logic of close_target().

Fixes: 36ec51948e0ec ("Add mkfs.ubifs")
Signed-off-by: Zhihao Cheng <chengzhihao1@huawei.com>
Signed-off-by: David Oberhollenzer <david.oberhollenzer@sigma-star.at>
---

diff --git a/ubifs-utils/mkfs.ubifs/mkfs.ubifs.c b/ubifs-utils/mkfs.ubifs/mkfs.ubifs.c
index 453a5c7..f4cb512 100644
--- a/ubifs-utils/mkfs.ubifs/mkfs.ubifs.c
+++ b/ubifs-utils/mkfs.ubifs/mkfs.ubifs.c
@@ -2886,8 +2886,6 @@ static int close_target(void)
 		if (close(out_fd) == -1)
 			return sys_err_msg("cannot close the target '%s'", output);
 	}
-	if (output)
-		free(output);
 	return 0;
 }
 
@@ -3081,6 +3079,7 @@ int main(int argc, char *argv[])
 		printf("Success!\n");
 
 out:
+	free(output);
 	close_ubi();
 	crypto_cleanup();
 	return err;