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

Re: [PATCH 1/2] get_main_ref_store: BUG() when outside a repository

From
Stefan Beller <sbeller@google.com>
Date
May 18, 2018, 22:51 UTC
Message-ID
<CAGZ79kbE5bdeFnQw=EiRw2iTw4mAs5=RefAh9oxYn9+yjy7YRw@mail.gmail.com>
In-Reply-To
<20180518222552.GA9623@sigill.intra.peff.net>
On Fri, May 18, 2018 at 3:25 PM, Jeff King <peff@peff.net> wrote:
Show 10 quoted lines
> If we don't have a repository, then we can't initialize the
> ref store.  Prior to 64a741619d (refs: store the main ref
> store inside the repository struct, 2018-04-11), we'd try to
> access get_git_dir(), and outside a repository that would
> trigger a BUG(). After that commit, though, we directly use
> the_repository->git_dir; if it's NULL we'll just segfault.
>
> Let's catch this case and restore the BUG() behavior.
> Obviously we don't ever want to hit this code, but a BUG()
> is a lot more helpful than a segfault if we do.

That is true and an immediate bandaid; an alternative would be to do:

  if (!r->gitdir)
    /* not in a git directory ? */
    return NULL;
    /* We signal the caller the absence of a git repo by NULL ness
      of the ref store */

However that we would need to catch at all callers of get_main_ref_store and error out accordingly.

Then the trade off would be, how many callers to the main ref store do we have compared to options that rely on a ref store present? (I assume a patch like the second patch would show up in more cases now for all BUGs that we discover via this patch. The tradeoff is just if we want to report all these early by checking the config and system state, or wait until we get NULL ref store and then bail)

I think checking early makes sense; I am not so sure about this patch; for the time being it makes sense, though in the long run, we rather want to catch this at a higher level:

r->gitdir is supposedly never NULL, so we shall not produce this state. Maybe we want to set the_repository to NULL if set_git_dir fails (via repo_set_gitdir in setup_git_env, which both return void today).

Enough of my rambling, the patches look good! Stefan

Previous: Jeff KingNext: Jeff King
Message 3 of 6 in “fix a segfault in get_main_ref_store()”
  1. 0/2 fix a segfault in get_main_ref_store()Jeff King, May 18, 2018
  2. 1/2 get_main_ref_store: BUG() when outside a repositoryJeff King, May 18, 2018
  3. Stefan BellerMay 18, 2018
  4. 2/2 config: die when --blob is used outside a repositoryJeff King, May 18, 2018
  5. Taylor BlauMay 18, 2018
  6. Johannes SchindelinMay 18, 2018

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.