]> git.ipfire.org Git - thirdparty/git.git/commitdiff
builtin/commit: fix leaking cleanup config
authorPatrick Steinhardt <ps@pks.im>
Tue, 5 Nov 2024 06:17:20 +0000 (07:17 +0100)
committerJunio C Hamano <gitster@pobox.com>
Tue, 5 Nov 2024 06:37:54 +0000 (22:37 -0800)
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 <ps@pks.im>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
builtin/commit.c
t/t7502-commit-porcelain.sh

index 18a55bd1b9189ab48e95fab159a5bbc80d816e03..71d674138c9a2b4e0967a95989f02363aa37beba 100644 (file)
@@ -135,7 +135,7 @@ static struct strvec trailer_args = STRVEC_INIT;
  * is specified explicitly.
  */
 static enum commit_msg_cleanup_mode cleanup_mode;
-static char *cleanup_arg;
+static char *cleanup_config;
 
 static enum commit_whence whence;
 static int use_editor = 1, include_status = 1;
@@ -1387,8 +1387,6 @@ static int parse_and_validate_options(int argc, const char *argv[],
        if (0 <= edit_flag)
                use_editor = edit_flag;
 
-       cleanup_mode = get_cleanup_mode(cleanup_arg, use_editor);
-
        handle_untracked_files_arg(s);
 
        if (all && argc > 0)
@@ -1636,8 +1634,10 @@ static int git_commit_config(const char *k, const char *v,
                include_status = git_config_bool(k, v);
                return 0;
        }
-       if (!strcmp(k, "commit.cleanup"))
-               return git_config_string(&cleanup_arg, k, v);
+       if (!strcmp(k, "commit.cleanup")) {
+               FREE_AND_NULL(cleanup_config);
+               return git_config_string(&cleanup_config, k, v);
+       }
        if (!strcmp(k, "commit.gpgsign")) {
                sign_commit = git_config_bool(k, v) ? "" : NULL;
                return 0;
@@ -1658,6 +1658,7 @@ int cmd_commit(int argc,
               struct repository *repo UNUSED)
 {
        static struct wt_status s;
+       static const char *cleanup_arg = NULL;
        static struct option builtin_commit_options[] = {
                OPT__QUIET(&quiet, N_("suppress summary after successful commit")),
                OPT__VERBOSE(&verbose, N_("show diff in commit message template")),
@@ -1757,6 +1758,12 @@ int cmd_commit(int argc,
        if (verbose == -1)
                verbose = (config_commit_verbose < 0) ? 0 : config_commit_verbose;
 
+       if (cleanup_arg) {
+               free(cleanup_config);
+               cleanup_config = xstrdup(cleanup_arg);
+       }
+       cleanup_mode = get_cleanup_mode(cleanup_config, use_editor);
+
        if (dry_run)
                return dry_run_commit(argv, prefix, current_head, &s);
        index_file = prepare_index(argv, prefix, current_head, 0);
index b37e2018a74a7b28725fa277a95cca516daf5cbe..84f1ff52b6738d3123de4d66f1d10d744bfd9e4b 100755 (executable)
@@ -5,6 +5,7 @@ test_description='git commit porcelain-ish'
 GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main
 export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
 
+TEST_PASSES_SANITIZE_LEAK=true
 . ./test-lib.sh
 
 commit_msg_is () {