Re: [PATCH 1/1] cat-file: fix a memory leak in cat_one_file
- From
Eric Sunshine <sunshine@sunshineco.com>
- Date
- Nov 27, 2014, 06:18 UTC
- Message-ID
- <CAPig+cSnOc42msPYsBSv+PTgFsDyBqeL7yhSutuRQUkezpTdmw@mail.gmail.com>
- In-Reply-To
- <20141127002417.GT6527@google.com>
On Wed, Nov 26, 2014 at 7:24 PM, Jonathan Nieder <jrnieder@gmail.com> wrote:
Show 15 quoted lines
> Alexander Kuleshov wrote:
> [...]
>> +++ b/builtin/cat-file.c
>> @@ -68,9 +69,14 @@ static int cat_one_file(int opt, const char
>> + buf_must_free = 1;
>> +
>> - if (!buf)
>> + if (!buf) {
>> + free(buf);
>> die("Cannot read object %s", obj_name);
>
> Is this free() needed? The program die()s right afterward, which
> would free all memory automatically as part of exiting. Tools like
> valgrind would understand that nothing is wrong since buf is still
> reachable through the stack.Moreover, 'buf' can only be NULL inside the conditional, so the free() is pointless.