threads / patch / 66928

patchimap-send: make the header loop easier to read

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

## tl;dr

2 messages between Nov 24, 2026 and Nov 25, 2026. Diffs are folded; open one to read it.

replies: 1people: 2as markdown or json

Harald Nordgren via GitGitGadget· Nov 24, 2026, 22:48 UTC · lore
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(-)
Show changes to imap-send.c +3 −1
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· Nov 25, 2026, 01:49 UTC · re: Harald Nordgren via GitGitGadget · lore

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

"Harald Nordgren via GitGitGadget" <gitgitgadget@gmail.com> writes:
Show 6 quoted lines
> 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.

Show 12 quoted lines
> 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.

← back to recent threads