From: Junio C Hamano <gitster@pobox.com>
To: Karthik Nayak <karthik.188@gmail.com>
Cc: git@vger.kernel.org, ps@pks.im
Subject: Re: [PATCH v2] refs/ref-cache: fix SEGFAULT when seeking in empty directories
Date: Thu, 25 Sep 2025 10:07:36 -0700 [thread overview]
Message-ID: <xmqqbjmyh353.fsf@gitster.g> (raw)
In-Reply-To: <20250925-583-git-for-each-ref-start-after-v2-1-3613b5a27ff1@gmail.com> (Karthik Nayak's message of "Thu, 25 Sep 2025 11:15:07 +0200")
Karthik Nayak <karthik.188@gmail.com> writes:
> diff --git a/refs/ref-cache.c b/refs/ref-cache.c
> index c180e0aad7..e5e5df16d8 100644
> --- a/refs/ref-cache.c
> +++ b/refs/ref-cache.c
> @@ -539,7 +539,7 @@ static int cache_ref_iterator_seek(struct ref_iterator *ref_iterator,
> */
> break;
> }
> - } while (slash);
> + } while (slash && dir->nr);
> }
This is at the tail of a "do { ... } while (...);" loop, but inside
the loop I see this construct:
for (idx = 0; idx < dir->nr; idx++) {
cmp = strncmp(refname, dir->entries[idx]->name, len);
if (cmp <= 0)
break;
}
/* don't overflow the index */
idx = idx >= dir->nr ? dir->nr - 1 : idx;
i.e., if we scan all the dir->entries[] elements in the innter loop
and did not find any hit, idx would become dir->nr and this inner
loop runs to the end. If (dir->nr == 0), then ?: operator [*] would
become the idx = (dir->nr - 1); And that idx is used for a while
before we get to this "while (slash && dir->nr)".
And then tha tis used like this.
if (slash)
slash = slash + 1;
level->index = idx;
if (dir->entries[idx]->flag & REF_DIR) {
...
IOW, isn't this check a bit too late? 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?
[Side note]
* I found the problematic ?: extremely hard to read, given its
contrast with the terminating condnition of for loop. If you
apply the discipline to keep the textual order match the actual
order, i.e.
for (idx = 0; idx < dir->nr; idx++)
...;
idx = dir->nr <= idx ? dir->nr - 1 : idx;
people would have spotted this more easily.
But perhaps that may be just me. Anyway...
next prev parent reply other threads:[~2025-09-25 17:07 UTC|newest]
Thread overview: 10+ messages / expand[flat|nested] mbox.gz Atom feed top
2025-09-24 7:55 [PATCH] refs/ref-cache: stop seeking into empty directories Karthik Nayak
2025-09-24 12:28 ` Patrick Steinhardt
2025-09-25 8:20 ` Karthik Nayak
2025-09-25 9:15 ` [PATCH v2] refs/ref-cache: fix SEGFAULT when seeking in " Karthik Nayak
2025-09-25 17:07 ` Junio C Hamano [this message]
2025-09-26 7:41 ` Karthik Nayak
2025-09-26 7:44 ` Karthik Nayak
2025-09-26 16:30 ` Junio C Hamano
2025-09-29 14:47 ` Karthik Nayak
2025-10-01 12:17 ` [PATCH v3] " Karthik Nayak
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=xmqqbjmyh353.fsf@gitster.g \
--to=gitster@pobox.com \
--cc=git@vger.kernel.org \
--cc=karthik.188@gmail.com \
--cc=ps@pks.im \
/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;
as well as URLs for NNTP newsgroup(s).