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> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Johannes Schindelin committed Jul 5, 2026 at 08:24 UTC da2211be7461762a47b67449cc3a518b8942ec84
1 file changed +5 -2
builtin/worktree.c
+5 -2
@@ -945,14 +945,17 @@ static int add(int ac, const char **av, const char *prefix,
945 strvec_push(&cp.args, branch);
946 if (opt_track)
947 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 + }
952 branch = new_branch;
953 } else if (opt_track) {
954 die(_("--[no-]track can only be used if a new branch is created"));
955 }
956
957 ret = add_worktree(path, branch, &opts);
958 +cleanup:
959 free(path);
960 free(opt_track);
961 free(branch_to_free);