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

Re: [PATCH 1/2] log: UNLEAK rev to silence a large number of leaks

From
Carlo Marcelo Arenas Belón <carenas@gmail.com>
Date
Sep 19, 2021, 21:34 UTC
Message-ID
<YUes7yxKHKW7cXcl@carlos-mbp.lan>
In-Reply-To
<87o88obkb1.fsf@evledraar.gmail.com>
On Sun, Sep 19, 2021 at 06:13:43PM +0200, Ævar Arnfjörð Bjarmason wrote:
Show 10 quoted lines
> 
> On Sat, Sep 18 2021, Carlo Marcelo Arenas Belón wrote:
> 
> > Note that the cleaning of the "name" in the cmdline item throws a warning
> > as shown below which I intentionally didn't fix, as it would seem that
> > either the use of const there or the need to strdup is wrong.  So hope
> > someone that knows this code better could chime in.
> 
> It should just be a "char *", I got that wrong in my version posted in
> the side-thread[1] & mentioned in the side-reply[2].

I was instead leaning towards keeping it as a "const char *" and removing the strdup as shown in the patch below (obviously the last hunk not relevant to your series).

This object doesn't hold or even manipulate, the objects it contains, so it might be also a cleaner API to ensure it only keeps references and doesn't own any in the more CS sense (note I am not a CS guy, so maybe I get the concept here wrong).

Ironically the original patch that added the strdup was because of leak related work, but I think that in this case might had gotten it backwards.

Even if we start holding pointers to names that are not static, I would expect whoever created those buffers to own the data anyway.

Carlo
CC Michael for advise as the original author
------ >8 ------
Subject: [PATCH] revision: remove dup() of name in add_rev_cmdline()

df835d3a0c (add_rev_cmdline(): make a copy of the name argument, 2013-05-25) adds it, probably introducing a leak.

All names we will ever get will either come from the commandline or be pointers to a static buffer in hex.c, so it is safe not to xstrdup and clean them up (just like the struct object *item).

Signed-off-by: Carlo Marcelo Arenas Belón <carenas@gmail.com>
---
 revision.c | 7 +------
 1 file changed, 1 insertion(+), 6 deletions(-)
diff --git a/revision.c b/revision.c
index ce62192dd8..b20bc58ccd 100644
--- a/revision.c
+++ b/revision.c
@@ -1468,7 +1468,6 @@ static int limit_list(struct rev_info *revs)
 
 /*
  * Add an entry to refs->cmdline with the specified information.
- * *name is copied.
  */
 static void add_rev_cmdline(struct rev_info *revs,
 			    struct object *item,
@@ -1481,7 +1480,7 @@ static void add_rev_cmdline(struct rev_info *revs,
 
 	ALLOC_GROW(info->rev, nr + 1, info->alloc);
 	info->rev[nr].item = item;
-	info->rev[nr].name = xstrdup(name);
+	info->rev[nr].name = name;
 	info->rev[nr].whence = whence;
 	info->rev[nr].flags = flags;
 	info->nr++;
@@ -1490,10 +1489,6 @@ static void add_rev_cmdline(struct rev_info *revs,
 static void clear_rev_cmdline(struct rev_info *revs)
 {
 	struct rev_cmdline_info *info = &revs->cmdline;
-	size_t i, nr = info->nr;
-
-	for (i = 0; i < nr; i++)
-		free(info->rev[i].name);
 
 	FREE_AND_NULL(info->rev);
 	info->nr = info->alloc = 0;
-- 
2.33.0.911.gbe391d4e11
Previous: Ævar Arnfjörð BjarmasonNext: Eric Sunshine
Message 6 of 15 in “Squash leaks in t0000”
  1. 0/2 Squash leaks in t0000Andrzej Hunt via GitGitGadget, Sep 18, 2021
  2. 1/2 log: UNLEAK rev to silence a large number of leaksAndrzej Hunt via GitGitGadget, Sep 18, 2021
  3. Carlo Marcelo Arenas BelónSep 18, 2021
  4. Andrzej HuntSep 19, 2021
  5. Ævar Arnfjörð BjarmasonSep 19, 2021
  6. Carlo Marcelo Arenas BelónSep 19, 2021
  7. Eric SunshineSep 20, 2021
  8. Carlo Marcelo Arenas BelónSep 20, 2021
  9. Jeff KingSep 21, 2021
  10. 2/2 log: UNLEAK original pending objectsAndrzej Hunt via GitGitGadget, Sep 18, 2021
  11. Carlo ArenasSep 18, 2021
  12. Andrzej HuntSep 19, 2021
  13. Ævar Arnfjörð BjarmasonSep 19, 2021
  14. Junio C HamanoSep 20, 2021
  15. Ævar Arnfjörð BjarmasonSep 21, 2021

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.