git.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Jeff King <peff@peff.net>
To: "Øystein Walle" <oystwa@gmail.com>
Cc: git@vger.kernel.org, ingy@ingy.net
Subject: Re: [PATCH] rev-parse: Detect missing opt-spec
Date: Fri, 2 Sep 2022 13:13:12 -0400	[thread overview]
Message-ID: <YxI5qBylRhj1jsEv@coredump.intra.peff.net> (raw)
In-Reply-To: <20220902050621.94381-1-oystwa@gmail.com>

On Fri, Sep 02, 2022 at 07:06:21AM +0200, Øystein Walle wrote:

> diff --git a/builtin/rev-parse.c b/builtin/rev-parse.c
> index b259d8990a..04958cf9a9 100644
> --- a/builtin/rev-parse.c
> +++ b/builtin/rev-parse.c
> @@ -479,6 +479,9 @@ static int cmd_parseopt(int argc, const char **argv, const char *prefix)
>  		if (!s)
>  			s = help;
>  
> +		if (s == sb.buf)
> +			die(_("Missing opt-spec before option flags"));
> +
>  		if (s - sb.buf == 1) /* short option only */
>  			o->short_name = *sb.buf;
>  		else if (sb.buf[1] != ',') /* long option only */

I think this is the right thing to do, at least for now. Certainly it
catches the bug. It doesn't allow short or long option names to contain
any flag characters, but that's probably OK in practice.

I think one could make an argument that cmd_parseopt() should do a
better job of parsing left-to-right. The reason it missed this case is
that it calls strpbrk(), expecting to jump past the short/long option
names, but it jumps less far than expected.

If the parsing were more left-to-right, like:

  - start with pointer at beginning of sb.buf

  - look for acceptable character for short option, or "," for none;
    advance pointer if found, otherwise bail

  - look for syntactically valid long option name; advance pointer,
    otherwise bail

  - look for valid flags

then I think it becomes much easier to reason about what is valid for
each item. And we _could_ do things like allowing a short-option that is
also a flag-character, if we wanted to.

But IMHO such a refactoring can come later, or not at all. While it
might make the code a bit more clear, I don't think it meaningfully
improves the behavior. And either way, we should start with a minimal
and easy-to-verify fix like you have here.

-Peff

      parent reply	other threads:[~2022-09-02 17:13 UTC|newest]

Thread overview: 14+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-09-01 21:15 [BUG] git crashes on simple rev-parse incantation Ingy dot Net
2022-09-02  4:28 ` Øystein Walle
2022-09-02  5:06 ` [PATCH] rev-parse: Detect missing opt-spec Øystein Walle
2022-09-02  5:46   ` Eric Sunshine
2022-09-02  6:39     ` [PATCH v2] " Øystein Walle
2022-09-02  7:15       ` Eric Sunshine
2022-09-02  6:47   ` [PATCH] " SZEDER Gábor
2022-09-02 16:27     ` Junio C Hamano
2022-09-02 17:59       ` [PATCH] rev-parse --parseopt: detect " Øystein Walle
2022-09-02 18:01         ` Øystein Walle
2022-09-02 18:45           ` Junio C Hamano
2022-09-02 21:00         ` SZEDER Gábor
2022-09-02 21:29           ` Junio C Hamano
2022-09-02 17:13   ` Jeff King [this message]

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=YxI5qBylRhj1jsEv@coredump.intra.peff.net \
    --to=peff@peff.net \
    --cc=git@vger.kernel.org \
    --cc=ingy@ingy.net \
    --cc=oystwa@gmail.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;
as well as URLs for NNTP newsgroup(s).