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

[PATCH 2/6] t4201: demonstrate shortlog reusing stale buffer on EOF

From
Junio C Hamano <gitster@pobox.com>
Date
Nov 22, 2026, 14:03 UTC
Message-ID
<20261122140347.41990-3-gitster@pobox.com>
In-Reply-To
<20261122140347.41990-1-gitster@pobox.com>

When builtin/shortlog.c:read_from_stdin() reads from the standard input, it skips the header lines until it finds the header (either "author" or "committer") we are interested in, parses what is on the line, skips all the remaining header lines, and then skips the blank lines that follow, to find the commit subject line.

But this assumes that strbuf_getline_lf() clears the buffer when returning EOF. If the input ends on an incomplete line, that is not true. Stale contents in 'oneline' cause the command to produce wrong output.

Here are step-by-step walkthroughs for the two failure modes in this code path.

 * If an incomplete "Author:" line at EOF follows an earlier commit, the
   earlier commit leaves its subject in 'oneline'.  The outer loop reads
   the next "Author:" line into 'ident', sets feof(stdin) while scanning
   for LF, and returns 0.  We successfully read the author of this
   second commit, and then the two inner loops to skip other headers and
   blank lines call strbuf_getline_lf() on 'oneline', which sees
   feof(stdin) and returns EOF without resetting 'oneline'.  The strbuf
   still has the subject of the previous commit, and that is what is
   given to insert_one_record().
 * If an incomplete header line (such as "Date:") appears at EOF, after
   matching "Author:", the first inner loop reads the "Date:" line into
   'oneline', sets feof(stdin), and returns 0.  Because '.len' is
   non-zero, the loop runs again; both that iteration and the second
   inner loop see feof(stdin) and return EOF without resetting
   'oneline'.  insert_one_record() therefore records the discarded
   "Date:" header as the commit's subject.

Add two test_expect_failure tests to t4201-shortlog.sh illustrating these issues.

Once strbuf_getwholeline() is changed to reset the buffer before returning on feof(), the first EOF read into 'oneline' empties it in both cases, so insert_one_record() records an empty subject, matching the behavior when the final line is LF-terminated. Because there are other code paths that will be "fixed" by such a change to strbuf_getwholeline(), we will not fix it in this commit (yet), and stop at documenting the issue in the form of test_expect_failure.

Signed-off-by: Junio C Hamano <gitster@pobox.com>
---
 t/t4201-shortlog.sh | 29 +++++++++++++++++++++++++++++
 1 file changed, 29 insertions(+)
diff --git a/t/t4201-shortlog.sh b/t/t4201-shortlog.sh
index 10c43e6e75..dc1b175653 100755
--- a/t/t4201-shortlog.sh
+++ b/t/t4201-shortlog.sh
@@ -447,4 +447,33 @@ test_expect_success 'unknown revision options are reported correctly' '
 	test_grep "unknown option .*--no-such-option" err
 '
 
+test_expect_failure 'shortlog from stdin with incomplete Author line at EOF' '
+	test_write_lines \
+		"Author: A U Thor <author@example.com>" \
+		"" \
+		"    First commit" >in &&
+	printf "%s" "Author: B U Thor <b@example.com>" >>in &&
+	test_write_lines \
+		"A U Thor (1):" \
+		"      First commit" \
+		"" \
+		"B U Thor (1):" \
+		"      " \
+		"" >expect &&
+	git shortlog <in >actual &&
+	test_cmp expect actual
+'
+
+test_expect_failure 'shortlog from stdin with incomplete header line at EOF' '
+	printf "%s\n%s" \
+		"Author: A U Thor <author@example.com>" \
+		"Date:   Mon Jan 1 00:00:00 2024 +0000" >in &&
+	test_write_lines \
+		"A U Thor (1):" \
+		"      " \
+		"" >expect &&
+	git shortlog <in >actual &&
+	test_cmp expect actual
+'
+
 test_done
-- 
2.56.1-766-g80003c9184
Previous: Junio C HamanoNext: Junio C Hamano
Message 3 of 8 in “getwholeline should clear the buffer upon EOF”
  1. 0/6 getwholeline should clear the buffer upon EOFJunio C Hamano, Nov 22, 2026
  2. 1/6 mailinfo: do not call handle_body() when headers end at EOFJunio C Hamano, Nov 22, 2026
  3. 2/6 t4201: demonstrate shortlog reusing stale buffer on EOFJunio C Hamano, Nov 22, 2026
  4. 3/6 t7301: demonstrate "clean -i" ask mode deleting past EOFJunio C Hamano, Nov 22, 2026
  5. 4/6 t9300: demonstrate fast-import re-parsing heredoc delimiter at EOFJunio C Hamano, Nov 22, 2026
  6. 5/6 strbuf_getwholeline(): clear strbuf upon EOFJunio C Hamano, Nov 22, 2026
  7. 6/6 t7301, t9300: test EOF loops fixed by clearing strbuf upon EOFJunio C Hamano, Nov 22, 2026
  8. Junio C HamanoNov 23, 2026

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.