Thread View: linux.kernel.git
2 messages
2 total messages
Started by "Harald Nordgren via GitGitGadge...
Tue, 24 Nov 2026 22:48
[PATCH] imap-send: make the header loop easier to read
Author: "Harald Nordgren via GitGitGadge...
Date: Tue, 24 Nov 2026 22:48
Date: Tue, 24 Nov 2026 22:48
47 lines
12155 bytes
12155 bytes
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
Re: [PATCH] imap-send: make the header loop easier to read
Author: Junio C Hamano
Date: Wed, 25 Nov 2026 01:49
Date: Wed, 25 Nov 2026 01:49
51 lines
14644 bytes
14644 bytes
"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.
Thread Navigation
This is a paginated view of messages in the thread with full content displayed inline.
Messages are displayed in chronological order, with the original post highlighted in green.
Use pagination controls to navigate through all messages in large threads.
Back to All Threads