Skip to content

Commit 8bc69c6

Browse files
committed
worktree: don't read out of bounds
`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 git-for-windows#6346 Signed-off-by: Matthias Aßhauer <mha1993@live.de>
1 parent 9a0c470 commit 8bc69c6

1 file changed

Lines changed: 11 additions & 7 deletions

File tree

builtin/worktree.c

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -297,17 +297,21 @@ static void remove_junk_on_signal(int signo)
297297
static const char *worktree_basename(const char *path, int *olen)
298298
{
299299
const char *name;
300-
int len;
300+
int len, len2;
301301

302-
len = strlen(path);
302+
len2 = len = strlen(path);
303303
while (len && is_dir_sep(path[len - 1]))
304304
len--;
305305

306-
for (name = path + len - 1; name > path; name--)
307-
if (is_dir_sep(*name)) {
308-
name++;
309-
break;
310-
}
306+
if(len) {
307+
for (name = path + len - 1; name > path; name--)
308+
if (is_dir_sep(*name)) {
309+
name++;
310+
break;
311+
}
312+
}
313+
else
314+
name = path + len2;
311315

312316
*olen = len;
313317
return name;

0 commit comments

Comments
 (0)