git/list[1] front-page[2] threads[3] people[4] search[5] about
 

[PATCH] update-index: Don't copy memory around

From
Stefan Beller <sbeller@google.com>
Date
Mar 23, 2015, 16:53 UTC
Message-ID
<1427129620-13380-1-git-send-email-sbeller@google.com>
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:
Show 27 quoted lines
> 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
Previous: Junio C HamanoNext: Junio C Hamano
Message 19 of 44 in “Fixing memory leaks”
  1. 00/15 Fixing memory leaksStefan Beller, Mar 21, 2015
  2. 01/15 read-cache: fix memleakStefan Beller, Mar 21, 2015
  3. Junio C HamanoMar 21, 2015
  4. Stefan BellerMar 23, 2015
  5. 1/2 read-cache: fix memleakStefan Beller, Mar 23, 2015
  6. 2/2 read-cache.c: fix a memleak in add_to_indexStefan Beller, Mar 23, 2015
  7. Junio C HamanoMar 23, 2015
  8. Junio C HamanoMar 23, 2015
  9. 02/15 read-cache: Improve readabilityStefan Beller, Mar 21, 2015
  10. Junio C HamanoMar 21, 2015
  11. Stefan BellerMar 21, 2015
  12. Junio C HamanoMar 22, 2015
  13. 03/15 read-cache: free cache entry in add_to_index in case of early returnStefan Beller, Mar 21, 2015
  14. Junio C HamanoMar 21, 2015
  15. Stefan BellerMar 21, 2015
  16. Junio C HamanoMar 22, 2015
  17. 04/15 update-index: fix a memleakStefan Beller, Mar 21, 2015
  18. Junio C HamanoMar 21, 2015
  19. update-index: Don't copy memory aroundStefan Beller, Mar 23, 2015
  20. Junio C HamanoMar 23, 2015
  21. 05/15 builtin/apply.c: fix a memleakStefan Beller, Mar 21, 2015
  22. Junio C HamanoMar 21, 2015
  23. builtin/apply.c: fix a memleak (Fixup, squashable)Stefan Beller, Mar 23, 2015
  24. Junio C HamanoMar 23, 2015
  25. 06/15 merge-blobs.c: Fix a memleakStefan Beller, Mar 21, 2015
  26. 07/15 merge-recursive: fix memleaksStefan Beller, Mar 21, 2015
  27. Junio C HamanoMar 21, 2015
  28. 08/15 http-push: Remove unneeded cleanupStefan Beller, Mar 21, 2015
  29. 09/15 http: release the memory of a http pack request as wellStefan Beller, Mar 21, 2015
  30. Junio C HamanoMar 22, 2015
  31. Stefan BellerMar 24, 2015
  32. Junio C HamanoMar 24, 2015
  33. 10/15 commit.c: fix a memory leakStefan Beller, Mar 21, 2015
  34. Junio C HamanoMar 21, 2015
  35. Duy NguyenMar 24, 2015
  36. Re* [PATCH 10/15] commit.c: fix a memory leakJunio C Hamano, Mar 24, 2015
  37. Stefan BellerMar 24, 2015
  38. 11/15 builtin/check-attr: fix a memleakStefan Beller, Mar 21, 2015
  39. Junio C HamanoMar 21, 2015
  40. 12/15 builtin/merge-base: fix memleakStefan Beller, Mar 21, 2015
  41. 13/15 builtin/unpack-file: fix a memleakStefan Beller, Mar 21, 2015
  42. 14/15 builtin/cat-file: free memleakStefan Beller, Mar 21, 2015
  43. 15/15 ls-files: fix a memleakStefan Beller, Mar 21, 2015
  44. Junio C HamanoMar 21, 2015

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.