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

[PATCH 5/6] strbuf_getwholeline(): clear strbuf upon EOF

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

strbuf_getwholeline(), and its frontends like strbuf_getline_lf(), has an early return upon seeing that the stream has reached EOF; curiously, it does not touch the strbuf when it takes this early return code path.

And indeed, a few callers of these functions make a false assumption that their buffer will be cleared when the function returns EOF, and due to that assumption, they are buggy, as we saw in the previous commits.

Teach strbuf_getwholeline() to clear the buffer when taking the fast EOF return path. By doing so, we fix all of these code paths, so we can turn the test_expect_failure tests we added into test_expect_success.

This would break existing callers that expect that strbuf_getwholeline() or its frontends do not clear the buffer upon EOF, but such callers are unlikely. If you call strbuf_getline(&buf), the normal thing to expect is to lose what you originally had in buf. Expecting that the original contents can be read back again when the call returns EOF is not really sensible. There is indeed nobody that relies on the fact that the original contents are still available to read.

If strbuf_getwholeline() is "fixed" to clear the buffer upon EOF, "git mailinfo" would call handle_body() with an empty 'line' upon feof(), but if this were a header-only message that was sent as "format=flowed" type, handle_filter_flowed() would get called and access .buf[len - 1] for this empty line whose len == 0, reading out of bounds. This issue has been addressed in an earlier patch to teach the code to never call handle_body() after EOF with an empty line.

Signed-off-by: Junio C Hamano <gitster@pobox.com>
---
 strbuf.c                     | 6 ++++--
 t/t4201-shortlog.sh          | 4 ++--
 t/t7301-clean-interactive.sh | 2 +-
 t/t9300-fast-import.sh       | 2 +-
 4 files changed, 8 insertions(+), 6 deletions(-)
diff --git a/strbuf.c b/strbuf.c
index c3baa47b3f..62abf01bfa 100644
--- a/strbuf.c
+++ b/strbuf.c
@@ -643,8 +643,10 @@ int strbuf_getwholeline(struct strbuf *sb, FILE *fp, int term)
 {
 	ssize_t r;
 
-	if (feof(fp))
+	if (feof(fp)) {
+		strbuf_reset(sb);
 		return EOF;
+	}
 
 	/* Translate slopbuf to NULL, as we cannot call realloc on it */
 	if (!sb->alloc)
@@ -688,10 +690,10 @@ int strbuf_getwholeline(struct strbuf *sb, FILE *fp, int term)
 {
 	int ch;
 
+	strbuf_reset(sb);
 	if (feof(fp))
 		return EOF;
 
-	strbuf_reset(sb);
 	flockfile(fp);
 	while ((ch = getc_unlocked(fp)) != EOF) {
 		if (!strbuf_avail(sb))
diff --git a/t/t4201-shortlog.sh b/t/t4201-shortlog.sh
index dc1b175653..fc731da0d3 100755
--- a/t/t4201-shortlog.sh
+++ b/t/t4201-shortlog.sh
@@ -447,7 +447,7 @@ 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_expect_success 'shortlog from stdin with incomplete Author line at EOF' '
 	test_write_lines \
 		"Author: A U Thor <author@example.com>" \
 		"" \
@@ -464,7 +464,7 @@ test_expect_failure 'shortlog from stdin with incomplete Author line at EOF' '
 	test_cmp expect actual
 '
 
-test_expect_failure 'shortlog from stdin with incomplete header line at EOF' '
+test_expect_success '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 &&
diff --git a/t/t7301-clean-interactive.sh b/t/t7301-clean-interactive.sh
index 73e923d212..c7a32fd7f0 100755
--- a/t/t7301-clean-interactive.sh
+++ b/t/t7301-clean-interactive.sh
@@ -381,7 +381,7 @@ test_expect_success 'git clean -id (ask)' '
 
 '
 
-test_expect_failure 'git clean -id (ask - EOF without trailing LF)' '
+test_expect_success 'git clean -id (ask - EOF without trailing LF)' '
 	mkdir -p build docs &&
 	touch a.out src/part3.c src/part3.h src/part4.c src/part4.h \
 		docs/manual.txt obj.o build/lib.so &&
diff --git a/t/t9300-fast-import.sh b/t/t9300-fast-import.sh
index 22d697ff9a..576fb820b3 100755
--- a/t/t9300-fast-import.sh
+++ b/t/t9300-fast-import.sh
@@ -4087,7 +4087,7 @@ test_expect_success 'Y: rewrite submodules via stdin' '
 	)
 '
 
-test_expect_failure 'fast-import: heredoc terminator at EOF is not re-parsed as command' '
+test_expect_success 'fast-import: heredoc terminator at EOF is not re-parsed as command' '
 	test_when_finished "rm -rf eof-deleteall" &&
 	git init eof-deleteall &&
 	(
-- 
2.56.1-766-g80003c9184
Previous: Junio C HamanoNext: Junio C Hamano
Message 6 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.