Git development
 help / color / mirror / Atom feed
From: "René Scharfe" <l.s.r@web.de>
To: "Matthias Aßhauer via GitGitGadget" <gitgitgadget@gmail.com>,
	git@vger.kernel.org
Cc: "Marc Branchaud" <marcnarc@xiplink.com>,
	"Nguyễn Thái Ngọc Duy" <pclouds@gmail.com>,
	"Eric Sunshine" <sunshine@sunshineco.com>,
	"Matthias Aßhauer" <mha1993@live.de>,
	"Junio C Hamano" <gitster@pobox.com>
Subject: [PATCH v1.5] worktree: Fix out of bounds read that causes data loss and reject invalid empty input in worktree add
Date: Tue, 11 Aug 2026 23:34:07 +0200	[thread overview]
Message-ID: <52ee6501-24ac-402b-b650-92a829030380@web.de> (raw)
In-Reply-To: <pull.2187.git.1784978348.gitgitgadget@gmail.com>

From: =?UTF-8?q?Matthias=20A=C3=9Fhauer?= <mha1993@live.de>

`worktree_basename` tries to read from memory before the passed `path`
string, if `path` is empty (or only consists of directory separators).
That results in unexpected nonsense data being returned to the caller,
which can lead to issues, such as `git worktree add ""` recursively
deleting the current working directory, including `.git`.

Stop reading out of bounds in these cases to avoid that behaviour.

This leads to `git worktree add ""` consistently exiting with the
message `BUG: How come '' becomes empty after sanitization?`, which is
still undesirable, but at least it doesn't result in data loss anymore.

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

Signed-off-by: René Scharfe <l.s.r@web.de>
---
How about this while we're waiting for a reroll?  It implements what the
commit message says, nothing more.  Follows the style of the first loop.

 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

  parent reply	other threads:[~2026-08-11 21:34 UTC|newest]

Thread overview: 9+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-07-25 11:19 [PATCH 0/2] worktree: Fix out of bounds read that causes data loss and reject invalid empty input in worktree add Matthias Aßhauer via GitGitGadget
2026-07-25 11:19 ` [PATCH 1/2] worktree: don't read out of bounds Matthias Aßhauer via GitGitGadget
2026-07-25 16:51   ` Junio C Hamano
2026-07-31  5:45     ` René Scharfe
2026-07-25 11:19 ` [PATCH 2/2] worktree: reject empty string Matthias Aßhauer via GitGitGadget
2026-08-02  6:26   ` René Scharfe
2026-08-02  9:58     ` René Scharfe
2026-08-11 21:34 ` René Scharfe [this message]
2026-08-11 22:46   ` [PATCH v1.5] worktree: Fix out of bounds read that causes data loss and reject invalid empty input in worktree add 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=52ee6501-24ac-402b-b650-92a829030380@web.de \
    --to=l.s.r@web.de \
    --cc=git@vger.kernel.org \
    --cc=gitgitgadget@gmail.com \
    --cc=gitster@pobox.com \
    --cc=marcnarc@xiplink.com \
    --cc=mha1993@live.de \
    --cc=pclouds@gmail.com \
    --cc=sunshine@sunshineco.com \
    /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