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

Re: git patch-id fails on long lines

From
Jeff King <peff@peff.net>
Date
Sep 20, 2011, 20:18 UTC
Message-ID
<20110920201808.GA18310@sigill.intra.peff.net>
In-Reply-To
<1316541771-sup-9996@pimlott.net>
On Tue, Sep 20, 2011 at 11:07:42AM -0700, Andrew Pimlott wrote:
Show 7 quoted lines
> In patch-id.c, get_one_patchid uses a fixed 1000-char buffer to read a line.[1]
> This causes incorrect results on longer lines.  Pasted below is a git commit
> (from git show) that demonstrates the problem.  The result of running git
> patch-id on this commit is:
> 
> 9220f380851be9cab1a760430e3be096dcbee8c6 9b96b6fde8f7df791a1490ae18e1fa75fbab3262
> 74b8ede07628a574fd586624e0c77a4b6c9967e0 aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa

Hmm, yeah. My initial impression "eh, so what, it will just add the line contents to the sha1 patch-id in two separate hunks". But we actually treat lines magically based on their beginnings, and it seems we accidentally think the "aaa" is the start of the next commit header.

I think this can be trivially converted to use strbuf_getwholeline, something like this (completely untested):

diff --git a/builtin/patch-id.c b/builtin/patch-id.c
index f821eb3..99ba2ca 100644
--- a/builtin/patch-id.c
+++ b/builtin/patch-id.c
@@ -58,11 +58,12 @@ static int scan_hunk_header(const char *p, int *p_before, int *p_after)
 
 static int get_one_patchid(unsigned char *next_sha1, git_SHA_CTX *ctx)
 {
-	static char line[1000];
+	static struct strbuf line_buf = STRBUF_INIT;
 	int patchlen = 0, found_next = 0;
 	int before = -1, after = -1;
 
-	while (fgets(line, sizeof(line), stdin) != NULL) {
+	while (strbuf_getwholeline(&line_buf, stdin, '\n') != EOF) {
+		char *line = line_buf.buf;
 		char *p = line;
 		int len;
 

That just reuses the same heap-allocated buffer over and over, and then
eventually leaks it at the end (but then the program exits anyway). You
could get fancier and actually pass in the strbuf from generate_id_list,
and then release it when we're done.

-Peff
Previous: Jeff King
Message 5 of 5 in “git patch-id fails on long lines”
  1. Andrew PimlottSep 20, 2011
  2. Junio C HamanoSep 20, 2011
  3. patch-id.c: use strbuf instead of a fixed bufferMichael Schubert, Sep 21, 2011
  4. Jeff KingSep 22, 2011
  5. Jeff KingSep 20, 2011

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.