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

[PATCH 1/3] commit: drop useless xstrdup of commit message

From
Jeff King <peff@peff.net>
Date
Jan 26, 2013, 09:42 UTC
Message-ID
<20130126094236.GA6999@sigill.intra.peff.net>
In-Reply-To
<20130126094026.GA9646@sigill.intra.peff.net>

When git-commit is asked to reuse a commit message via "-c", we call read_commit_message, which looks up the commit and hands back either the re-encoded result, or a copy of the original. We make a copy in the latter case so that the ownership semantics of the return value are clear (in either case, it can be freed).

However, since we return a "const char *", and since the resulting buffer's lifetime is the same as that of the whole program, we never bother to free it at all.

Let's just drop the copy. That saves us a copy in the common case. While it does mean we leak in the re-encode case, it doesn't matter, since we are relying on program exit to free the memory anyway.

Signed-off-by: Jeff King <peff@peff.net>
---
This one isn't strictly necessary, but it makes it a lot more obvious
what is going on with the memory ownership of this code in the next
patch.
 builtin/commit.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/builtin/commit.c b/builtin/commit.c
index 38b9a9c..fbbb40f 100644
--- a/builtin/commit.c
+++ b/builtin/commit.c
@@ -962,7 +962,7 @@ static const char *read_commit_message(const char *name)
 	 * encodings are identical.
 	 */
 	if (out == NULL)
-		out = xstrdup(commit->buffer);
+		out = commit->buffer;
 	return out;
 }
 
-- 
1.8.0.2.16.g72e2fc9
Previous: Jeff KingNext: Jeff King
Message 19 of 24 in “segmentation fault (nullpointer) with git log --submodule -p”
  1. ArminJan 23, 2013
  2. Jeff KingJan 23, 2013
  3. Stefan NäweJan 24, 2013
  4. Duy NguyenJan 24, 2013
  5. Stefan NäweJan 24, 2013
  6. Duy NguyenJan 24, 2013
  7. Jeff KingJan 24, 2013
  8. Junio C HamanoJan 24, 2013
  9. Jeff KingJan 25, 2013
  10. Duy NguyenJan 25, 2013
  11. Junio C HamanoJan 25, 2013
  12. Jeff KingJan 25, 2013
  13. Junio C HamanoJan 25, 2013
  14. Jonathan NiederJan 25, 2013
  15. Junio C HamanoJan 25, 2013
  16. Jonathon MahJan 25, 2013
  17. Junio C HamanoJan 25, 2013
  18. 0/3 lazily load commit->bufferJeff King, Jan 26, 2013
  19. 1/3 commit: drop useless xstrdup of commit messageJeff King, Jan 26, 2013
  20. 2/3 logmsg_reencode: never return NULLJeff King, Jan 26, 2013
  21. 3/3 logmsg_reencode: lazily load missing commit buffersJeff King, Jan 26, 2013
  22. Junio C HamanoJan 26, 2013
  23. Jeff KingJan 26, 2013
  24. Junio C HamanoJan 27, 2013

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.