builtin/remote: fix various trivial memory leaks

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>

Patrick Steinhardt committed Aug 1, 2024 at 12:40 UTC fc68633352180fc2645772b5926898fee4e006e3
7 files changed +38 -7
builtin/remote.c
+32 -7
@@ -555,13 +555,16 @@ static int add_branch_for_removal(const char *refname,
555 refspec.dst = (char *)refname;
556 if (remote_find_tracking(branches->remote, &refspec))
557 return 0;
558 + free(refspec.src);
559
560 /* don't delete a branch if another remote also uses it */
561 for (kr = branches->keep->list; kr; kr = kr->next) {
562 memset(&refspec, 0, sizeof(refspec));
563 refspec.dst = (char *)refname;
563 - if (!remote_find_tracking(kr->remote, &refspec))
564 + if (!remote_find_tracking(kr->remote, &refspec)) {
565 + free(refspec.src);
566 return 0;
567 + }
568 }
569
570 /* don't delete non-remote-tracking refs */
@@ -668,7 +671,11 @@ static int config_read_push_default(const char *key, const char *value,
671 static void handle_push_default(const char* old_name, const char* new_name)
672 {
673 struct push_default_info push_default = {
671 - old_name, CONFIG_SCOPE_UNKNOWN, STRBUF_INIT, -1 };
674 + .old_name = old_name,
675 + .scope = CONFIG_SCOPE_UNKNOWN,
676 + .origin = STRBUF_INIT,
677 + .linenr = -1,
678 + };
679 git_config(config_read_push_default, &push_default);
680 if (push_default.scope >= CONFIG_SCOPE_COMMAND)
681 ; /* pass */
@@ -688,6 +695,8 @@ static void handle_push_default(const char* old_name, const char* new_name)
695 push_default.origin.buf, push_default.linenr,
696 old_name);
697 }
698 +
699 + strbuf_release(&push_default.origin);
700 }
701
702
@@ -785,7 +794,7 @@ static int mv(int argc, const char **argv, const char *prefix)
794 }
795
796 if (!refspec_updated)
788 - return 0;
797 + goto out;
798
799 /*
800 * First remove symrefs, then rename the rest, finally create
@@ -851,10 +860,15 @@ static int mv(int argc, const char **argv, const char *prefix)
860 display_progress(progress, ++refs_renamed_nr);
861 }
862 stop_progress(&progress);
854 - string_list_clear(&remote_branches, 1);
863
864 handle_push_default(rename.old_name, rename.new_name);
865
866 +out:
867 + string_list_clear(&remote_branches, 1);
868 + strbuf_release(&old_remote_context);
869 + strbuf_release(&buf);
870 + strbuf_release(&buf2);
871 + strbuf_release(&buf3);
872 return 0;
873 }
874
@@ -945,12 +959,21 @@ static int rm(int argc, const char **argv, const char *prefix)
959
960 if (!result) {
961 strbuf_addf(&buf, "remote.%s", remote->name);
948 - if (git_config_rename_section(buf.buf, NULL) < 1)
949 - return error(_("Could not remove config section '%s'"), buf.buf);
962 + if (git_config_rename_section(buf.buf, NULL) < 1) {
963 + result = error(_("Could not remove config section '%s'"), buf.buf);
964 + goto out;
965 + }
966
967 handle_push_default(remote->name, NULL);
968 }
969
970 +out:
971 + for (struct known_remote *r = known_remotes.list; r;) {
972 + struct known_remote *next = r->next;
973 + free(r);
974 + r = next;
975 + }
976 + strbuf_release(&buf);
977 return result;
978 }
979
@@ -983,8 +1006,10 @@ static int append_ref_to_tracked_list(const char *refname,
1006
1007 memset(&refspec, 0, sizeof(refspec));
1008 refspec.dst = (char *)refname;
986 - if (!remote_find_tracking(states->remote, &refspec))
1009 + if (!remote_find_tracking(states->remote, &refspec)) {
1010 string_list_append(&states->tracked, abbrev_branch(refspec.src));
1011 + free(refspec.src);
1012 + }
1013
1014 return 0;
1015 }
t/t5512-ls-remote.sh
+1
@@ -5,6 +5,7 @@ test_description='git ls-remote'
5 GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main
6 export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
7
8 +TEST_PASSES_SANITIZE_LEAK=true
9 . ./test-lib.sh
10
11 generate_references () {
t/t5514-fetch-multiple.sh
+1
@@ -5,6 +5,7 @@ test_description='fetch --all works correctly'
5 GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main
6 export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
7
8 +TEST_PASSES_SANITIZE_LEAK=true
9 . ./test-lib.sh
10
11 setup_repository () {
t/t5520-pull.sh
+1
@@ -5,6 +5,7 @@ test_description='pulling into void'
5 GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main
6 export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
7
8 +TEST_PASSES_SANITIZE_LEAK=true
9 . ./test-lib.sh
10
11 modify () {
t/t5528-push-default.sh
+1
@@ -4,6 +4,7 @@ test_description='check various push.default settings'
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
10 test_expect_success 'setup bare remotes' '
t/t5543-atomic-push.sh
+1
@@ -5,6 +5,7 @@ test_description='pushing to a repository using the atomic push option'
5 GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main
6 export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
7
8 +TEST_PASSES_SANITIZE_LEAK=true
9 . ./test-lib.sh
10
11 mk_repo_pair () {
t/t5570-git-daemon.sh
+1
@@ -4,6 +4,7 @@ test_description='test fetching over git protocol'
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
10 . "$TEST_DIRECTORY"/lib-git-daemon.sh