From: Junio C Hamano <gitster@pobox.com>
To: git@vger.kernel.org
Subject: [PATCH 5/6] strbuf_getwholeline(): clear strbuf upon EOF
Date: Sun, 22 Nov 2026 06:03:46 -0800 [thread overview]
Message-ID: <20261122140347.41990-6-gitster@pobox.com> (raw)
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
next prev parent reply other threads:[~2026-11-22 14:03 UTC|newest]
Thread overview: 8+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-11-22 14:03 [PATCH 0/6] getwholeline should clear the buffer upon EOF Junio C Hamano
2026-11-22 14:03 ` [PATCH 1/6] mailinfo: do not call handle_body() when headers end at EOF Junio C Hamano
2026-11-22 14:03 ` [PATCH 2/6] t4201: demonstrate shortlog reusing stale buffer on EOF Junio C Hamano
2026-11-22 14:03 ` [PATCH 3/6] t7301: demonstrate "clean -i" ask mode deleting past EOF Junio C Hamano
2026-11-22 14:03 ` [PATCH 4/6] t9300: demonstrate fast-import re-parsing heredoc delimiter at EOF Junio C Hamano
2026-11-22 14:03 ` Junio C Hamano [this message]
2026-11-22 14:03 ` [PATCH 6/6] t7301, t9300: test EOF loops fixed by clearing strbuf upon EOF Junio C Hamano
2026-11-23 15:37 ` [PATCH 0/6] getwholeline should clear the buffer " Junio C Hamano
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20261122140347.41990-6-gitster@pobox.com \
--to=gitster@pobox.com \
--cc=git@vger.kernel.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox