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

Re: [PATCH v2] refs/ref-cache: fix SEGFAULT when seeking in empty directories

From
Karthik Nayak <karthik.188@gmail.com>
Date
Sep 29, 2025, 14:47 UTC
Message-ID
<CAOLa=ZSB90Ne=8VdJcXX0U9MhSWGAi1tTdzAkE9KdE=XuaOBCQ@mail.gmail.com>
In-Reply-To
<xmqq5xd5ch1h.fsf@gitster.g>
Junio C Hamano <gitster@pobox.com> writes:
Show 38 quoted lines
> Karthik Nayak <karthik.188@gmail.com> writes:
>
>> This wouldn't happen because before the loop starts, we set:
>>
>>    dir = get_ref_dir(iter->cache->root);
>>
>> So, `dir` will always have an entry (i.e. 'refs') in the first
>> iteration, after that, the checks in the while section  of the 'do {}
>> while (...)' loop will kick in.
>
> Ah, OK.  So we know dir->nr != 0 when the loop is entered for the
> first time.  We may descend into a new subdirectory and at that
> point we might hit dir->nr == 0 and that is why we want to catch it
> at the end of "do {} while ()".  Makes sense.
>
>>> I wonder if we can leave at
>>> the beginning of the outer loop, even before sort_ref_dir(dir), when
>>> dir->nr is zero, or something?
>>
>> We could add it there too. I can't see the merit of one over the other.
>
> No, I don't, either.  As long as we know dir->nr != 0 upon the entry
> into the loop, the check at the tail end in while() is sufficient.
>
> Thanks for clarifying.  I wonder if a bit of update to the log
> message would help future readers of the code, though.
>
>     When using the files-backend with packed-refs, it is possible that some
>     of the refs directories are empty. For e.g. just after repacking, the
>     'refs/heads' directory would be empty. The ref-cache seek mechanism,
>     doesn't take this into consideration when descending into a subdirectory,
>     and makes an out of bounds access, causing SEGFAULT as we try to
>     access entries within the directory. Fix this by breaking out of the
>     loop when we enter an empty directory.
>
> or something?  I dunno.
>
> Thanks.

Yeah, I think something like this would be much better, I'll amend the commit and send in a new version soon.

Thanks! Karthik

Previous: Junio C HamanoNext: Karthik Nayak
Message 9 of 10 in “refs/ref-cache: stop seeking into empty directories”
  1. refs/ref-cache: stop seeking into empty directoriesKarthik Nayak, Sep 24, 2025
  2. Patrick SteinhardtSep 24, 2025
  3. Karthik NayakSep 25, 2025
  4. refs/ref-cache: fix SEGFAULT when seeking in empty directoriesKarthik Nayak, Sep 25, 2025
  5. Junio C HamanoSep 25, 2025
  6. Karthik NayakSep 26, 2025
  7. Karthik NayakSep 26, 2025
  8. Junio C HamanoSep 26, 2025
  9. Karthik NayakSep 29, 2025
  10. refs/ref-cache: fix SEGFAULT when seeking in empty directoriesKarthik Nayak, Oct 1, 2025

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.