git/list[1] front-page[2] threads[3] people[4] search[5] about
 

Re: [PATCH 1/1] pathspec: fix memleak

From
Junio C Hamano <gitster@pobox.com>
Date
Aug 12, 2022, 17:41 UTC
Message-ID
<xmqqsfm1bbrf.fsf@gitster.g>
In-Reply-To
<20220812081744.456280-2-anthony.2lannoy@gmail.com>
Anthony Delannoy <anthony.2lannoy@gmail.com> writes:
Show 13 quoted lines
> diff --git a/preload-index.c b/preload-index.c
> index e5529a5863..a05f4d1390 100644
> --- a/preload-index.c
> +++ b/preload-index.c
> @@ -148,6 +148,9 @@ void preload_index(struct index_state *index,
>  		if (pthread_join(p->pthread, NULL))
>  			die("unable to join threaded lstat");
>  		t2_sum_lstat += p->t2_nr_lstat;
> +
> +		if (pathspec)
> +			free(p->pathspec.items);
>  	}
>  	stop_progress(&pd.progress);

Given the way how copy_pathspec() makes a deep copy of a pathspec, I suspect that this is still leaking all the resources held by the array that is freed here. Let's take a look:

        void copy_pathspec(struct pathspec *dst, const struct pathspec *src)
        {
                int i, j;
                *dst = *src;
                ALLOC_ARRAY(dst->items, dst->nr);
                COPY_ARRAY(dst->items, src->items, dst->nr);

Here, we copy the array of "struct pathspec_item". But that is not enough because ...

                for (i = 0; i < dst->nr; i++) {
                        struct pathspec_item *d = &dst->items[i];
                        struct pathspec_item *s = &src->items[i];
                        d->match = xstrdup(s->match);
                        d->original = xstrdup(s->original);

... each "struct pathspec_item" instance has pointer members like these, and the copying of the array made these strings shared between the src and dst arrays. Here we make a copy of the string owned by the element in the src array and give the copy to the element in the dst array.

                        ALLOC_ARRAY(d->attr_match, d->attr_match_nr);
                        COPY_ARRAY(d->attr_match, s->attr_match, d->attr_match_nr);

Likewise for a separate array pointed by a member in "struct pathspec_item" ...

                        for (j = 0; j < d->attr_match_nr; j++) {
                                const char *value = s->attr_match[j].value;
                                d->attr_match[j].value = xstrdup_or_null(value);
... which has a pointer member here ...
                        }
                        d->attr_check = attr_check_dup(s->attr_check);
... and here.  Both are deep-copied.
                }
        }

There is pathspec.c::clear_pathspec() API function, which looks as if it was made for this exact use case.

I wonder if this is a good place to use it, perhaps like the attached patch.

 preload-index.c | 6 ++++++
 1 file changed, 6 insertions(+)
diff --git c/preload-index.c w/preload-index.c
index e5529a5863..100f7a374d 100644
--- c/preload-index.c
+++ w/preload-index.c
@@ -151,6 +151,12 @@ void preload_index(struct index_state *index,
 	}
 	stop_progress(&pd.progress);
 
+	if (pathspec) {
+		/* earlier we made deep copies for each thread to work with */
+		for (i = 0; i < threads; i++)
+			clear_pathspec(&data[i].pathspec);
+	}
+
 	trace_performance_leave("preload index");
 
 	trace2_data_intmax("index", NULL, "preload/sum_lstat", t2_sum_lstat);
Previous: Anthony Delannoy
Message 3 of 3 in “pathspec: fix memleak”
  1. 0/1 pathspec: fix memleakAnthony Delannoy, Aug 12, 2022
  2. 1/1 pathspec: fix memleakAnthony Delannoy, Aug 12, 2022
  3. Junio C HamanoAug 12, 2022

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.