builtin/rebase: fix leaking `commit.gpgsign` value

In `get_replay_opts()`, we override the `gpg_sign` field that already got populated by `sequencer_init_config()` in case the user has "commit.gpgsign" set in their config. This creates a memory leak because we overwrite the previously assigned value, which may have already pointed to an allocated string. Let's plug the memory leak by freeing the value before we overwrite it. Signed-off-by: Patrick Steinhardt <ps@pks.im> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Patrick Steinhardt committed Aug 14, 2024 at 08:52 UTC 1ca57bea4a8a4637c4e7d2a2f46677acc4795d81
5 files changed +5
builtin/rebase.c
+1
@@ -186,6 +186,7 @@ static struct replay_opts get_replay_opts(const struct rebase_options *opts)
186 replay.committer_date_is_author_date =
187 opts->committer_date_is_author_date;
188 replay.ignore_date = opts->ignore_date;
189 + free(replay.gpg_sign);
190 replay.gpg_sign = xstrdup_or_null(opts->gpg_sign_opt);
191 replay.reflog_action = xstrdup(opts->reflog_action);
192 if (opts->strategy)
sequencer.c
+1
@@ -303,6 +303,7 @@ static int git_sequencer_config(const char *k, const char *v,
303 }
304
305 if (!strcmp(k, "commit.gpgsign")) {
306 + free(opts->gpg_sign);
307 opts->gpg_sign = git_config_bool(k, v) ? xstrdup("") : NULL;
308 return 0;
309 }
t/t3404-rebase-interactive.sh
+1
@@ -26,6 +26,7 @@ Initial setup:
26 touch file "conflict".
27 '
28
29 +TEST_PASSES_SANITIZE_LEAK=true
30 . ./test-lib.sh
31
32 . "$TEST_DIRECTORY"/lib-rebase.sh
t/t3435-rebase-gpg-sign.sh
+1
@@ -8,6 +8,7 @@ test_description='test rebase --[no-]gpg-sign'
8 GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main
9 export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
10
11 +TEST_PASSES_SANITIZE_LEAK=true
12 . ./test-lib.sh
13 . "$TEST_DIRECTORY/lib-rebase.sh"
14 . "$TEST_DIRECTORY/lib-gpg.sh"
t/t7030-verify-tag.sh
+1
@@ -4,6 +4,7 @@ test_description='signed tag tests'
4 GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main
5 export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
6
7 +TEST_PASSES_SANITIZE_LEAK=true
8 . ./test-lib.sh
9 . "$TEST_DIRECTORY/lib-gpg.sh"
10