Thread (44 messages) 44 messages, 5 authors, 14h ago
HOTtoday
Revisions (2)
  1. v1 current
  2. v2 [diff vs current]

[PATCH 09/13] worktree: fix resource leaks when branch creation fails

From: Johannes Schindelin via GitGitGadget <hidden>
Date: 2026-07-01 07:04:47
Subsystem: the rest · Maintainer: Linus Torvalds

From: Johannes Schindelin <redacted>

In the "add" subcommand, when run_command() fails while creating
a new branch (line 948), the function returns -1 immediately
without freeing the allocations made earlier: path (from
prefix_filename at line 858), opt_track, branch_to_free, and
new_branch_to_free.

Redirect the error return through the existing cleanup block at
the end of the function so all four allocations are properly
freed.

Pointed out by Coverity.

Assisted-by: Claude Opus 4.6
Signed-off-by: Johannes Schindelin <redacted>
---
 builtin/worktree.c | 7 +++++--
 1 file changed, 5 insertions(+), 2 deletions(-)
diff --git a/builtin/worktree.c b/builtin/worktree.c
index d21c43fde3..4bc7b4f6e7 100644
--- a/builtin/worktree.c
+++ b/builtin/worktree.c
@@ -945,14 +945,17 @@ static int add(int ac, const char **av, const char *prefix,
 		strvec_push(&cp.args, branch);
 		if (opt_track)
 			strvec_push(&cp.args, opt_track);
-		if (run_command(&cp))
-			return -1;
+		if (run_command(&cp)) {
+			ret = -1;
+			goto cleanup;
+		}
 		branch = new_branch;
 	} else if (opt_track) {
 		die(_("--[no-]track can only be used if a new branch is created"));
 	}
 
 	ret = add_worktree(path, branch, &opts);
+cleanup:
 	free(path);
 	free(opt_track);
 	free(branch_to_free);
-- 
gitgitgadget
Keyboard shortcuts
hback out one level
jnext message in thread
kprevious message in thread
ldrill in
Escclose help / fold thread tree
?toggle this help