Skip to content

Commit fc68633

Browse files
pks-tgitster
authored andcommitted
builtin/remote: fix various trivial memory leaks
There are multiple trivial memory leaks in git-remote(1). Fix those. Signed-off-by: Patrick Steinhardt <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent e06c1d1 commit fc68633

File tree

7 files changed

+38
-7
lines changed

7 files changed

+38
-7
lines changed

builtin/remote.c

Lines changed: 32 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -555,13 +555,16 @@ static int add_branch_for_removal(const char *refname,
555555
refspec.dst = (char *)refname;
556556
if (remote_find_tracking(branches->remote, &refspec))
557557
return 0;
558+
free(refspec.src);
558559

559560
/* don't delete a branch if another remote also uses it */
560561
for (kr = branches->keep->list; kr; kr = kr->next) {
561562
memset(&refspec, 0, sizeof(refspec));
562563
refspec.dst = (char *)refname;
563-
if (!remote_find_tracking(kr->remote, &refspec))
564+
if (!remote_find_tracking(kr->remote, &refspec)) {
565+
free(refspec.src);
564566
return 0;
567+
}
565568
}
566569

567570
/* don't delete non-remote-tracking refs */
@@ -668,7 +671,11 @@ static int config_read_push_default(const char *key, const char *value,
668671
static void handle_push_default(const char* old_name, const char* new_name)
669672
{
670673
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+
};
672679
git_config(config_read_push_default, &push_default);
673680
if (push_default.scope >= CONFIG_SCOPE_COMMAND)
674681
; /* pass */
@@ -688,6 +695,8 @@ static void handle_push_default(const char* old_name, const char* new_name)
688695
push_default.origin.buf, push_default.linenr,
689696
old_name);
690697
}
698+
699+
strbuf_release(&push_default.origin);
691700
}
692701

693702

@@ -785,7 +794,7 @@ static int mv(int argc, const char **argv, const char *prefix)
785794
}
786795

787796
if (!refspec_updated)
788-
return 0;
797+
goto out;
789798

790799
/*
791800
* First remove symrefs, then rename the rest, finally create
@@ -851,10 +860,15 @@ static int mv(int argc, const char **argv, const char *prefix)
851860
display_progress(progress, ++refs_renamed_nr);
852861
}
853862
stop_progress(&progress);
854-
string_list_clear(&remote_branches, 1);
855863

856864
handle_push_default(rename.old_name, rename.new_name);
857865

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);
858872
return 0;
859873
}
860874

@@ -945,12 +959,21 @@ static int rm(int argc, const char **argv, const char *prefix)
945959

946960
if (!result) {
947961
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+
}
950966

951967
handle_push_default(remote->name, NULL);
952968
}
953969

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);
954977
return result;
955978
}
956979

@@ -983,8 +1006,10 @@ static int append_ref_to_tracked_list(const char *refname,
9831006

9841007
memset(&refspec, 0, sizeof(refspec));
9851008
refspec.dst = (char *)refname;
986-
if (!remote_find_tracking(states->remote, &refspec))
1009+
if (!remote_find_tracking(states->remote, &refspec)) {
9871010
string_list_append(&states->tracked, abbrev_branch(refspec.src));
1011+
free(refspec.src);
1012+
}
9881013

9891014
return 0;
9901015
}

t/t5512-ls-remote.sh

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ test_description='git ls-remote'
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
generate_references () {

t/t5514-fetch-multiple.sh

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ test_description='fetch --all works correctly'
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
setup_repository () {

t/t5520-pull.sh

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ test_description='pulling into void'
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
modify () {

t/t5528-push-default.sh

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ test_description='check various push.default settings'
44
GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main
55
export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
66

7+
TEST_PASSES_SANITIZE_LEAK=true
78
. ./test-lib.sh
89

910
test_expect_success 'setup bare remotes' '

t/t5543-atomic-push.sh

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ test_description='pushing to a repository using the atomic push option'
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
mk_repo_pair () {

t/t5570-git-daemon.sh

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ test_description='test fetching over git protocol'
44
GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main
55
export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
66

7+
TEST_PASSES_SANITIZE_LEAK=true
78
. ./test-lib.sh
89

910
. "$TEST_DIRECTORY"/lib-git-daemon.sh

0 commit comments

Comments
 (0)