Skip to content

Commit 77188b5

Browse files
KarthikNayakgitster
authored andcommitted
send-pack: fix memory leak around duplicate refs
The 'git-send-pack(1)' allows users to push objects to a remote repository and explicitly list the references to be pushed. The status of each reference pushed is captured into a list mapped by refname. If a reference fails to be updated, its error message is captured in the `ref->remote_status` field. While the command allows duplicate ref inputs, the list doesn't accommodate this behavior as a particular refname is linked to a single `struct ref*` element. So if the user inputs a reference twice like: git send-pack remote.git A:foo B:foo where the user is trying to update the same reference 'foo' twice and the reference fails to be updated, we first fill `ref->remote_status` with error message for the input 'A:foo' then we override the same field with the error message for 'B:foo'. This override happens without first free'ing the previous value. Fix this leak. The current tests already incorporate the above example, but in the test 'A:foo' succeeds while 'B:foo' fails, meaning that the memory leak isn't triggered. Add a new test with multiple duplicates. Signed-off-by: Karthik Nayak <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 0e358de commit 77188b5

File tree

2 files changed

+13
-0
lines changed

2 files changed

+13
-0
lines changed

send-pack.c

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -257,6 +257,13 @@ static int receive_status(struct repository *r,
257257
refname);
258258
continue;
259259
}
260+
261+
/*
262+
* Clients sending duplicate refs can cause the same value
263+
* to be overridden, causing a memory leak.
264+
*/
265+
free(hint->remote_status);
266+
260267
if (!strcmp(head, "ng")) {
261268
hint->status = REF_STATUS_REMOTE_REJECT;
262269
if (p)

t/t5408-send-pack-stdin.sh

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,12 @@ test_expect_success 'cmdline refs written in order' '
7373
verify_push A foo
7474
'
7575

76+
test_expect_success 'cmdline refs with multiple duplicates' '
77+
clear_remote &&
78+
test_must_fail git send-pack remote.git A:foo B:foo C:foo &&
79+
verify_push A foo
80+
'
81+
7682
test_expect_success '--stdin refs come after cmdline' '
7783
clear_remote &&
7884
echo A:foo >input &&

0 commit comments

Comments
 (0)