public inbox for git@vger.kernel.org
 help / color / mirror / Atom feed
From: Jeff King <peff@peff.net>
To: cui via GitGitGadget <gitgitgadget@gmail.com>
Cc: git@vger.kernel.org, cui <523516579@qq.com>,
	cuiweixie <cuiweixie@gmail.com>
Subject: Re: [PATCH] fetch: fix wrong evaluation order in URL trailing-slash trimming
Date: Wed, 25 Feb 2026 09:10:59 -0500	[thread overview]
Message-ID: <20260225141059.GE2139176@coredump.intra.peff.net> (raw)
In-Reply-To: <pull.2211.git.git.1771984857879.gitgitgadget@gmail.com>

On Wed, Feb 25, 2026 at 02:00:57AM +0000, cui via GitGitGadget wrote:

> if i == -1, url[i] will be UB.
> [...]
>  	display_state->url_len = strlen(display_state->url);
> -	for (i = display_state->url_len - 1; display_state->url[i] == '/' && 0 <= i; i--)
> +	for (i = display_state->url_len - 1; 0 <= i && display_state->url[i] == '/'; i--)
>  		;
>  	display_state->url_len = i + 1;
>  	if (4 < i && !strncmp(".git", display_state->url + i - 3, 4))

Yeah, the original is obviously nonsense. Probably it is not worth too
much effort to add a test here, but I wondered if this can even be
triggered in practice.

We would hit it when there is no non-slash character in the URL. I'm not
sure it's possible to get this far with that, but it makes sense to me
to write it as you have.

I can't help but think this would be easier to read without an empty
loop body, like:

  for (len = strlen(display_state->url); len > 0; len--) {
	if (display_state->url[len-1] != '/')
		break;
  }

which makes it much more clear we never leave the bounds of the string
(and also works with a size_t, which is a more appropriate type than int
here).

But it may not be worth polishing this bit of code too much (if we did,
I'd also suggest strip_suffix_mem() to drop ".git" rather than all of
those magic numbers. Or even stuffing it in a strbuf and using
strbuf_setlen() and strbuf_strip_suffix().

But anyway, your patch seems like an obvious improvement in the
meantime.

-Peff

  reply	other threads:[~2026-02-25 14:11 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-02-25  2:00 [PATCH] fetch: fix wrong evaluation order in URL trailing-slash trimming cui via GitGitGadget
2026-02-25 14:10 ` Jeff King [this message]
2026-02-25 15:36   ` Junio C Hamano
  -- strict thread matches above, loose matches on Subject: below --
2026-02-23 10:07 cuiweixie

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=20260225141059.GE2139176@coredump.intra.peff.net \
    --to=peff@peff.net \
    --cc=523516579@qq.com \
    --cc=cuiweixie@gmail.com \
    --cc=git@vger.kernel.org \
    --cc=gitgitgadget@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