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>
This commit is contained in:
Patrick Steinhardt 2024-08-01 12:40:12 +02:00 committed by Junio C Hamano
parent e06c1d1640
commit fc68633352
7 changed files with 38 additions and 7 deletions

View 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;
}

View file

@ -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 () {

View file

@ -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 () {

View file

@ -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 () {

View file

@ -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' '

View file

@ -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 () {

View file

@ -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