{"thread":{"id":"66928","subject":"[PATCH] imap-send: make the header loop easier to read","startedAt":"2026-11-24T22:48:55Z","lastAt":"2026-11-25T01:49:17Z","messageCount":2,"participants":["Harald Nordgren via GitGitGadget","Junio C Hamano"],"isPatch":true,"patchVersion":1,"patchTotal":null},"messages":[{"id":"559286","messageId":"pull.2508.git.git.1795560535063.gitgitgadget@gmail.com","threadId":"66928","inReplyTo":null,"subject":"[PATCH] imap-send: make the header loop easier to read","fromName":"Harald Nordgren via GitGitGadget","fromEmail":"gitgitgadget@gmail.com","sentAt":"2026-11-24T22:48:55Z","receivedAt":"2026-11-24T22:48:55Z","isPatch":true,"sender":{"key":"haraldnordgren@gmail.com","avatar":"https://avatars.githubusercontent.com/u/9569897?v=4"},"body":"From: Harald Nordgren <haraldnordgren@gmail.com>\n\nThe loop that looks for the From, Date and Subject headers of a\nmessage stops at the first empty line, which can end in LF or CRLF.\nThe check for LF also moves to the next line, and the check for CRLF\ndepends on that, which is easy to miss.\n\nCheck for both kinds of empty line in the same way, before moving to\nthe next line. The behavior does not change.\n\nSuggested-by: Patrick Steinhardt <ps@pks.im>\nSigned-off-by: Harald Nordgren <haraldnordgren@gmail.com>\n---\n    imap-send: make the header loop easier to read\n    \n    Make the loop that finds the end of the message headers easier to read,\n    as suggested in review of the series that is now in master:\n    \n    https://lore.kernel.org/git/avq0R5zx0PgSN-Oa@pks.im\n\nPublished-As: https://github.com/gitgitgadget/git/releases/tag/pr-git-2508%2FHaraldNordgren%2Fimap-send-header-loop-v1\nFetch-It-Via: git fetch https://github.com/gitgitgadget/git pr-git-2508/HaraldNordgren/imap-send-header-loop-v1\nPull-Request: https://github.com/git/git/pull/2508\n\n imap-send.c | 4 +++-\n 1 file changed, 3 insertions(+), 1 deletion(-)\n\ndiff --git a/imap-send.c b/imap-send.c\nindex 1b5850a80e..c2d6b7c55a 100644\n--- a/imap-send.c\n+++ b/imap-send.c\n@@ -1462,7 +1462,9 @@ static bool has_required_headers(const char *line)\n \tbool from = false, date = false, subject = false;\n \n \twhile ((line = strchr(line, '\\n')) &&\n-\t       *++line != '\\n' && !starts_with(line, \"\\r\\n\")) {\n+\t       !starts_with(line, \"\\n\\n\") &&\n+\t       !starts_with(line, \"\\n\\r\\n\")) {\n+\t\tline++;\n \t\tif (istarts_with(line, \"From: \"))\n \t\t\tfrom = true;\n \t\telse if (istarts_with(line, \"Date: \"))\n\nbase-commit: 77904e546de3bd940621092ecec47a88d0cede5d\n-- \ngitgitgadget\n\n"},{"id":"559314","messageId":"xmqqecc9fzoi.fsf@gitster.g","threadId":"66928","inReplyTo":"pull.2508.git.git.1795560535063.gitgitgadget@gmail.com","subject":"Re: [PATCH] imap-send: make the header loop easier to read","fromName":"Junio C Hamano","fromEmail":"gitster@pobox.com","sentAt":"2026-11-25T01:49:17Z","receivedAt":"2026-11-25T01:49:17Z","isPatch":true,"sender":{"key":"gitster@pobox.com","avatar":"https://avatars.githubusercontent.com/u/54884?v=4"},"body":"\"Harald Nordgren via GitGitGadget\" <gitgitgadget@gmail.com> writes:\n\n> From: Harald Nordgren <haraldnordgren@gmail.com>\n>\n> The loop that looks for the From, Date and Subject headers of a\n> message stops at the first empty line, which can end in LF or CRLF.\n> The check for LF also moves to the next line, and the check for CRLF\n> depends on that, which is easy to miss.\n\nIndeed, hiding *++line inside the first part of the condition forced\nanyone reading the !starts_with(line, \"\\r\\n\") check to remember that\n'line' had already moved past the newline.\n\n> diff --git a/imap-send.c b/imap-send.c\n> index 1b5850a80e..c2d6b7c55a 100644\n> --- a/imap-send.c\n> +++ b/imap-send.c\n> @@ -1462,7 +1462,9 @@ static bool has_required_headers(const char *line)\n>  \tbool from = false, date = false, subject = false;\n>  \n>  \twhile ((line = strchr(line, '\\n')) &&\n> -\t       *++line != '\\n' && !starts_with(line, \"\\r\\n\")) {\n> +\t       !starts_with(line, \"\\n\\n\") &&\n> +\t       !starts_with(line, \"\\n\\r\\n\")) {\n> +\t\tline++;\n\nChecking for \"\\n\\n\" and \"\\n\\r\\n\" symmetrically before advancing 'line'\ninside the loop body makes the termination condition much clearer, but\nis this correct?  The latter expects CRLF on the second (i.e., blank)\nline, so presumably line[-1] would be '\\r', but nobody seems to check\nthat.\n\nNot that the original checked that, either.  We are not interested in\nseeing that the first line is an empty line, so that is perfectly fine.\nThe strchr(line, '\\n') would take us to the LF at the end of line,\nwhether it is a LF by itself, or it is the second byte of CRLF.\n\nIn imap-send.c, this function is only called by count_messages() on a\nNUL-terminated strbuf holding the mbox input.  When strchr(line, '\\n')\nreturns non-NULL, 'line' points at the LF terminating the current line,\nand starts_with() stops as soon as it sees a mismatching byte.  So even\nif the input ends immediately after that LF or after \"\\n\\r\", neither\nstarts_with(line, \"\\n\\n\") nor starts_with(line, \"\\n\\r\\n\") reads past the\nterminating NUL.  At EOF after a single trailing LF, both checks pass\nand the loop body runs istarts_with() on the empty string \"\", matching\nthe old *++line behavior, after which strchr() returns NULL and exits\nthe loop.  So as an \"identical behaviour, but easier to reason about\"\nrewrite, this looks good to me.\n\nWill queue.  Thanks.\n\n"}]}