sequencer API users: fix get_replay_opts() leaks

Make the replay_opts_release() function added in the preceding commit
non-static, and use it for freeing the "struct replay_opts"
constructed for "rebase" and "revert".

To safely call our new replay_opts_release() we'll need to stop
calling it in sequencer_remove_state(), and instead call it where we
allocate the "struct replay_opts" itself.

This is because in e.g. do_interactive_rebase() we construct a "struct
replay_opts" with "get_replay_opts()", and then call
"complete_action()". If we get far enough in that function without
encountering errors we'll call "pick_commits()" which (indirectly)
calls sequencer_remove_state() at the end.

But if we encounter errors anywhere along the way we'd punt out early,
and not free() the memory we allocated. Remembering whether we
previously called sequencer_remove_state() would be a hassle.

Using a FREE_AND_NULL() pattern would also work, as it would be safe
to call replay_opts_release() repeatedly. But let's fix this properly
instead, by having the owner of the data free() it.

Signed-off-by: Ævar Arnfjörð Bjarmason <avarab@gmail.com>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
This commit is contained in:
Ævar Arnfjörð Bjarmason 2023-02-06 20:08:08 +01:00 committed by Junio C Hamano
parent 6a09c3a9a6
commit 9ff2f06069
19 changed files with 23 additions and 5 deletions

View File

@ -297,6 +297,7 @@ static int do_interactive_rebase(struct rebase_options *opts, unsigned flags)
} }
cleanup: cleanup:
replay_opts_release(&replay);
free(revisions); free(revisions);
free(shortrevisions); free(shortrevisions);
todo_list_release(&todo_list); todo_list_release(&todo_list);
@ -338,6 +339,7 @@ static int run_sequencer_rebase(struct rebase_options *opts)
struct replay_opts replay_opts = get_replay_opts(opts); struct replay_opts replay_opts = get_replay_opts(opts);
ret = sequencer_continue(the_repository, &replay_opts); ret = sequencer_continue(the_repository, &replay_opts);
replay_opts_release(&replay_opts);
break; break;
} }
case ACTION_EDIT_TODO: case ACTION_EDIT_TODO:
@ -553,6 +555,7 @@ static int finish_rebase(struct rebase_options *opts)
replay.action = REPLAY_INTERACTIVE_REBASE; replay.action = REPLAY_INTERACTIVE_REBASE;
ret = sequencer_remove_state(&replay); ret = sequencer_remove_state(&replay);
replay_opts_release(&replay);
} else { } else {
strbuf_addstr(&dir, opts->state_dir); strbuf_addstr(&dir, opts->state_dir);
if (remove_dir_recursively(&dir, 0)) if (remove_dir_recursively(&dir, 0))
@ -1324,6 +1327,7 @@ int cmd_rebase(int argc, const char **argv, const char *prefix)
replay.action = REPLAY_INTERACTIVE_REBASE; replay.action = REPLAY_INTERACTIVE_REBASE;
ret = sequencer_remove_state(&replay); ret = sequencer_remove_state(&replay);
replay_opts_release(&replay);
} else { } else {
strbuf_reset(&buf); strbuf_reset(&buf);
strbuf_addstr(&buf, options.state_dir); strbuf_addstr(&buf, options.state_dir);

View File

@ -251,6 +251,7 @@ int cmd_revert(int argc, const char **argv, const char *prefix)
if (opts.revs) if (opts.revs)
release_revisions(opts.revs); release_revisions(opts.revs);
free(opts.revs); free(opts.revs);
replay_opts_release(&opts);
return res; return res;
} }
@ -267,5 +268,6 @@ int cmd_cherry_pick(int argc, const char **argv, const char *prefix)
free(opts.revs); free(opts.revs);
if (res < 0) if (res < 0)
die(_("cherry-pick failed")); die(_("cherry-pick failed"));
replay_opts_release(&opts);
return res; return res;
} }

View File

@ -351,7 +351,7 @@ static const char *gpg_sign_opt_quoted(struct replay_opts *opts)
return buf.buf; return buf.buf;
} }
static void replay_opts_release(struct replay_opts *opts) void replay_opts_release(struct replay_opts *opts)
{ {
free(opts->gpg_sign); free(opts->gpg_sign);
free(opts->reflog_action); free(opts->reflog_action);
@ -385,8 +385,6 @@ int sequencer_remove_state(struct replay_opts *opts)
} }
} }
replay_opts_release(opts);
strbuf_reset(&buf); strbuf_reset(&buf);
strbuf_addstr(&buf, get_dir(opts)); strbuf_addstr(&buf, get_dir(opts));
if (remove_dir_recursively(&buf, 0)) if (remove_dir_recursively(&buf, 0))

View File

