diff mbox series

[v4,13/17] submodule--helper: fix a leak in module_add()

Message ID patch-v4-13.17-b27b665d287-20220728T162442Z-avarab@gmail.com (mailing list archive)
State Accepted
Commit 623bd7d154daf93e910414bfc8b3080c7269f2a6
Headers show
Series submodule--helper: (only) fix memory leaks | expand

Commit Message

Ævar Arnfjörð Bjarmason July 28, 2022, 4:30 p.m. UTC
Fix a leak in module_path(), since a6226fd772b (submodule--helper:
convert the bulk of cmd_add() to C, 2021-08-10), we've been freeing
add_data.sm_path, but in this case we clobbered it, and didn't free
the value we clobbered.

This makes test 28 of "t/t7400-submodule-basic.sh" ("submodule add in
subdirectory") pass when we're compiled with SANITIZE=leak..

Signed-off-by: Ævar Arnfjörð Bjarmason <avarab@gmail.com>
---
 builtin/submodule--helper.c | 8 ++++++--
 1 file changed, 6 insertions(+), 2 deletions(-)
diff mbox series

Patch

diff --git a/builtin/submodule--helper.c b/builtin/submodule--helper.c
index 0f6c07e3d1e..6077aecd202 100644
--- a/builtin/submodule--helper.c
+++ b/builtin/submodule--helper.c
@@ -3290,8 +3290,12 @@  static int module_add(int argc, const char **argv, const char *prefix)
 	else
 		add_data.sm_path = xstrdup(argv[1]);
 
-	if (prefix && *prefix && !is_absolute_path(add_data.sm_path))
-		add_data.sm_path = xstrfmt("%s%s", prefix, add_data.sm_path);
+	if (prefix && *prefix && !is_absolute_path(add_data.sm_path)) {
+		char *sm_path = add_data.sm_path;
+
+		add_data.sm_path = xstrfmt("%s%s", prefix, sm_path);
+		free(sm_path);
+	}
 
 	if (starts_with_dot_dot_slash(add_data.repo) ||
 	    starts_with_dot_slash(add_data.repo)) {