Skip to content

Commit 6ef9f77

Browse files
pks-tgitster
authored andcommitted
builtin/commit: fix leaking cleanup config
The cleanup string set by the config is leaking when it is being overridden by an option. Fix this by tracking these via two separate variables such that we can free the old value. Signed-off-by: Patrick Steinhardt <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent ff31b7b commit 6ef9f77

File tree

2 files changed

+13
-5
lines changed

2 files changed

+13
-5
lines changed

builtin/commit.c

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ static struct strvec trailer_args = STRVEC_INIT;
135135
* is specified explicitly.
136136
*/
137137
static enum commit_msg_cleanup_mode cleanup_mode;
138-
static char *cleanup_arg;
138+
static char *cleanup_config;
139139

140140
static enum commit_whence whence;
141141
static int use_editor = 1, include_status = 1;
@@ -1387,8 +1387,6 @@ static int parse_and_validate_options(int argc, const char *argv[],
13871387
if (0 <= edit_flag)
13881388
use_editor = edit_flag;
13891389

1390-
cleanup_mode = get_cleanup_mode(cleanup_arg, use_editor);
1391-
13921390
handle_untracked_files_arg(s);
13931391

13941392
if (all && argc > 0)
@@ -1636,8 +1634,10 @@ static int git_commit_config(const char *k, const char *v,
16361634
include_status = git_config_bool(k, v);
16371635
return 0;
16381636
}
1639-
if (!strcmp(k, "commit.cleanup"))
1640-
return git_config_string(&cleanup_arg, k, v);
1637+
if (!strcmp(k, "commit.cleanup")) {
1638+
FREE_AND_NULL(cleanup_config);
1639+
return git_config_string(&cleanup_config, k, v);
1640+
}
16411641
if (!strcmp(k, "commit.gpgsign")) {
16421642
sign_commit = git_config_bool(k, v) ? "" : NULL;
16431643
return 0;
@@ -1658,6 +1658,7 @@ int cmd_commit(int argc,
16581658
struct repository *repo UNUSED)
16591659
{
16601660
static struct wt_status s;
1661+
static const char *cleanup_arg = NULL;
16611662
static struct option builtin_commit_options[] = {
16621663
OPT__QUIET(&quiet, N_("suppress summary after successful commit")),
16631664
OPT__VERBOSE(&verbose, N_("show diff in commit message template")),
@@ -1757,6 +1758,12 @@ int cmd_commit(int argc,
17571758
if (verbose == -1)
17581759
verbose = (config_commit_verbose < 0) ? 0 : config_commit_verbose;
17591760

1761+
if (cleanup_arg) {
1762+
free(cleanup_config);
1763+
cleanup_config = xstrdup(cleanup_arg);
1764+
}
1765+
cleanup_mode = get_cleanup_mode(cleanup_config, use_editor);
1766+
17601767
if (dry_run)
17611768
return dry_run_commit(argv, prefix, current_head, &s);
17621769
index_file = prepare_index(argv, prefix, current_head, 0);

t/t7502-commit-porcelain.sh

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ test_description='git commit porcelain-ish'
55
GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main
66
export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
77

8+
TEST_PASSES_SANITIZE_LEAK=true
89
. ./test-lib.sh
910

1011
commit_msg_is () {

0 commit comments

Comments
 (0)