]> git.ipfire.org Git - thirdparty/git.git/commitdiff
config: plug various memory leaks
authorPatrick Steinhardt <ps@pks.im>
Mon, 27 May 2024 11:46:44 +0000 (13:46 +0200)
committerJunio C Hamano <gitster@pobox.com>
Mon, 27 May 2024 18:20:00 +0000 (11:20 -0700)
Now that memory ownership rules around `git_config_string()` and
`git_config_pathname()` are clearer, it also got easier to spot that
the returned memory needs to be free'd. Plug a subset of those cases and
mark now-passing tests as leak free.

Signed-off-by: Patrick Steinhardt <ps@pks.im>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
13 files changed:
alias.c
config.c
t/t1306-xdg-files.sh
t/t1350-config-hooks-path.sh
t/t3415-rebase-autosquash.sh
t/t4041-diff-submodule-option.sh
t/t4060-diff-submodule-option-diff-format.sh
t/t4210-log-i18n.sh
t/t6006-rev-list-format.sh
t/t7005-editor.sh
t/t7102-reset.sh
t/t9129-git-svn-i18n-commitencoding.sh
t/t9139-git-svn-non-utf8-commitencoding.sh

diff --git a/alias.c b/alias.c
index 269892c356a5b03903e009b30db385eabf127baf..4daafd9bdae66ef9bd8cdf147699e4057cde89f7 100644 (file)
--- a/alias.c
+++ b/alias.c
@@ -21,9 +21,11 @@ static int config_alias_cb(const char *key, const char *value,
                return 0;
 
        if (data->alias) {
-               if (!strcasecmp(p, data->alias))
+               if (!strcasecmp(p, data->alias)) {
+                       FREE_AND_NULL(data->v);
                        return git_config_string(&data->v,
                                                 key, value);
+               }
        } else if (data->list) {
                string_list_append(data->list, p);
        }
index da52a7f8a1f7db51b731509a0a7a2d9d6b23c186..496cd1a61eaa0aec346caa1c01d1bfc56155472b 100644 (file)
--- a/config.c
+++ b/config.c
@@ -1414,8 +1414,10 @@ static int git_default_core_config(const char *var, const char *value,
                return 0;
        }
 
-       if (!strcmp(var, "core.attributesfile"))
+       if (!strcmp(var, "core.attributesfile")) {
+               FREE_AND_NULL(git_attributes_file);
                return git_config_pathname(&git_attributes_file, var, value);
+       }
 
        if (!strcmp(var, "core.hookspath")) {
                if (ctx->kvi && ctx->kvi->scope == CONFIG_SCOPE_LOCAL &&
@@ -1428,6 +1430,7 @@ static int git_default_core_config(const char *var, const char *value,
                              "again with "
                              "`GIT_CLONE_PROTECTION_ACTIVE=false`"),
                            value);
+               FREE_AND_NULL(git_hooks_path);
                return git_config_pathname(&git_hooks_path, var, value);
        }
 
@@ -1576,8 +1579,10 @@ static int git_default_core_config(const char *var, const char *value,
                return 0;
        }
 
-       if (!strcmp(var, "core.editor"))
+       if (!strcmp(var, "core.editor")) {
+               FREE_AND_NULL(editor_program);
                return git_config_string(&editor_program, var, value);
+       }
 
        if (!strcmp(var, "core.commentchar") ||
            !strcmp(var, "core.commentstring")) {
@@ -1595,11 +1600,13 @@ static int git_default_core_config(const char *var, const char *value,
                return 0;
        }
 
-       if (!strcmp(var, "core.askpass"))
+       if (!strcmp(var, "core.askpass")) {
+               FREE_AND_NULL(askpass_program);
                return git_config_string(&askpass_program, var, value);
+       }
 
        if (!strcmp(var, "core.excludesfile")) {
-               free(excludes_file);
+               FREE_AND_NULL(excludes_file);
                return git_config_pathname(&excludes_file, var, value);
        }
 
@@ -1702,11 +1709,15 @@ static int git_default_sparse_config(const char *var, const char *value)
 
 static int git_default_i18n_config(const char *var, const char *value)
 {
-       if (!strcmp(var, "i18n.commitencoding"))
+       if (!strcmp(var, "i18n.commitencoding")) {
+               FREE_AND_NULL(git_commit_encoding);
                return git_config_string(&git_commit_encoding, var, value);
+       }
 
-       if (!strcmp(var, "i18n.logoutputencoding"))
+       if (!strcmp(var, "i18n.logoutputencoding")) {
+               FREE_AND_NULL(git_log_output_encoding);
                return git_config_string(&git_log_output_encoding, var, value);
+       }
 
        /* Add other config variables here and to Documentation/config.txt. */
        return 0;
@@ -1779,10 +1790,15 @@ static int git_default_push_config(const char *var, const char *value)
 
 static int git_default_mailmap_config(const char *var, const char *value)
 {
-       if (!strcmp(var, "mailmap.file"))
+       if (!strcmp(var, "mailmap.file")) {
+               FREE_AND_NULL(git_mailmap_file);
                return git_config_pathname(&git_mailmap_file, var, value);
-       if (!strcmp(var, "mailmap.blob"))
+       }
+
+       if (!strcmp(var, "mailmap.blob")) {
+               FREE_AND_NULL(git_mailmap_blob);
                return git_config_string(&git_mailmap_blob, var, value);
+       }
 
        /* Add other config variables here and to Documentation/config.txt. */
        return 0;
@@ -1790,8 +1806,10 @@ static int git_default_mailmap_config(const char *var, const char *value)
 
 static int git_default_attr_config(const char *var, const char *value)
 {
-       if (!strcmp(var, "attr.tree"))
+       if (!strcmp(var, "attr.tree")) {
+               FREE_AND_NULL(git_attr_tree);
                return git_config_string(&git_attr_tree, var, value);
+       }
 
        /*
         * Add other attribute related config variables here and to
index 40d3c42618c04f8b0e656af83fb2e303601dd7ff..53e5b290b9bcab0d8809e34f4343f191af4ed9dc 100755 (executable)
@@ -7,6 +7,7 @@
 
 test_description='Compatibility with $XDG_CONFIG_HOME/git/ files'
 
+TEST_PASSES_SANITIZE_LEAK=true
 . ./test-lib.sh
 
 test_expect_success 'read config: xdg file exists and ~/.gitconfig doesn'\''t' '
index f6dc83e2aabf69ec51364ae9a5f6d5d50ce8db14..5c47f9ecc3d79bf696fae134185ecfb9f17342b4 100755 (executable)
@@ -2,6 +2,7 @@
 
 test_description='Test the core.hooksPath configuration variable'
 
+TEST_PASSES_SANITIZE_LEAK=true
 . ./test-lib.sh
 
 test_expect_success 'set up a pre-commit hook in core.hooksPath' '
index fcc40d6fe1fd5b2a9c4f2a5e4ede15e73c247790..22452ff84cb339936cf2efcb1b4147655943eeff 100755 (executable)
@@ -5,6 +5,7 @@ test_description='auto squash'
 GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main
 export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
 
+TEST_PASSES_SANITIZE_LEAK=true
 . ./test-lib.sh
 
 . "$TEST_DIRECTORY"/lib-rebase.sh
index 0c1502d4b05f114bbedc003fe8bfa818aaab4980..8fc40e75eb3c78d911cc48ca535abbd1c569cba9 100755 (executable)
@@ -12,6 +12,7 @@ This test tries to verify the sanity of the --submodule option of git diff.
 GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main
 export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
 
+TEST_PASSES_SANITIZE_LEAK=true
 . ./test-lib.sh
 
 # Tested non-UTF-8 encoding
index 97c6424cd51714eeadffd5899c709a409b1cedd9..8ce67442d96b2ce8e659a907e47982ebd7c2facf 100755 (executable)
@@ -10,6 +10,7 @@ test_description='Support for diff format verbose submodule difference in git di
 This test tries to verify the sanity of --submodule=diff option of git diff.
 '
 
+TEST_PASSES_SANITIZE_LEAK=true
 . ./test-lib.sh
 
 # Tested non-UTF-8 encoding
index 75216f19ce3a3e39e20b5d14406d9f1e8c34632b..7120030b5c650d91da5cf9ab9e3e0d6782936521 100755 (executable)
@@ -1,6 +1,8 @@
 #!/bin/sh
 
 test_description='test log with i18n features'
+
+TEST_PASSES_SANITIZE_LEAK=true
 . ./lib-gettext.sh
 
 # two forms of é
index 573eb97a0f7f06e45d3358e65be8b55816ea47b3..f1623b1c06d0fa177dd2ae05c688238b927b3b9f 100755 (executable)
@@ -8,6 +8,7 @@ test_description='git rev-list --pretty=format test'
 GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main
 export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
 
+TEST_PASSES_SANITIZE_LEAK=true
 . ./test-lib.sh
 . "$TEST_DIRECTORY"/lib-terminal.sh
 
index 5fcf281dfbf8d601fc934d890db79f7284b19402..b9822294fedb7cf3d16d253853d92d274657916d 100755 (executable)
@@ -2,6 +2,7 @@
 
 test_description='GIT_EDITOR, core.editor, and stuff'
 
+TEST_PASSES_SANITIZE_LEAK=true
 . ./test-lib.sh
 
 unset EDITOR VISUAL GIT_EDITOR
index 62d9f846ce86c54745eb87619d14fefc005d6fe3..2add26d76844deb04aee4568bc467f66df965d57 100755 (executable)
@@ -10,6 +10,7 @@ Documented tests for git reset'
 GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main
 export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
 
+TEST_PASSES_SANITIZE_LEAK=true
 . ./test-lib.sh
 
 commit_msg () {
index 185248a4cd766172969ad88f7dcd94258733f644..01e1e8a8f76765d081270d37a980778fad4189d0 100755 (executable)
@@ -4,7 +4,6 @@
 
 test_description='git svn honors i18n.commitEncoding in config'
 
-TEST_FAILS_SANITIZE_LEAK=true
 . ./lib-git-svn.sh
 
 compare_git_head_with () {
index b7f756b2b7f28f6e5d32fa595ebbf1ca2bebb45b..22d80b0be2b94515132a79401b719f98794f4616 100755 (executable)
@@ -4,7 +4,6 @@
 
 test_description='git svn refuses to dcommit non-UTF8 messages'
 
-TEST_FAILS_SANITIZE_LEAK=true
 . ./lib-git-svn.sh
 
 # ISO-2022-JP can pass for valid UTF-8, so skipping that in this test