From: "Matthias Aßhauer via GitGitGadget" <gitgitgadget@gmail.com>
To: 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>,
"Matthias Aßhauer" <mha1993@live.de>
Subject: [PATCH 1/2] worktree: don't read out of bounds
Date: Sat, 25 Jul 2026 11:19:06 +0000 [thread overview]
Message-ID: <8bc69c6b80ed42888327331b1567cecf7225ea7e.1784978348.git.gitgitgadget@gmail.com> (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: Matthias Aßhauer <mha1993@live.de>
---
builtin/worktree.c | 18 +++++++++++-------
1 file changed, 11 insertions(+), 7 deletions(-)
diff --git a/builtin/worktree.c b/builtin/worktree.c
index 4bc7b4f6e7..d8188035db 100644
--- a/builtin/worktree.c
+++ b/builtin/worktree.c
@@ -297,17 +297,21 @@ static void remove_junk_on_signal(int signo)
static const char *worktree_basename(const char *path, int *olen)
{
const char *name;
- int len;
+ int len, len2;
- len = strlen(path);
+ len2 = len = strlen(path);
while (len && is_dir_sep(path[len - 1]))
len--;
- for (name = path + len - 1; name > path; name--)
- if (is_dir_sep(*name)) {
- name++;
- break;
- }
+ if(len) {
+ for (name = path + len - 1; name > path; name--)
+ if (is_dir_sep(*name)) {
+ name++;
+ break;
+ }
+ }
+ else
+ name = path + len2;
*olen = len;
return name;
--
gitgitgadget
next prev parent reply other threads:[~2026-07-25 11:19 UTC|newest]
Thread overview: 3+ 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 ` Matthias Aßhauer via GitGitGadget [this message]
2026-07-25 11:19 ` [PATCH 2/2] worktree: reject empty string Matthias Aßhauer via GitGitGadget
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=8bc69c6b80ed42888327331b1567cecf7225ea7e.1784978348.git.gitgitgadget@gmail.com \
--to=gitgitgadget@gmail.com \
--cc=git@vger.kernel.org \
--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