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>
jch
René Scharfe 2026-08-25 20:03:47 +02:00 committed by Junio C Hamano
parent e9019fcafe
commit 997c1daf1d
1 changed files with 3 additions and 5 deletions

View File

@ -303,11 +303,9 @@ static const char *worktree_basename(const char *path, int *olen)
while (len && is_dir_sep(path[len - 1]))
len--;

for (name = path + len - 1; name > path; name--)
if (is_dir_sep(*name)) {
name++;
break;
}
name = path + len;
while (name > path && !is_dir_sep(name[-1]))
name--;

*olen = len;
return name;