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

Re: [BUG] segmentation fault in git-diff

From
Jeff King <peff@peff.net>
Date
Apr 9, 2020, 23:41 UTC
Message-ID
<20200409234152.GA42330@coredump.intra.peff.net>
In-Reply-To
<xmqq4ktsmfnn.fsf@gitster.c.googlers.com>
On Thu, Apr 09, 2020 at 03:57:00PM -0700, Junio C Hamano wrote:
Show 8 quoted lines
> $ git checkout v2.22.0 && make && ./git-rev-parse :/any/path/
> 
> segfaults, while
> 
> $ git checkout v2.21.0 && make && ./git-rev-parse :/any/path/
> 
> is OK.  We should be able to bisect this fairly straightforward
> between these two.

Indeed. The culprit is c931ba4e78 (sha1-name.c: remove the_repo from handle_one_ref(), 2019-04-16), which swapped out for_each_ref() for refs_for_each_ref(repo->refs). But it misses the access method for repo->refs which lazy-initializes the pointer.

So the immediate fix is:
diff --git a/sha1-name.c b/sha1-name.c
index d9050776dd..c679e246cd 100644
--- a/sha1-name.c
+++ b/sha1-name.c
@@ -1771,8 +1771,8 @@ static enum get_oid_result get_oid_with_context_1(struct repository *repo,
 
 			cb.repo = repo;
 			cb.list = &list;
-			refs_for_each_ref(repo->refs, handle_one_ref, &cb);
-			refs_head_ref(repo->refs, handle_one_ref, &cb);
+			refs_for_each_ref(get_main_ref_store(repo), handle_one_ref, &cb);
+			refs_head_ref(get_main_ref_store(repo), handle_one_ref, &cb);
 			commit_list_sort_by_date(&list);
 			return get_oid_oneline(repo, name + 2, oid, list);
 		}

But there are a bunch of other commits around the same time replacing
the_repository, and it seems like an easy mistake to make. Perhaps we
should rename the "refs" member of "struct repository" to something more
clearly private, which would force callers to use the access method.

I also wonder if there should be a repo_for_each_ref() which does it for
us, though I guess there a bazillion variants (like head_ref()) that
would need similar treatment. Asking each caller to use
get_main_ref_store() isn't too bad.

-Peff
Previous: Junio C HamanoNext: Junio C Hamano
Message 6 of 10 in “[BUG] segmentation fault in git-diff”
  1. Érico RolimApr 9, 2020
  2. Junio C HamanoApr 9, 2020
  3. Junio C HamanoApr 9, 2020
  4. Junio C HamanoApr 9, 2020
  5. Junio C HamanoApr 9, 2020
  6. Jeff KingApr 9, 2020
  7. Re* [BUG] segmentation fault in git-diffJunio C Hamano, Apr 10, 2020
  8. Érico RolimApr 10, 2020
  9. Jeff KingApr 10, 2020
  10. Junio C HamanoApr 10, 2020

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.