# [PATCH] imap-send: make the header loop easier to read

2 messages from 2026-11-24 to 2026-11-25. Participants: Harald Nordgren via GitGitGadget, Junio C Hamano.
Thread: https://gitlist.dev/t/66928

## Harald Nordgren via GitGitGadget, 2026-11-24 22:48

Subject: [PATCH] imap-send: make the header loop easier to read
Message-ID: <pull.2508.git.git.1795560535063.gitgitgadget@gmail.com>
URL: https://gitlist.dev/e/pull.2508.git.git.1795560535063.gitgitgadget%40gmail.com

```
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.

Check for both kinds of empty line in the same way, before moving to
the next line. The behavior does not change.

Suggested-by: Patrick Steinhardt <ps@pks.im>
Signed-off-by: Harald Nordgren <haraldnordgren@gmail.com>
---
    imap-send: make the header loop easier to read
    
    Make the loop that finds the end of the message headers easier to read,
    as suggested in review of the series that is now in master:
    
    https://lore.kernel.org/git/avq0R5zx0PgSN-Oa@pks.im

Published-As: https://github.com/gitgitgadget/git/releases/tag/pr-git-2508%2FHaraldNordgren%2Fimap-send-header-loop-v1
Fetch-It-Via: git fetch https://github.com/gitgitgadget/git pr-git-2508/HaraldNordgren/imap-send-header-loop-v1
Pull-Request: https://github.com/git/git/pull/2508

 imap-send.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)

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++;
 		if (istarts_with(line, "From: "))
 			from = true;
 		else if (istarts_with(line, "Date: "))

base-commit: 77904e546de3bd940621092ecec47a88d0cede5d
-- 
gitgitgadget


```

## Junio C Hamano, 2026-11-25 01:49

Subject: Re: [PATCH] imap-send: make the header loop easier to read
Message-ID: <xmqqecc9fzoi.fsf@gitster.g>
URL: https://gitlist.dev/e/xmqqecc9fzoi.fsf%40gitster.g
In-Reply-To: <pull.2508.git.git.1795560535063.gitgitgadget@gmail.com>

```
"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.


```
