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

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Anthony Delannoy <anthony.2lannoy@xxxxxxxxx> 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);



[Index of Archives]     [Linux Kernel Development]     [Gcc Help]     [IETF Annouce]     [DCCP]     [Netdev]     [Networking]     [Security]     [V4L]     [Bugtraq]     [Yosemite]     [MIPS Linux]     [ARM Linux]     [Linux Security]     [Linux RAID]     [Linux SCSI]     [Fedora Users]

  Powered by Linux