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

[PATCH] commit-reach: plug minor memory leak after using is_descendant_of()

From
René Scharfe <l.s.r@web.de>
Date
Jun 19, 2020, 13:13 UTC
Message-ID
<ada13c16-d964-c6ee-80ac-626edbc5f52d@web.de>

ref_newer() builds a commit_list to pass a single potential ancestor to is_descendant_of(). The latter leaves the list intact. Release the allocated memory after the call.

Signed-off-by: René Scharfe <l.s.r@web.de>
---
We could allocate the commit_list on the stack, which would simplify such
glue code quite a bit.  That would be dangerous in case is_descendant_of()
or some other function that is handed such a list tries to consume/free()
it.  How can we be tell a function is safe to be given a stack-allocated
list?  Perhaps by marking its argument as const.  Or by converting all
functions to arrays.
 commit-reach.c | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)
diff --git a/commit-reach.c b/commit-reach.c
index 4ca7e706a1..6bba16e7b5 100644
--- a/commit-reach.c
+++ b/commit-reach.c
@@ -396,6 +396,7 @@ int ref_newer(const struct object_id *new_oid, const struct object_id *old_oid)
 	struct object *o;
 	struct commit *old_commit, *new_commit;
 	struct commit_list *old_commit_list = NULL;
+	int ret;

 	/*
 	 * Both new_commit and old_commit must be commit-ish and new_commit is descendant of
@@ -417,7 +418,9 @@ int ref_newer(const struct object_id *new_oid, const struct object_id *old_oid)
 		return 0;

 	commit_list_insert(old_commit, &old_commit_list);
-	return is_descendant_of(new_commit, old_commit_list);
+	ret = is_descendant_of(new_commit, old_commit_list);
+	free_commit_list(old_commit_list);
+	return ret;
 }

 /*
--
2.27.0
Next: Jeff King
Message 1 of 3 in “commit-reach: plug minor memory leak after using is_descendant_of()”
  1. commit-reach: plug minor memory leak after using is_descendant_of()René Scharfe, Jun 19, 2020
  2. Jeff KingJun 19, 2020
  3. Derrick StoleeJun 19, 2020

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.