git@vger.kernel.org mailing list mirror (one of many)
 help / color / mirror / code / Atom feed
From: Junio C Hamano <gitster@pobox.com>
To: Anthony Delannoy <anthony.2lannoy@gmail.com>
Cc: git@vger.kernel.org
Subject: Re: [PATCH 1/1] pathspec: fix memleak
Date: Fri, 12 Aug 2022 10:41:08 -0700	[thread overview]
Message-ID: <xmqqsfm1bbrf.fsf@gitster.g> (raw)
In-Reply-To: <20220812081744.456280-2-anthony.2lannoy@gmail.com> (Anthony Delannoy's message of "Fri, 12 Aug 2022 10:17:44 +0200")

Anthony Delannoy <anthony.2lannoy@gmail.com> writes:

> 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);

      reply	other threads:[~2022-08-12 17:41 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-08-12  8:17 [PATCH 0/1] pathspec: fix memleak Anthony Delannoy
2022-08-12  8:17 ` [PATCH 1/1] " Anthony Delannoy
2022-08-12 17:41   ` Junio C Hamano [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: http://vger.kernel.org/majordomo-info.html

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=xmqqsfm1bbrf.fsf@gitster.g \
    --to=gitster@pobox.com \
    --cc=anthony.2lannoy@gmail.com \
    --cc=git@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://80x24.org/mirrors/git.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).