git/list[1] front-page[2] threads[3] people[4] search[5] about
 

Re: [PATCH v1.5] worktree: Fix out of bounds read that causes data loss and reject invalid empty input in worktree add

From
René Scharfe <l.s.r@web.de>
Date
Aug 16, 2026, 17:51 UTC
Message-ID
<17e8c4e6-9eeb-4c71-9297-d8d5771217d8@web.de>
In-Reply-To
<xmqqwltwz36a.fsf@gitster.g>
On 8/12/26 12:46 AM, Junio C Hamano wrote:
Show 25 quoted lines
> René Scharfe <l.s.r@web.de> writes:
> 
>> 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.
> 
> This one I think is obvious and clear.  Why not take the authorship
> too so that we do not have to worry about DCO?

That feels unfair: Matthias did most of the work by identifying the bug and removing the premature subtraction from the loop doesn't seem very original to me. Ultimately my main concern is getting this surprisingly impactful bug fixed in a reasonable amount of time, though..

René
Show 24 quoted lines
> 
>>
>>  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;
Previous: Junio C HamanoNext: Junio C Hamano
Message 10 of 11 in “worktree: Fix out of bounds read that causes data loss and reject invalid empty input in worktree add”
  1. 0/2 worktree: Fix out of bounds read that causes data loss and reject invalid empty input in worktree addMatthias Aßhauer via GitGitGadget, Jul 25, 2026
  2. 1/2 worktree: don't read out of boundsMatthias Aßhauer via GitGitGadget, Jul 25, 2026
  3. Junio C HamanoJul 25, 2026
  4. René ScharfeJul 31, 2026
  5. 2/2 worktree: reject empty stringMatthias Aßhauer via GitGitGadget, Jul 25, 2026
  6. René ScharfeAug 2, 2026
  7. René ScharfeAug 2, 2026
  8. worktree: Fix out of bounds read that causes data loss and reject invalid empty input in worktree addRené Scharfe, Aug 11, 2026
  9. Junio C HamanoAug 11, 2026
  10. René ScharfeAug 16, 2026
  11. Junio C HamanoAug 16, 2026

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.