]> git.ipfire.org Git - thirdparty/git.git/commitdiff
builtin/remote: fix various trivial memory leaks
authorPatrick Steinhardt <ps@pks.im>
Thu, 1 Aug 2024 10:40:12 +0000 (12:40 +0200)
committerJunio C Hamano <gitster@pobox.com>
Thu, 1 Aug 2024 15:47:36 +0000 (08:47 -0700)
There are multiple trivial memory leaks in git-remote(1). Fix those.

Signed-off-by: Patrick Steinhardt <ps@pks.im>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
builtin/remote.c
t/t5512-ls-remote.sh
t/t5514-fetch-multiple.sh
t/t5520-pull.sh
t/t5528-push-default.sh
t/t5543-atomic-push.sh
t/t5570-git-daemon.sh

index 303da7f73fc841c56fd48a46d7044c654410de02..9d54fddf8c33673aa0f4b1e693837cae90814a14 100644 (file)
@@ -555,13 +555,16 @@ static int add_branch_for_removal(const char *refname,
        refspec.dst = (char *)refname;
        if (remote_find_tracking(branches->remote, &refspec))
                return 0;
+       free(refspec.src);
 
        /* don't delete a branch if another remote also uses it */
        for (kr = branches->keep->list; kr; kr = kr->next) {
                memset(&refspec, 0, sizeof(refspec));
                refspec.dst = (char *)refname;
-               if (!remote_find_tracking(kr->remote, &refspec))
+               if (!remote_find_tracking(kr->remote, &refspec)) {
+                       free(refspec.src);
                        return 0;
+               }
        }
 
        /* don't delete non-remote-tracking refs */
@@ -668,7 +671,11 @@ static int config_read_push_default(const char *key, const char *value,
 static void handle_push_default(const char* old_name, const char* new_name)
 {
        struct push_default_info push_default = {
-               old_name, CONFIG_SCOPE_UNKNOWN, STRBUF_INIT, -1 };
+               .old_name = old_name,
+               .scope = CONFIG_SCOPE_UNKNOWN,
+               .origin = STRBUF_INIT,
+               .linenr = -1,
+       };
        git_config(config_read_push_default, &push_default);
        if (push_default.scope >= CONFIG_SCOPE_COMMAND)
                ; /* pass */
@@ -688,6 +695,8 @@ static void handle_push_default(const char* old_name, const char* new_name)
                        push_default.origin.buf, push_default.linenr,
                        old_name);
        }
+
+       strbuf_release(&push_default.origin);
 }
 
 
@@ -785,7 +794,7 @@ static int mv(int argc, const char **argv, const char *prefix)
        }
 
        if (!refspec_updated)
-               return 0;
+               goto out;
 
        /*
         * First remove symrefs, then rename the rest, finally create
@@ -851,10 +860,15 @@ static int mv(int argc, const char **argv, const char *prefix)
                display_progress(progress, ++refs_renamed_nr);
        }
        stop_progress(&progress);
-       string_list_clear(&remote_branches, 1);
 
        handle_push_default(rename.old_name, rename.new_name);
 
+out:
+       string_list_clear(&remote_branches, 1);
+       strbuf_release(&old_remote_context);
+       strbuf_release(&buf);
+       strbuf_release(&buf2);
+       strbuf_release(&buf3);
        return 0;
 }
 
@@ -945,12 +959,21 @@ static int rm(int argc, const char **argv, const char *prefix)
 
        if (!result) {
                strbuf_addf(&buf, "remote.%s", remote->name);
-               if (git_config_rename_section(buf.buf, NULL) < 1)
-                       return error(_("Could not remove config section '%s'"), buf.buf);
+               if (git_config_rename_section(buf.buf, NULL) < 1) {
+                       result = error(_("Could not remove config section '%s'"), buf.buf);
+                       goto out;
+               }
 
                handle_push_default(remote->name, NULL);
        }
 
+out:
+       for (struct known_remote *r = known_remotes.list; r;) {
+               struct known_remote *next = r->next;
+               free(r);
+               r = next;
+       }
+       strbuf_release(&buf);
        return result;
 }
 
@@ -983,8 +1006,10 @@ static int append_ref_to_tracked_list(const char *refname,
 
        memset(&refspec, 0, sizeof(refspec));
        refspec.dst = (char *)refname;
-       if (!remote_find_tracking(states->remote, &refspec))
+       if (!remote_find_tracking(states->remote, &refspec)) {
                string_list_append(&states->tracked, abbrev_branch(refspec.src));
+               free(refspec.src);
+       }
 
        return 0;
 }
index 42e77eb5a98adc853daebda5c86ea576e906660d..d687d824d121f132dddd5a2917395113502dea1b 100755 (executable)
@@ -5,6 +5,7 @@ test_description='git ls-remote'
 GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main
 export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
 
+TEST_PASSES_SANITIZE_LEAK=true
 . ./test-lib.sh
 
 generate_references () {
index 25772c85c5a9a89bf247cfa41cf9d8b127572cd8..579872c258db40acd35acd9db027eeb123702e00 100755 (executable)
@@ -5,6 +5,7 @@ test_description='fetch --all works correctly'
 GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main
 export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
 
+TEST_PASSES_SANITIZE_LEAK=true
 . ./test-lib.sh
 
 setup_repository () {
index 47534f1062d203a9b823d545740004e2002162ff..1098cbd0a19218f58296960293b88a375a4cb4eb 100755 (executable)
@@ -5,6 +5,7 @@ test_description='pulling into void'
 GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main
 export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
 
+TEST_PASSES_SANITIZE_LEAK=true
 . ./test-lib.sh
 
 modify () {
index 14f7eced9a0b43bf8a565e3817f2ef802389a3ba..bc2bada34c62984c37d73e94b2b76f6959ca2c75 100755 (executable)
@@ -4,6 +4,7 @@ test_description='check various push.default settings'
 GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main
 export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
 
+TEST_PASSES_SANITIZE_LEAK=true
 . ./test-lib.sh
 
 test_expect_success 'setup bare remotes' '
index 04b47ad84a86ab3407ba26f069d934ed7dcb5dec..479d103469527e6b923877cd480825b59e7094d4 100755 (executable)
@@ -5,6 +5,7 @@ test_description='pushing to a repository using the atomic push option'
 GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main
 export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
 
+TEST_PASSES_SANITIZE_LEAK=true
 . ./test-lib.sh
 
 mk_repo_pair () {
index f9a9bf950328e805ffb27c743b8d85f61510eec8..c5f08b67996b0dd7057b0b12a0d006839038ab4e 100755 (executable)
@@ -4,6 +4,7 @@ test_description='test fetching over git protocol'
 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-git-daemon.sh