remote: fix leaking peer ref when expanding refmap

When expanding remote refs via the refspec in `get_expanded_map()`, we first copy the remote ref and then override its peer ref with the expanded name. This may cause a memory leak though in case the peer ref is already set, as this field is being copied by `copy_ref()`, as well. Fix the leak by freeing the peer ref before we re-assign the field. Signed-off-by: Patrick Steinhardt <ps@pks.im> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Patrick Steinhardt committed Aug 22, 2024 at 11:18 UTC 8960819e73d266a114f731a5a9a5dd90e5ab38bc
5 files changed +6
remote.c
+2
@@ -2081,6 +2081,8 @@ static struct ref *get_expanded_map(const struct ref *remote_refs,
2081 !ignore_symref_update(expn_name, &scratch)) {
2082 struct ref *cpy = copy_ref(ref);
2083
2084 + if (cpy->peer_ref)
2085 + free_one_ref(cpy->peer_ref);
2086 cpy->peer_ref = alloc_ref(expn_name);
2087 if (refspec->force)
2088 cpy->peer_ref->force = 1;
t/t5536-fetch-conflicts.sh
+1
@@ -2,6 +2,7 @@
2
3 test_description='fetch handles conflicting refspecs correctly'
4
5 +TEST_PASSES_SANITIZE_LEAK=true
6 . ./test-lib.sh
7
8 D=$(pwd)
t/t5553-set-upstream.sh
+1
@@ -4,6 +4,7 @@ test_description='"git fetch/pull --set-upstream" basic tests.'
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 check_config () {
t/t5703-upload-pack-ref-in-want.sh
+1
@@ -2,6 +2,7 @@
2
3 test_description='upload-pack ref-in-want'
4
5 +TEST_PASSES_SANITIZE_LEAK=true
6 . ./test-lib.sh
7
8 get_actual_refs () {
t/t6050-replace.sh
+1
@@ -7,6 +7,7 @@ test_description='Tests replace refs functionality'
7 GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main
8 export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
9
10 +TEST_PASSES_SANITIZE_LEAK=true
11 . ./test-lib.sh
12 . "$TEST_DIRECTORY/lib-gpg.sh"
13