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

Re: [BUG] segmentation fault in git-diff

From
Junio C Hamano <gitster@pobox.com>
Date
Apr 9, 2020, 23:37 UTC
Message-ID
<xmqqzhbkkz84.fsf@gitster.c.googlers.com>
In-Reply-To
<xmqq4ktsmfnn.fsf@gitster.c.googlers.com>

The problem bisects down to c931ba4e (sha1-name.c: remove the_repo from handle_one_ref(), 2019-04-16), which did this:

- for_each_ref(handle_one_ref, &list); - head_ref(handle_one_ref, &list); + 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);

The old code used the helper for_each_ref(). This is a thin wrapper around refs_for_each_ref() and allows the default repository object to be used implicitly by the caller. It is understandable that the code wanted to work on arbitrary repository object, and replaced the for_each_ref() helper with refs_for_each_ref() helper that takes any ref store object.

But there is a small mistake. for_each_ref() makes sure that the ref store is initialized; the new code blindly assumes it has already been initialized.

    int for_each_ref(each_ref_fn fn, void *cb_data)
    {
            return refs_for_each_ref(get_main_ref_store(the_repository), fn, cb_data);
    }

So, I think the fix is simple. With the attached one liner on top of c931ba4e (sha1-name.c: remove the_repo from handle_one_ref(), 2019-04-16),

	$ git rev-parse :/any/path/

no longer segfaults. I think it would also work just fine when merged to more modern codebase, but I haven't tried it (yet).

 sha1-name.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/sha1-name.c b/sha1-name.c
index d9050776dd..3aba62938f 100644
--- a/sha1-name.c
+++ b/sha1-name.c
@@ -1771,7 +1771,7 @@ 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_for_each_ref(get_main_ref_store(repo), handle_one_ref, &cb);
 			refs_head_ref(repo->refs, handle_one_ref, &cb);
 			commit_list_sort_by_date(&list);
 			return get_oid_oneline(repo, name + 2, oid, list);
Previous: Junio C HamanoNext: Jeff King
Message 5 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.