From: Junio C Hamano <gitster@pobox.com>
To: "Harald Nordgren via GitGitGadget" <gitgitgadget@gmail.com>
Cc: git@vger.kernel.org, Harald Nordgren <haraldnordgren@gmail.com>
Subject: Re: [PATCH] imap-send: make the header loop easier to read
Date: Tue, 24 Nov 2026 17:49:17 -0800 [thread overview]
Message-ID: <xmqqecc9fzoi.fsf@gitster.g> (raw)
In-Reply-To: <pull.2508.git.git.1795560535063.gitgitgadget@gmail.com> (Harald Nordgren via GitGitGadget's message of "Tue, 24 Nov 2026 22:48:55 +0000")
"Harald Nordgren via GitGitGadget" <gitgitgadget@gmail.com> writes:
> From: Harald Nordgren <haraldnordgren@gmail.com>
>
> The loop that looks for the From, Date and Subject headers of a
> message stops at the first empty line, which can end in LF or CRLF.
> The check for LF also moves to the next line, and the check for CRLF
> depends on that, which is easy to miss.
Indeed, hiding *++line inside the first part of the condition forced
anyone reading the !starts_with(line, "\r\n") check to remember that
'line' had already moved past the newline.
> diff --git a/imap-send.c b/imap-send.c
> index 1b5850a80e..c2d6b7c55a 100644
> --- a/imap-send.c
> +++ b/imap-send.c
> @@ -1462,7 +1462,9 @@ static bool has_required_headers(const char *line)
> bool from = false, date = false, subject = false;
>
> while ((line = strchr(line, '\n')) &&
> - *++line != '\n' && !starts_with(line, "\r\n")) {
> + !starts_with(line, "\n\n") &&
> + !starts_with(line, "\n\r\n")) {
> + line++;
Checking for "\n\n" and "\n\r\n" symmetrically before advancing 'line'
inside the loop body makes the termination condition much clearer, but
is this correct? The latter expects CRLF on the second (i.e., blank)
line, so presumably line[-1] would be '\r', but nobody seems to check
that.
Not that the original checked that, either. We are not interested in
seeing that the first line is an empty line, so that is perfectly fine.
The strchr(line, '\n') would take us to the LF at the end of line,
whether it is a LF by itself, or it is the second byte of CRLF.
In imap-send.c, this function is only called by count_messages() on a
NUL-terminated strbuf holding the mbox input. When strchr(line, '\n')
returns non-NULL, 'line' points at the LF terminating the current line,
and starts_with() stops as soon as it sees a mismatching byte. So even
if the input ends immediately after that LF or after "\n\r", neither
starts_with(line, "\n\n") nor starts_with(line, "\n\r\n") reads past the
terminating NUL. At EOF after a single trailing LF, both checks pass
and the loop body runs istarts_with() on the empty string "", matching
the old *++line behavior, after which strchr() returns NULL and exits
the loop. So as an "identical behaviour, but easier to reason about"
rewrite, this looks good to me.
Will queue. Thanks.
prev parent reply other threads:[~2026-11-25 1:49 UTC|newest]
Thread overview: 2+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-11-24 22:48 [PATCH] imap-send: make the header loop easier to read Harald Nordgren via GitGitGadget
2026-11-25 1:49 ` Junio C Hamano [this message]
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=xmqqecc9fzoi.fsf@gitster.g \
--to=gitster@pobox.com \
--cc=git@vger.kernel.org \
--cc=gitgitgadget@gmail.com \
--cc=haraldnordgren@gmail.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