From: Felipe Contreras <felipe.contreras@gmail.com>
To: "René Scharfe" <rene.scharfe@lsrfire.ath.cx>
Cc: git@vger.kernel.org, "Junio C Hamano" <gitster@pobox.com>,
"Nguyễn Thái Ngọc Duy" <pclouds@gmail.com>,
"Adam Spiers" <git@adamspiers.org>,
"Ramkumar Ramachandra" <artagnon@gmail.com>
Subject: Re: [PATCH v3 2/2] read-cache: plug a few leaks
Date: Sat, 8 Jun 2013 07:15:30 -0500 [thread overview]
Message-ID: <CAMP44s2Bp5p1211e6Utdch4B+v3J83GCY0_ucG7duakswkb+pg@mail.gmail.com> (raw)
In-Reply-To: <51B31651.6020307@lsrfire.ath.cx>
On Sat, Jun 8, 2013 at 6:32 AM, René Scharfe
<rene.scharfe@lsrfire.ath.cx> wrote:
> Am 08.06.2013 00:29, schrieb Felipe Contreras:
>
>> We are not freeing 'istate->cache' properly.
>>
>> We can't rely on 'initialized' to keep track of the 'istate->cache',
>> because it doesn't really mean it's initialized. So assume it always has
>> data, and free it before overwriting it.
>
>
> That sounds a bit backwards to me. If ->initialized doesn't mean that the
> index state is initialized then something is fishy. Would it make sense and
> be sufficient to set ->initialized in add_index_entry?
I don't know.
> Or to get rid of it and check for ->cache_alloc instead?
That might make sense. I was thinking on renaming 'initialized' to
'loaded', but I really don't care.
>> Signed-off-by: Felipe Contreras <felipe.contreras@gmail.com>
>> ---
>> read-cache.c | 4 ++++
>> 1 file changed, 4 insertions(+)
>>
>> diff --git a/read-cache.c b/read-cache.c
>> index 5e30746..a1dd04d 100644
>> --- a/read-cache.c
>> +++ b/read-cache.c
>> @@ -1451,6 +1451,7 @@ int read_index_from(struct index_state *istate,
>> const char *path)
>> istate->version = ntohl(hdr->hdr_version);
>> istate->cache_nr = ntohl(hdr->hdr_entries);
>> istate->cache_alloc = alloc_nr(istate->cache_nr);
>> + free(istate->cache);
>> istate->cache = xcalloc(istate->cache_alloc,
>> sizeof(*istate->cache));
>> istate->initialized = 1;
>
>
> You wrote earlier that this change is safe with current callers and that it
> prevents leaks with the following sequence:
>
> discard_cache();
> # add entries
> read_cache();
>
> Do we currently have such a call sequence somewhere?
I don't know.
> Wouldn't that be a
> bug, namely forgetting to call discard_cache before read_cache?
Why would it be a bug? There's nothing in the API that hints there's a
problem with that.
> I've added a "assert(istate->cache_nr == 0);" a few lines above and the test
> suite still passed. With the hunk below, ->cache is also always NULL and
> cache_alloc is always 0 at that point. So we don't need that free call
> there in the cases covered by the test suite at least -- better leave it
> out.
Why leave it out? If somebody makes the mistake of doing the above
sequence, would you prefer that we leak?
--
Felipe Contreras
next prev parent reply other threads:[~2013-06-08 12:15 UTC|newest]
Thread overview: 16+ messages / expand[flat|nested] mbox.gz Atom feed top
2013-06-07 22:29 [PATCH v3 0/2] cherry-pick: fix memory leaks Felipe Contreras
2013-06-07 22:29 ` [PATCH v3 1/2] unpack-trees: plug a memory leak Felipe Contreras
2013-06-07 22:29 ` [PATCH v3 2/2] read-cache: plug a few leaks Felipe Contreras
2013-06-08 11:32 ` René Scharfe
2013-06-08 12:15 ` Felipe Contreras [this message]
2013-06-08 13:22 ` René Scharfe
2013-06-08 14:04 ` Felipe Contreras
2013-06-08 15:56 ` René Scharfe
2013-06-08 16:53 ` Felipe Contreras
2013-06-08 17:22 ` René Scharfe
2013-06-08 17:27 ` Felipe Contreras
2013-06-09 2:11 ` René Scharfe
2013-06-09 2:25 ` Felipe Contreras
2013-06-09 17:38 ` René Scharfe
2013-06-09 18:27 ` Felipe Contreras
2013-06-09 18:49 ` Junio C Hamano
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
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=CAMP44s2Bp5p1211e6Utdch4B+v3J83GCY0_ucG7duakswkb+pg@mail.gmail.com \
--to=felipe.contreras@gmail.com \
--cc=artagnon@gmail.com \
--cc=git@adamspiers.org \
--cc=git@vger.kernel.org \
--cc=gitster@pobox.com \
--cc=pclouds@gmail.com \
--cc=rene.scharfe@lsrfire.ath.cx \
/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.
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 NNTP newsgroup(s).