Web lists-archives.com

Re: [PATCH] get_oid: handle NULL repo->index

On Wed, May 15, 2019 at 08:24:34AM +0700, Duy Nguyen wrote:

> On Tue, May 14, 2019 at 8:54 PM Jeff King <peff@xxxxxxxx> wrote:
> > diff --git a/sha1-name.c b/sha1-name.c
> > index 775a73d8ad..455e9fb1ea 100644
> > --- a/sha1-name.c
> > +++ b/sha1-name.c
> > @@ -1837,7 +1837,7 @@ static enum get_oid_result get_oid_with_context_1(struct repository *repo,
> >                 if (flags & GET_OID_RECORD_PATH)
> >                         oc->path = xstrdup(cp);
> >
> > -               if (!repo->index->cache)
> > +               if (!repo->index || !repo->index->cache)
> >                         repo_read_index(repo);
> We could even drop the "if" and call repo_read_index()
> unconditionally. If the index is already read, it will be no-op
> (forcing a reread has always been discard_index(); read_index();)

I think you missed my bit after the "---":

  Arguably this code should just unconditionally call repo_read_index(),
  which should be a noop if the index is already loaded. But I wanted to
  do the minimal fix here, without getting into any subtle differences
  between what checking index->cache versus index->initialized might
  mean.  Anybody who wants to dig into that is welcome to make a patch
  on top. :)

> Thanks for catching this by the way. I'll need to go through all
> the_index conversion to see if I left similar traps like this.

Yeah. I could not find any others, but it would not hurt to have a
second set of eyes.

Also from my earlier message, if you missed it:

  I also wondered if we should simply allocate an empty index whenever
  we have a non-toplevel "struct repository", which might be less
  surprising to other callers. I don't have a strong opinion either way.
  I did grep around for other callers which might have similar problems,
  but couldn't find any.