From: Fabian Stelzer <fs@gigacodes.de>
To: Eric Sunshine <sunshine@sunshineco.com>
Cc: Junio C Hamano <gitster@pobox.com>,
Git List <git@vger.kernel.org>,
Pedro Martelletto <pedro@yubico.com>, Jeff King <peff@peff.net>,
Johannes Schindelin <johannes.schindelin@gmx.de>
Subject: Re: [PATCH v2] gpg-interface: trim CR from ssh-keygen
Date: Tue, 4 Jan 2022 13:55:34 +0100 [thread overview]
Message-ID: <20220104125534.wznwbkyxfcmyfqhb@fs> (raw)
In-Reply-To: <CAPig+cR93GyN53JoZbaiROrNtzGjiet7eTPQOk-26G+mB0KaCA@mail.gmail.com>
On 03.01.2022 22:06, Eric Sunshine wrote:
>On Mon, Jan 3, 2022 at 8:19 PM Junio C Hamano <gitster@pobox.com> wrote:
>> Eric Sunshine <sunshine@sunshineco.com> writes:
>> > On Mon, Jan 3, 2022 at 6:34 PM Junio C Hamano <gitster@pobox.com> wrote:
>> >> Eric Sunshine <sunshine@sunshineco.com> writes:
>> >> > On Mon, Jan 3, 2022 at 9:24 AM Fabian Stelzer <fs@gigacodes.de> wrote:
>> >> >> - trust_size = strcspn(line, "\n");
>> >> >> + trust_size = strcspn(line, "\n"); /* truncate at LF */
>> >> >> + if (trust_size && trust_size != strlen(line) &&
>> >> >> + line[trust_size - 1] == '\r')
>> >> >> + trust_size--; /* the LF was part of CRLF at the end */
>> >> >
>> >> > I may be misunderstanding, but isn't the strlen() unnecessary?
>> >> >
>> >> > if (trust_size && line[trust_size] &&
>> >> > line[trust_size - 1] == '\r')
>> >> > trust_size--;
>> >>
>> >> That changes behaviour when "line" has more than one lines in it.
>> >> strcspn() finds the first LF, and the posted patch ignores CRLF not
>> >> at the end of line[]. Your variant feels more correct if the
>> >> objective is to find the end of the first line (regardless of the
>> >> choice of the end-of-line convention, either LF or CRLF) and omit
>> >> the line terminator.
>> >
>> > Okay, that makes sense if that's the intention of the patch. Perhaps
>> > the commit message should mention that `line` might contain multiple
>> > lines and that it's only interested in the very last LF (unless it's
>> > already obvious to everyone else, even though it wasn't to me).
>>
>> I do not think that is the case. strcspn(line, "\n") will stop at
>> the first one, so unless it is guaranteed that "line" has only one
>> line in it, the patch as posted is not correct. Your variant
>> without strlen() feels more correct, as I said.
>
>Okay, sorry for my unclear thinking. The existing code (before this
>patch) does indeed seem to be interested only in the first line of
>`line`, in which case I agree that the patch's use of strlen() does
>not appear to be correct if `line` could ever contain more than one
>line.
I guess we need a bit more context for this patch to make sense:
for (line = ssh_principals_out.buf; *line;
line = strchrnul(line + 1, '\n')) {
while (*line == '\n')
line++;
if (!*line)
break;
trust_size = strcspn(line, "\n"); /* truncate at LF */
if (trust_size && trust_size != strlen(line) &&
line[trust_size - 1] == '\r')
trust_size--; /* the LF was part of CRLF at the end */
principal = xmemdupz(line, trust_size);
ssh_principals_out contains the result of the find-principals call which
contains one found principal per line (normally LF, CRLF in some cygwin
setup).
A principal can contain CR as a valid character. This is problematic if CR
is the last char of the principal since we have no way of knowing then if we
are in cygwin with CRLF line endings or another platform using LF and the CR
is the last character of the principal.
Lets leave this rather weird edge case aside for now.
So what we want to do is split the buffer by line, no matter which line
endings are used, and copy the principal without any line ending characters.
The `trust_size != strlen(line)` check was supposed to guard against `line`
having no LF at all and ending with a CR. I think a
`line[trust_size + 1] != '\0'` would work as well.
But since this whole thing is already hard enough to follow i guess it's
better we simply remove it instead of adding checks for the unlikely case we
encounter a broken ssh-keygen. Especially since the effect would only be a
failed signature validation.
We could even remove the `if (trust_size)` condition since this only happens
when `line` begins with LF which is already skipped over a few lines before.
But it's probably better to leave this in just in case the code changes.
Generally I think this is a common enough problem that there should be a
function to split a strbuf by line no matter if LF or CRLF is used. Similar
to strbuf_getline() but to read from a strbuf or maybe even handle this
within pipe_command() when filling the strbuf. Maybe git even has better
code to handle this but i haven't found it yet?
next prev parent reply other threads:[~2022-01-04 12:55 UTC|newest]
Thread overview: 30+ messages / expand[flat|nested] mbox.gz Atom feed top
2021-12-03 13:31 [PATCH] gpg-interface: trim CR from ssh-keygen -Y find-principals Johannes Schindelin via GitGitGadget
2021-12-03 14:18 ` Fabian Stelzer
2021-12-03 15:58 ` Jeff King
2021-12-04 13:11 ` Fabian Stelzer
2021-12-05 5:50 ` Junio C Hamano
[not found] ` <CABPYr=y+sDDko9zPxQTOM6Tz4E7CafH7hJc6oB1zv7XYA9KH1A@mail.gmail.com>
2021-12-09 16:33 ` Fabian Stelzer
[not found] ` <CABPYr=xfotWvTQK9k1eKHa0kP4SsB=TKKuM0d8cpMb5BtuUZLA@mail.gmail.com>
2021-12-09 17:20 ` Fabian Stelzer
2021-12-30 10:25 ` Fabian Stelzer
2021-12-05 23:06 ` Damien Miller
2021-12-06 8:39 ` Fabian Stelzer
2022-01-03 9:53 ` [PATCH v2] gpg-interface: trim CR from ssh-keygen Fabian Stelzer
2022-01-03 17:17 ` Eric Sunshine
2022-01-03 23:34 ` Junio C Hamano
2022-01-04 0:41 ` Eric Sunshine
2022-01-04 1:19 ` Junio C Hamano
2022-01-04 3:06 ` Eric Sunshine
2022-01-04 12:55 ` Fabian Stelzer [this message]
2022-01-04 19:33 ` Junio C Hamano
2022-01-05 7:09 ` Eric Sunshine
2022-01-05 10:36 ` Fabian Stelzer
2022-01-05 20:40 ` Junio C Hamano
2022-01-06 10:26 ` Fabian Stelzer
2022-01-06 17:50 ` Junio C Hamano
2022-01-09 20:49 ` Eric Sunshine
2022-01-10 12:28 ` Fabian Stelzer
2022-01-07 9:07 ` [PATCH v3] " Fabian Stelzer
2022-01-09 21:37 ` Eric Sunshine
2022-01-10 12:59 ` Fabian Stelzer
2022-01-10 17:51 ` Junio C Hamano
2022-01-10 17:03 ` Junio C Hamano
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20220104125534.wznwbkyxfcmyfqhb@fs \
--to=fs@gigacodes.de \
--cc=git@vger.kernel.org \
--cc=gitster@pobox.com \
--cc=johannes.schindelin@gmx.de \
--cc=pedro@yubico.com \
--cc=peff@peff.net \
--cc=sunshine@sunshineco.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox