upload-pack: fix leaking child process data on reachability checks
We spawn a git-rev-list(1) command to perform reachability checks in "upload-pack.c". We do not release memory associated with the process in error cases though, thus leaking memory. Fix these by calling `child_process_clear()`. Signed-off-by: Patrick Steinhardt <ps@pks.im> Signed-off-by: Junio C Hamano <gitster@pobox.com>
Patrick Steinhardt committed
Sep 5, 2024 at 12:08 UTC
ac2e7d545efdc4ceeef8c1191bb276e86d793f29
2 files changed
+17
-6
t/t5516-fetch-push.sh
+1
@@ -19,6 +19,7 @@ GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main
19
export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
20
21
TEST_CREATE_REPO_NO_TEMPLATE=1
22
+TEST_PASSES_SANITIZE_LEAK=true
23
. ./test-lib.sh
24
25
D=$(pwd)
upload-pack.c
+16
-6
@@ -709,10 +709,13 @@ static int get_reachable_list(struct upload_pack_data *data,
709
struct object *o;
710
char namebuf[GIT_MAX_HEXSZ + 2]; /* ^ + hash + LF */
711
const unsigned hexsz = the_hash_algo->hexsz;
712
+ int ret;
713
714
if (do_reachable_revlist(&cmd, &data->shallows, reachable,
714
- data->allow_uor) < 0)
715
- return -1;
715
+ data->allow_uor) < 0) {
716
+ ret = -1;
717
+ goto out;
718
+ }
719
720
while ((i = read_in_full(cmd.out, namebuf, hexsz + 1)) == hexsz + 1) {
721
struct object_id oid;
@@ -736,10 +739,16 @@ static int get_reachable_list(struct upload_pack_data *data,
739
}
740
close(cmd.out);
741
739
- if (finish_command(&cmd))
740
- return -1;
742
+ if (finish_command(&cmd)) {
743
+ ret = -1;
744
+ goto out;
745
+ }
746
742
- return 0;
747
+ ret = 0;
748
+
749
+out:
750
+ child_process_clear(&cmd);
751
+ return ret;
752
}
753
754
static int has_unreachable(struct object_array *src, enum allow_uor allow_uor)
@@ -749,7 +758,7 @@ static int has_unreachable(struct object_array *src, enum allow_uor allow_uor)
758
int i;
759
760
if (do_reachable_revlist(&cmd, src, NULL, allow_uor) < 0)
752
- return 1;
761
+ goto error;
762
763
/*
764
* The commits out of the rev-list are not ancestors of
@@ -775,6 +784,7 @@ static int has_unreachable(struct object_array *src, enum allow_uor allow_uor)
784
error:
785
if (cmd.out >= 0)
786
close(cmd.out);
787
+ child_process_clear(&cmd);
788
return 1;
789
}
790