git.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Stefan Beller <sbeller@google.com>
To: git@vger.kernel.org, karsten.blees@gmail.com
Cc: gitster@pobox.com, Stefan Beller <sbeller@google.com>
Subject: [PATCH] update-index: Don't copy memory around
Date: Mon, 23 Mar 2015 09:53:40 -0700	[thread overview]
Message-ID: <1427129620-13380-1-git-send-email-sbeller@google.com> (raw)
In-Reply-To: <xmqqwq2baui7.fsf@gitster.dls.corp.google.com>

Signed-off-by: Stefan Beller <sbeller@google.com>
---

On Fri, Mar 20, 2015 at 8:40 PM, Junio C Hamano <gitster@pobox.com> wrote:
> Stefan Beller <sbeller@google.com> writes:
>
>> `old` is not used outside the loop and would get lost
>> once we reach the goto.
>>
>> Signed-off-by: Stefan Beller <sbeller@google.com>
>> ---
>>  builtin/update-index.c | 1 +
>>  1 file changed, 1 insertion(+)
>>
>> diff --git a/builtin/update-index.c b/builtin/update-index.c
>> index 5878986..6271b54 100644
>> --- a/builtin/update-index.c
>> +++ b/builtin/update-index.c
>> @@ -584,6 +584,7 @@ static int do_reupdate(int ac, const char **av,
>>               path = xstrdup(ce->name);
>>               update_one(path);
>>               free(path);
>> +             free(old);
>
> The change looks trivially correct.
>
> A tangent; I wonder if we want to make path a strbuf that is
> declared in the function scope, reset (not released) at each
> iteration of the loop and released after the loop; keep allocating
> and freeing a small piece of string all the time feels somewhat
> wasteful.

> Also, we might want to add to the "Be careful" comment to describe
> why this cannot just be a call to "update_one(ce->name)" that does
> not use "path".

Indeed I am rather wondering why we need to pass in a copy to update_one
instead of ce->name. I was reading update_one and looking for why, but
eventually noticed update_one accepts a 'const char *', so it's not changed
in there. Digging down the into update_one also doesn't make it obvious to me.

I found (5699d17ee094, 2013-11-14, read-cache.c: fix memory leaks caused by
removed cache entries), which adds this duplication, though I do not understand
why. This passes the test suite, so I wonder if this patch would be a subtle bug
now.

 builtin/update-index.c | 5 +----
 1 file changed, 1 insertion(+), 4 deletions(-)

diff --git a/builtin/update-index.c b/builtin/update-index.c
index 6271b54..5857405 100644
--- a/builtin/update-index.c
+++ b/builtin/update-index.c
@@ -564,7 +564,6 @@ static int do_reupdate(int ac, const char **av,
 		const struct cache_entry *ce = active_cache[pos];
 		struct cache_entry *old = NULL;
 		int save_nr;
-		char *path;
 
 		if (ce_stage(ce) || !ce_path_match(ce, &pathspec, NULL))
 			continue;
@@ -581,9 +580,7 @@ static int do_reupdate(int ac, const char **av,
 		 * or worse yet 'allow_replace', active_nr may decrease.
 		 */
 		save_nr = active_nr;
-		path = xstrdup(ce->name);
-		update_one(path);
-		free(path);
+		update_one(ce->name);
 		free(old);
 		if (save_nr != active_nr)
 			goto redo;
-- 
2.3.0.81.gc37f363

  reply	other threads:[~2015-03-23 16:53 UTC|newest]

Thread overview: 44+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2015-03-21  0:27 [PATCH 00/15] Fixing memory leaks Stefan Beller
2015-03-21  0:27 ` [PATCH 01/15] read-cache: fix memleak Stefan Beller
2015-03-21  3:26   ` Junio C Hamano
2015-03-23 16:24     ` Stefan Beller
2015-03-23 17:57       ` [PATCH 1/2] " Stefan Beller
2015-03-23 17:57         ` [PATCH 2/2] read-cache.c: fix a memleak in add_to_index Stefan Beller
2015-03-23 18:07           ` Junio C Hamano
2015-03-23 18:11         ` [PATCH 1/2] read-cache: fix memleak Junio C Hamano
2015-03-21  0:27 ` [PATCH 02/15] read-cache: Improve readability Stefan Beller
2015-03-21  4:19   ` Junio C Hamano
2015-03-21  5:11     ` Stefan Beller
2015-03-22 19:26       ` Junio C Hamano
2015-03-21  0:28 ` [PATCH 03/15] read-cache: free cache entry in add_to_index in case of early return Stefan Beller
2015-03-21  3:31   ` Junio C Hamano
2015-03-21  5:10     ` Stefan Beller
2015-03-22 19:11       ` Junio C Hamano
2015-03-21  0:28 ` [PATCH 04/15] update-index: fix a memleak Stefan Beller
2015-03-21  3:40   ` Junio C Hamano
2015-03-23 16:53     ` Stefan Beller [this message]
2015-03-23 17:11       ` [PATCH] update-index: Don't copy memory around Junio C Hamano
2015-03-21  0:28 ` [PATCH 05/15] builtin/apply.c: fix a memleak Stefan Beller
2015-03-21  3:45   ` Junio C Hamano
2015-03-23 17:13     ` [PATCH] builtin/apply.c: fix a memleak (Fixup, squashable) Stefan Beller
2015-03-23 17:27       ` Junio C Hamano
2015-03-21  0:28 ` [PATCH 06/15] merge-blobs.c: Fix a memleak Stefan Beller
2015-03-21  0:28 ` [PATCH 07/15] merge-recursive: fix memleaks Stefan Beller
2015-03-21  3:48   ` Junio C Hamano
2015-03-21  0:28 ` [PATCH 08/15] http-push: Remove unneeded cleanup Stefan Beller
2015-03-21  0:28 ` [PATCH 09/15] http: release the memory of a http pack request as well Stefan Beller
2015-03-22 19:36   ` Junio C Hamano
2015-03-24 16:54     ` Stefan Beller
2015-03-24 17:48       ` Junio C Hamano
2015-03-21  0:28 ` [PATCH 10/15] commit.c: fix a memory leak Stefan Beller
2015-03-21  3:59   ` Junio C Hamano
2015-03-24 13:42     ` Duy Nguyen
2015-03-24 21:17       ` Re* " Junio C Hamano
2015-03-24 21:20         ` Stefan Beller
2015-03-21  0:28 ` [PATCH 11/15] builtin/check-attr: fix a memleak Stefan Beller
2015-03-21  4:02   ` Junio C Hamano
2015-03-21  0:28 ` [PATCH 12/15] builtin/merge-base: fix memleak Stefan Beller
2015-03-21  0:28 ` [PATCH 13/15] builtin/unpack-file: fix a memleak Stefan Beller
2015-03-21  0:28 ` [PATCH 14/15] builtin/cat-file: free memleak Stefan Beller
2015-03-21  0:28 ` [PATCH 15/15] ls-files: fix a memleak Stefan Beller
2015-03-21  3:21 ` [PATCH 00/15] Fixing memory leaks 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=1427129620-13380-1-git-send-email-sbeller@google.com \
    --to=sbeller@google.com \
    --cc=git@vger.kernel.org \
    --cc=gitster@pobox.com \
    --cc=karsten.blees@gmail.com \
    /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).