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

BUG: Segfault on "git pull" on "bad object HEAD"

From
Ævar Arnfjörð Bjarmason <avarab@gmail.com>
Date
Jul 11, 2018, 11:00 UTC
Message-ID
<87k1q2c9zq.fsf@evledraar.gmail.com>

This segfaults, but should print an error instead, have a repo with a corrupt HEAD:

    (
        rm -rf /tmp/git &&
        git clone --single-branch --branch todo git@github.com:git/git.git /tmp/git &&
        echo 1111111111111111111111111111111111111111 >/tmp/git/.git/refs/heads/todo &&
        git -C /tmp/git pull
    )

On this repository e.g. "git log" will print "fatal: bad object HEAD", but for some reason "git pull" makes it this far:

    $ git pull
    Segmentation fault
The immediate reason is that in run_diff_index() we have this:
	ent = revs->pending.objects;
And that in this case that's NULL:
    (gdb) bt
    #0  0x000055555565993f in run_diff_index (revs=0x7fffffffcb90, cached=1) at diff-lib.c:524
    #1  0x00005555557633da in has_uncommitted_changes (ignore_submodules=1) at wt-status.c:2345
    #2  0x00005555557634c9 in require_clean_work_tree (action=0x555555798f18 "pull with rebase", hint=0x555555798efb "please commit or stash them.", ignore_submodules=1, gently=0) at wt-status.c:2370
    #3  0x00005555555dbdee in cmd_pull (argc=0, argv=0x7fffffffd868, prefix=0x0) at builtin/pull.c:885
    #4  0x000055555556c9da in run_builtin (p=0x555555a2de50 <commands+1872>, argc=1, argv=0x7fffffffd868) at git.c:417
    #5  0x000055555556cce2 in handle_builtin (argc=1, argv=0x7fffffffd868) at git.c:633
    #6  0x000055555556ce8a in run_argv (argcp=0x7fffffffd71c, argv=0x7fffffffd710) at git.c:685
    #7  0x000055555556d03f in cmd_main (argc=1, argv=0x7fffffffd868) at git.c:762
    #8  0x0000555555611786 in main (argc=3, argv=0x7fffffffd858) at common-main.c:45
    (gdb) p revs
    $4 = (struct rev_info *) 0x7fffffffcb90
    (gdb) p revs->pending
    $5 = {nr = 0, alloc = 0, objects = 0x0}
    (gdb)

This has been an issue since at least v2.8.0 (didn't test back further). I'm not familiar with the status / diff code, so I'm not sure where the assertion should be added.

This came up in the wild due to a user with a corrupt repo (don't know how it got corrupt) trying "git pull" and seeing git segfault.

Next: Jeff King
Message 1 of 7 in “BUG: Segfault on "git pull" on "bad object HEAD"”
  1. Ævar Arnfjörð BjarmasonJul 11, 2018
  2. Jeff KingJul 11, 2018
  3. has_uncommitted_changes(): fall back to empty treeJeff King, Jul 11, 2018
  4. Ævar Arnfjörð BjarmasonJul 11, 2018
  5. Jeff KingJul 11, 2018
  6. Junio C HamanoJul 11, 2018
  7. Duy NguyenJul 11, 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.