Re: [PATCH 01/15] read-cache: fix memleak
- From
Stefan Beller <sbeller@google.com>
- Date
- Mar 23, 2015, 16:24 UTC
- Message-ID
- <CAGZ79kY=b3=ppmB+eJOAgcUec7hq+ycJiCvBzkTc7U78Mgog_g@mail.gmail.com>
- In-Reply-To
- <xmqq619vc9pl.fsf@gitster.dls.corp.google.com>
On Fri, Mar 20, 2015 at 8:26 PM, Junio C Hamano <gitster@pobox.com> wrote:
Show 38 quoted lines
> Stefan Beller <sbeller@google.com> writes:
>
>> `ce` is allocated in make_cache_entry and should be freed if it is not
>> used any more. refresh_cache_entry as a wrapper around refresh_cache_ent
>> will either return `ce` or a new updated cache entry which is allocated
>> to new memory. In that case we need to free `ce` ourselfs.
>>
>> Signed-off-by: Stefan Beller <sbeller@google.com>
>> ---
>> read-cache.c | 2 ++
>> 1 file changed, 2 insertions(+)
>>
>> diff --git a/read-cache.c b/read-cache.c
>> index 8d71860..f72ea9f 100644
>> --- a/read-cache.c
>> +++ b/read-cache.c
>> @@ -747,6 +747,8 @@ struct cache_entry *make_cache_entry(unsigned int mode,
>> free(ce);
>> return NULL;
>> } else {
>> + if (ret != ce)
>> + free(ce);
>> return ret;
>> }
>> }
>
> Good, I vaguely recall that we did something similar in another
> codepath that forgot the fact that refresh_cache_entry() may make
> the incoming ce unnecessary.
>
> As the rule is "if ret is different from ce, then ce must be freed"
> in this codepath, I wonder if this is easier to read:
>
> ret = refresh_cache_entry(ce, ...);
> if (ret != ce)
> free(ce);
> return ret;
>Indeed it is. Thanks for pointing out!
I'll send this as part of another series applying on top of sb/leaks soon.