Git development
 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 2/6] t4201: demonstrate shortlog reusing stale buffer on EOF
Date: Fri, 4 Dec 2026 16:47:09 +0100	[thread overview]
Message-ID: <axLgc8iFr527BfeA@pks.im> (raw)
In-Reply-To: <20261122140347.41990-3-gitster@pobox.com>

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

Right. After reading the first two paragraphs I already suspected the
second issue, which is the more obvious one. But the first issue can of
course also happen.

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

Naturally we could introduce all of these tests as `test_expect_success`
by manually resetting the line, and we could then drop all of those
resets in the commit that does the change in `strbuf_getline_lf()`. But
in the end the result is the same anyway, so... *shrug*

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 [this message]
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
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=axLgc8iFr527BfeA@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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox