]> git.ipfire.org Git - thirdparty/git.git/commitdiff
ref-filter: avoid strrchr() in rstrip_ref_components()
authorJeff King <peff@peff.net>
Sun, 15 Feb 2026 09:07:44 +0000 (04:07 -0500)
committerJunio C Hamano <gitster@pobox.com>
Tue, 17 Feb 2026 17:45:29 +0000 (09:45 -0800)
To strip path components from our refname string, we repeatedly call
strrchr() to find the trailing slash, shortening the string each time by
assigning NUL over it. This has two downsides:

  1. Calling strrchr() in a loop is quadratic, since each call has to
     call strlen() under the hood to find the end of the string (even
     though we know exactly where it is from the last loop iteration).

  2. We need a temporary buffer, since we're munging the string with NUL
     as we shorten it (which we must do, because strrchr() has no other
     way of knowing what we consider the end of the string).

Using memrchr() would let us fix both of these, but it isn't portable.
So instead, let's just open-code the string traversal from back to
front as we loop.

I doubt that the quadratic nature is a serious concern. You can see it
in practice with something like:

  git init
  git commit --allow-empty -m foo
  echo "$(git rev-parse HEAD) refs/heads$(perl -e 'print "/a" x 500_000')" >.git/packed-refs
  time git for-each-ref --format='%(refname:rstrip=-1)'

That takes ~5.5s to run on my machine before this patch, and ~11ms
after. But I don't think there's a reasonable way for somebody to infect
you with such a garbage ref, as the wire protocol is limited to 64k
pkt-lines. The difference is measurable for me for a 32k-component ref
(about 19ms vs 7ms), so perhaps you could create some chaos by pushing a
lot of them. But we also run into filesystem limits (if the loose
backend is in use), and in practice it seems like there are probably
simpler and more effective ways to waste CPU.

Likewise the extra allocation probably isn't really measurable. In fact,
since our goal is to return an allocated string, we end up having to
make the same allocation anyway (though it is sized to the result,
rather than the input). My main goal was simplicity in avoiding the need
to handle cleaning it up in the early return path.

Signed-off-by: Jeff King <peff@peff.net>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
ref-filter.c

index 9589418c256840a4a2aefdfa0bb410875ba6cd55..01512d50bd6d386e6846fd7a838d4e5620de6c32 100644 (file)
@@ -2213,17 +2213,15 @@ static const char *lstrip_ref_components(const char *refname, int len)
 static const char *rstrip_ref_components(const char *refname, int len)
 {
        int remaining = normalize_component_count(refname, len);
-       char *start = xstrdup(refname);
+       const char *end = refname + strlen(refname);
 
-       while (remaining-- > 0) {
-               char *p = strrchr(start, '/');
-               if (!p) {
-                       free(start);
+       while (remaining > 0) {
+               if (end == refname)
                        return xstrdup("");
-               } else
-                       p[0] = '\0';
+               if (*--end == '/')
+                       remaining--;
        }
-       return start;
+       return xmemdupz(refname, end - refname);
 }
 
 static const char *show_ref(struct refname_atom *atom, const char *refname)