All of lore.kernel.org
 help / color / mirror / Atom feed
From: Patrick Steinhardt <ps@pks.im>
To: Junio C Hamano <gitster@pobox.com>
Cc: git@vger.kernel.org
Subject: Re: [PATCH 5/6] strbuf_getwholeline(): clear strbuf upon EOF
Date: Fri, 4 Dec 2026 16:47:20 +0100	[thread overview]
Message-ID: <axLgiApJvOTHIbO5@pks.im> (raw)
In-Reply-To: <20261122140347.41990-6-gitster@pobox.com>

On Sun, Nov 22, 2026 at 06:03:46AM -0800, Junio C Hamano wrote:
> 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.

This last paragraph reads a bit funny to me. I would have simply
mentionde that there was one exception that indeed relied on the current
behaviour, but that this was already fixed in a preceding patch.

I think another good argument for this patch is that
`strbuf_getwholeline_fd()` already does the reset unconditionally. So
the fix here kind of aligns those two functions with one another.

> 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))

I was a bit puzzled at first why the function has two starts. But this
is the two different implementations that depend on whether or not we
have getdelim(3p).

Do we maybe also want to update the function's documentation to mention
that they reset the buffer on EOF?

Patrick

  reply	other threads:[~2026-12-04 15:47 UTC|newest]

Thread overview: 12+ 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-12-04 15:47   ` Patrick Steinhardt
2026-11-22 14:03 ` [PATCH 3/6] t7301: demonstrate "clean -i" ask mode deleting past EOF Junio C Hamano
2026-12-04 15:47   ` Patrick Steinhardt
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 ` [PATCH 5/6] strbuf_getwholeline(): clear strbuf upon EOF Junio C Hamano
2026-12-04 15:47   ` Patrick Steinhardt [this message]
2026-11-22 14:03 ` [PATCH 6/6] t7301, t9300: test EOF loops fixed by clearing " Junio C Hamano
2026-11-23 15:37 ` [PATCH 0/6] getwholeline should clear the buffer " Junio C Hamano
2026-12-04 15:49 ` Patrick Steinhardt

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=axLgiApJvOTHIbO5@pks.im \
    --to=ps@pks.im \
    --cc=git@vger.kernel.org \
    --cc=gitster@pobox.com \
    /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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.