Git development
 help / color / mirror / Atom feed
From: "René Scharfe" <l.s.r@web.de>
To: git@vger.kernel.org
Cc: "Matthias Aßhauer" <mha1993@live.de>
Subject: [PATCH 1/4] worktree add: don't read out of bounds in worktree_basename()
Date: Tue, 25 Aug 2026 20:03:47 +0200	[thread overview]
Message-ID: <20260825180350.2099-2-l.s.r@web.de> (raw)
In-Reply-To: <20260825180350.2099-1-l.s.r@web.de>

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>
---
 builtin/worktree.c | 8 +++-----
 1 file changed, 3 insertions(+), 5 deletions(-)

diff --git a/builtin/worktree.c b/builtin/worktree.c
index 654d27c3e1..a770dd5ead 100644
--- a/builtin/worktree.c
+++ b/builtin/worktree.c
@@ -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;
-- 
2.55.0


  reply	other threads:[~2026-08-25 18:04 UTC|newest]

Thread overview: 9+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-08-25 18:03 [PATCH 0/4] worktree add: worktree_basename() fixes René Scharfe
2026-08-25 18:03 ` René Scharfe [this message]
2026-08-25 18:03 ` [PATCH 2/4] worktree add: reject separator-only path René Scharfe
2026-08-25 18:03 ` [PATCH 3/4] worktree add: trim slashes when deriving branch name from path René Scharfe
2026-08-25 19:47   ` Junio C Hamano
2026-08-25 18:03 ` [PATCH 4/4] worktree add: let worktree_basename() return string copy René Scharfe
2026-08-25 20:04   ` Junio C Hamano
2026-08-26  4:37     ` René Scharfe
2026-08-26 14:35       ` Junio C Hamano

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=20260825180350.2099-2-l.s.r@web.de \
    --to=l.s.r@web.de \
    --cc=git@vger.kernel.org \
    --cc=mha1993@live.de \
    /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