Commit

997c1daf1dce036cd52ba2a4a3d4060b74dd9d54

worktree add: don't read out of bounds in worktree_basename()

When we search for the start of the basename and `len` is zero, `name`
ends up being `path` - 1, out of bounds.  Avoid that by checking before
decrementing.

Fixes https://github.com/git-for-windows/git/issues/6346.

Original-patch-by: Matthias Aßhauer <mha1993@live.de>
Signed-off-by: René Scharfe <l.s.r@web.de>
Signed-off-by: Junio C Hamano <gitster@pobox.com>

René Scharfe <l.s.r@web.de> · 2026-08-25 18:03 UTC · committed by Junio C Hamano

Browse files
1 file changed, +3 −5
modified builtin/worktree.c
@@ -303,11 +303,9 @@ static const char *worktree_basename(const char *path, int *olen)
303 303 while (len && is_dir_sep(path[len - 1]))
304 304 len--;
305 305
306 - for (name = path + len - 1; name > path; name--)
307 - if (is_dir_sep(*name)) {
308 - name++;
309 - break;
310 - }
306 + name = path + len;
307 + while (name > path && !is_dir_sep(name[-1]))
308 + name--;
311 309
312 310 *olen = len;
313 311 return name;