* [PATCH] fetch: fix wrong evaluation order in URL trailing-slash trimming
@ 2026-02-23 10:07 cuiweixie
0 siblings, 0 replies; 4+ messages in thread
From: cuiweixie @ 2026-02-23 10:07 UTC (permalink / raw)
To: git; +Cc: cuiweixie
if i == -1, url[i] will be UB.
Signed-off-by: cuiweixie <cuiweixie@gmail.com>
---
builtin/fetch.c | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/builtin/fetch.c b/builtin/fetch.c
index a3bc7e9380..306138c6e5 100644
--- a/builtin/fetch.c
+++ b/builtin/fetch.c
@@ -722,7 +722,7 @@ static void display_state_init(struct display_state *display_state, struct ref *
display_state->url = xstrdup("foreign");
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))
--
2.39.5 (Apple Git-154)
^ permalink raw reply related [flat|nested] 4+ messages in thread
* [PATCH] fetch: fix wrong evaluation order in URL trailing-slash trimming
@ 2026-02-25 2:00 cui via GitGitGadget
2026-02-25 14:10 ` Jeff King
0 siblings, 1 reply; 4+ messages in thread
From: cui via GitGitGadget @ 2026-02-25 2:00 UTC (permalink / raw)
To: git; +Cc: cui, cuiweixie
From: cuiweixie <cuiweixie@gmail.com>
if i == -1, url[i] will be UB.
Signed-off-by: cuiweixie <cuiweixie@gmail.com>
---
fetch: fix wrong evaluation order in URL trailing-slash trimming
Published-As: https://github.com/gitgitgadget/git/releases/tag/pr-git-2211%2Fcuiweixie%2Fbugfix-v1
Fetch-It-Via: git fetch https://github.com/gitgitgadget/git pr-git-2211/cuiweixie/bugfix-v1
Pull-Request: https://github.com/git/git/pull/2211
builtin/fetch.c | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/builtin/fetch.c b/builtin/fetch.c
index a3bc7e9380..306138c6e5 100644
--- a/builtin/fetch.c
+++ b/builtin/fetch.c
@@ -722,7 +722,7 @@ static void display_state_init(struct display_state *display_state, struct ref *
display_state->url = xstrdup("foreign");
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))
base-commit: 7c02d39fc2ed2702223c7674f73150d9a7e61ba4
--
gitgitgadget
^ permalink raw reply related [flat|nested] 4+ messages in thread
* Re: [PATCH] fetch: fix wrong evaluation order in URL trailing-slash trimming
2026-02-25 2:00 cui via GitGitGadget
@ 2026-02-25 14:10 ` Jeff King
2026-02-25 15:36 ` Junio C Hamano
0 siblings, 1 reply; 4+ messages in thread
From: Jeff King @ 2026-02-25 14:10 UTC (permalink / raw)
To: cui via GitGitGadget; +Cc: git, cui, cuiweixie
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
^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: [PATCH] fetch: fix wrong evaluation order in URL trailing-slash trimming
2026-02-25 14:10 ` Jeff King
@ 2026-02-25 15:36 ` Junio C Hamano
0 siblings, 0 replies; 4+ messages in thread
From: Junio C Hamano @ 2026-02-25 15:36 UTC (permalink / raw)
To: Jeff King; +Cc: cui via GitGitGadget, git, cui, cuiweixie
Jeff King <peff@peff.net> writes:
> 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).
Yes, this is vastly more readable, even though what it does is
exactly the same as the original.
Or instead of having strlen() to scan the entire string once and
then ourselves scan backwards from the end, scan forward ourselves
only once while noting where the last non-slash byte was, or
something.
> 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().
True. None of these "we could do it this way too" bikeshedding has
much value. The patch posted is an obvious and trivial enough
improvement.
Thanks.
^ permalink raw reply [flat|nested] 4+ messages in thread
end of thread, other threads:[~2026-02-25 15:36 UTC | newest]
Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-02-23 10:07 [PATCH] fetch: fix wrong evaluation order in URL trailing-slash trimming cuiweixie
-- strict thread matches above, loose matches on Subject: below --
2026-02-25 2:00 cui via GitGitGadget
2026-02-25 14:10 ` Jeff King
2026-02-25 15:36 ` Junio C Hamano
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox