git/list[1] front-page[2] threads[3] people[4] search[5] about
 

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

From
Junio C Hamano <gitster@pobox.com>
Date
Nov 25, 2026, 01:49 UTC
Message-ID
<xmqqecc9fzoi.fsf@gitster.g>
In-Reply-To
<pull.2508.git.git.1795560535063.gitgitgadget@gmail.com>
"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.
Previous: Harald Nordgren via GitGitGadget
Message 2 of 2 in “imap-send: make the header loop easier to read”
  1. imap-send: make the header loop easier to readHarald Nordgren via GitGitGadget, Nov 24, 2026
  2. Junio C HamanoNov 25, 2026

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.