Skip to content

Commit 37ce381

Browse files
committed
worktree: fix resource leaks when branch creation fails
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 <johannes.schindelin@gmx.de>
1 parent 58f4661 commit 37ce381

1 file changed

Lines changed: 5 additions & 2 deletions

File tree

builtin/worktree.c

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -945,14 +945,17 @@ static int add(int ac, const char **av, const char *prefix,
945945
strvec_push(&cp.args, branch);
946946
if (opt_track)
947947
strvec_push(&cp.args, opt_track);
948-
if (run_command(&cp))
949-
return -1;
948+
if (run_command(&cp)) {
949+
ret = -1;
950+
goto cleanup;
951+
}
950952
branch = new_branch;
951953
} else if (opt_track) {
952954
die(_("--[no-]track can only be used if a new branch is created"));
953955
}
954956

955957
ret = add_worktree(path, branch, &opts);
958+
cleanup:
956959
free(path);
957960
free(opt_track);
958961
free(branch_to_free);

0 commit comments

Comments
 (0)