Skip to content

Commit 4119fc0

Browse files
pks-tgitster
authored andcommitted
builtin/ls-remote: fix leaking pattern strings
Users can pass patterns to git-ls-remote(1), which allows them to filter the list of printed references. We assemble those patterns into an array and prefix them with "*/", but never free either the array nor the allocated strings. Refactor the code to use a `struct strvec` instead of manually tracking the strings in an array. Like this, we can easily use `strvec_clear()` to release both the vector and the contained string for us, plugging the leak. Helped-by: Taylor Blau <[email protected]> Signed-off-by: Patrick Steinhardt <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 6771e20 commit 4119fc0

File tree

2 files changed

+11
-14
lines changed

2 files changed

+11
-14
lines changed

builtin/ls-remote.c

Lines changed: 10 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -19,17 +19,16 @@ static const char * const ls_remote_usage[] = {
1919
* Is there one among the list of patterns that match the tail part
2020
* of the path?
2121
*/
22-
static int tail_match(const char **pattern, const char *path)
22+
static int tail_match(const struct strvec *pattern, const char *path)
2323
{
24-
const char *p;
2524
char *pathbuf;
2625

27-
if (!pattern)
26+
if (!pattern->nr)
2827
return 1; /* no restriction */
2928

3029
pathbuf = xstrfmt("/%s", path);
31-
while ((p = *(pattern++)) != NULL) {
32-
if (!wildmatch(p, pathbuf, 0)) {
30+
for (size_t i = 0; i < pattern->nr; i++) {
31+
if (!wildmatch(pattern->v[i], pathbuf, 0)) {
3332
free(pathbuf);
3433
return 1;
3534
}
@@ -47,7 +46,7 @@ int cmd_ls_remote(int argc, const char **argv, const char *prefix)
4746
int status = 0;
4847
int show_symref_target = 0;
4948
const char *uploadpack = NULL;
50-
const char **pattern = NULL;
49+
struct strvec pattern = STRVEC_INIT;
5150
struct transport_ls_refs_options transport_options =
5251
TRANSPORT_LS_REFS_OPTIONS_INIT;
5352
int i;
@@ -93,13 +92,8 @@ int cmd_ls_remote(int argc, const char **argv, const char *prefix)
9392

9493
packet_trace_identity("ls-remote");
9594

96-
if (argc > 1) {
97-
int i;
98-
CALLOC_ARRAY(pattern, argc);
99-
for (i = 1; i < argc; i++) {
100-
pattern[i - 1] = xstrfmt("*/%s", argv[i]);
101-
}
102-
}
95+
for (int i = 1; i < argc; i++)
96+
strvec_pushf(&pattern, "*/%s", argv[i]);
10397

10498
if (flags & REF_TAGS)
10599
strvec_push(&transport_options.ref_prefixes, "refs/tags/");
@@ -136,7 +130,7 @@ int cmd_ls_remote(int argc, const char **argv, const char *prefix)
136130
struct ref_array_item *item;
137131
if (!check_ref_type(ref, flags))
138132
continue;
139-
if (!tail_match(pattern, ref->name))
133+
if (!tail_match(&pattern, ref->name))
140134
continue;
141135
item = ref_array_push(&ref_array, ref->name, &ref->old_oid);
142136
item->symref = xstrdup_or_null(ref->symref);
@@ -158,5 +152,7 @@ int cmd_ls_remote(int argc, const char **argv, const char *prefix)
158152
if (transport_disconnect(transport))
159153
status = 1;
160154
transport_ls_refs_options_release(&transport_options);
155+
156+
strvec_clear(&pattern);
161157
return status;
162158
}

t/t5535-fetch-push-symref.sh

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
test_description='avoiding conflicting update through symref aliasing'
44

5+
TEST_PASSES_SANITIZE_LEAK=true
56
. ./test-lib.sh
67

78
test_expect_success 'setup' '

0 commit comments

Comments
 (0)