@ -158,6 +158,7 @@ int sequencer_pick_revisions(struct repository *repo,
int sequencer_continue(struct repository *repo, struct replay_opts *opts); int sequencer_continue(struct repository *repo, struct replay_opts *opts);
int sequencer_rollback(struct repository *repo, struct replay_opts *opts); int sequencer_rollback(struct repository *repo, struct replay_opts *opts);
int sequencer_skip(struct repository *repo, struct replay_opts *opts); int sequencer_skip(struct repository *repo, struct replay_opts *opts);
void replay_opts_release(struct replay_opts *opts);
int sequencer_remove_state(struct replay_opts *opts); int sequencer_remove_state(struct replay_opts *opts);
#define TODO_LIST_KEEP_EMPTY (1U << 0) #define TODO_LIST_KEEP_EMPTY (1U << 0)

View File

@ -5,6 +5,7 @@ test_description='rebase should handle arbitrary git message'
GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main
export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
TEST_PASSES_SANITIZE_LEAK=true
. ./test-lib.sh . ./test-lib.sh
. "$TEST_DIRECTORY"/lib-rebase.sh . "$TEST_DIRECTORY"/lib-rebase.sh

View File

@ -7,6 +7,7 @@ Tests if git rebase --root --onto <newparent> can rebase the root commit.
GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main
export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
TEST_PASSES_SANITIZE_LEAK=true
. ./test-lib.sh . ./test-lib.sh
log_with_names () { log_with_names () {

View File

@ -5,6 +5,7 @@ test_description='git rebase - test patch id computation'
GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main
export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
TEST_PASSES_SANITIZE_LEAK=true
. ./test-lib.sh . ./test-lib.sh
scramble () { scramble () {

View File

@ -2,6 +2,7 @@
test_description='git rebase interactive with rewording' test_description='git rebase interactive with rewording'
TEST_PASSES_SANITIZE_LEAK=true
. ./test-lib.sh . ./test-lib.sh
. "$TEST_DIRECTORY"/lib-rebase.sh . "$TEST_DIRECTORY"/lib-rebase.sh

View File

@ -1,6 +1,8 @@
#!/bin/sh #!/bin/sh
test_description='rebase topology tests with merges' test_description='rebase topology tests with merges'
TEST_PASSES_SANITIZE_LEAK=true
. ./test-lib.sh . ./test-lib.sh
. "$TEST_DIRECTORY"/lib-rebase.sh . "$TEST_DIRECTORY"/lib-rebase.sh

View File

@ -14,6 +14,7 @@ to the "fixup" command that works with "fixup!", "fixup -C" works with
"amend!" upon --autosquash. "amend!" upon --autosquash.
' '
TEST_PASSES_SANITIZE_LEAK=true
. ./test-lib.sh . ./test-lib.sh
. "$TEST_DIRECTORY"/lib-rebase.sh . "$TEST_DIRECTORY"/lib-rebase.sh

View File

@ -1,6 +1,8 @@
#!/bin/sh #!/bin/sh
test_description='rebase behavior when on-disk files are broken' test_description='rebase behavior when on-disk files are broken'
TEST_PASSES_SANITIZE_LEAK=true
. ./test-lib.sh . ./test-lib.sh
test_expect_success 'set up conflicting branches' ' test_expect_success 'set up conflicting branches' '

View File

@ -13,6 +13,7 @@ test_description='test cherry-pick and revert with renames
GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main
export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
TEST_PASSES_SANITIZE_LEAK=true
. ./test-lib.sh . ./test-lib.sh
test_expect_success setup ' test_expect_success setup '

View File

@ -11,6 +11,7 @@ test_description='cherry picking and reverting a merge
GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main
export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
TEST_PASSES_SANITIZE_LEAK=true
. ./test-lib.sh . ./test-lib.sh
test_expect_success setup ' test_expect_success setup '

View File

@ -5,6 +5,7 @@ test_description='test cherry-picking (and reverting) a root commit'
GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main
export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
TEST_PASSES_SANITIZE_LEAK=true
. ./test-lib.sh . ./test-lib.sh
test_expect_success setup ' test_expect_success setup '

View File

@ -5,6 +5,7 @@ test_description='test cherry-picking with --ff option'
GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main
export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
TEST_PASSES_SANITIZE_LEAK=true
. ./test-lib.sh . ./test-lib.sh
test_expect_success setup ' test_expect_success setup '

View File

@ -2,6 +2,7 @@
test_description='Test cherry-pick -x and -s' test_description='Test cherry-pick -x and -s'
TEST_PASSES_SANITIZE_LEAK=true
. ./test-lib.sh . ./test-lib.sh
pristine_detach () { pristine_detach () {

View File

@ -5,6 +5,7 @@
test_description='Test rebasing, stashing, etc. with submodules' test_description='Test rebasing, stashing, etc. with submodules'
TEST_PASSES_SANITIZE_LEAK=true
. ./test-lib.sh . ./test-lib.sh
test_expect_success setup ' test_expect_success setup '

View File

@ -3,7 +3,6 @@
# Copyright (c) 2006 Eric Wong # Copyright (c) 2006 Eric Wong
test_description='git svn commit-diff clobber' test_description='git svn commit-diff clobber'
TEST_FAILS_SANITIZE_LEAK=true
. ./lib-git-svn.sh . ./lib-git-svn.sh
test_expect_success 'initialize repo' ' test_expect_success 'initialize repo' '

View File

@ -5,7 +5,6 @@
test_description='concurrent git svn dcommit' test_description='concurrent git svn dcommit'
TEST_FAILS_SANITIZE_LEAK=true
. ./lib-git-svn.sh . ./lib-git-svn.sh