libgit2/git2go

Crash when a hunk points to commit with incomplete signature

suhaibmujahid opened this issue · 1 comments

If you look to the following commit from openstack/neutron, it seems that it is missing the email in the signature.

commit 55599d71849b460a1b0a90e7406542162ba8efdc
Author: salvatore <>
Date:   Fri May 27 00:52:11 2011 +0100

    networks api with final URL structure.
    No serialization yet

The problem is when git2go execute BlameFile and there is a hunk point to this commit. git2go panic.

panic: runtime error: invalid memory address or nil pointer dereference [recovered]
	panic: runtime error: invalid memory address or nil pointer dereference
[signal SIGSEGV: segmentation violation code=0x1 addr=0x18 pc=0x41dfe19]

goroutine 34 [running]:
testing.tRunner.func1.1(0x42ff540, 0x45d64e0)
	/usr/local/go/src/testing/testing.go:941 +0x3d0
testing.tRunner.func1(0xc0000d05a0)
	/usr/local/go/src/testing/testing.go:944 +0x3f9
panic(0x42ff540, 0x45d64e0)
	/usr/local/go/src/runtime/panic.go:967 +0x15d
github.com/libgit2/git2go/v30.newSignatureFromC(0x0, 0xc0000ce1e0)
	/Users/suhaib/go/pkg/mod/github.com/libgit2/git2go/v30@v30.0.0/signature.go:21 +0x29
github.com/libgit2/git2go/v30.blameHunkFromC(0xc30acb0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, ...)
	/Users/suhaib/go/pkg/mod/github.com/libgit2/git2go/v30@v30.0.0/blame.go:156 +0x87
github.com/libgit2/git2go/v30.(*Blame).HunkByLine(0xc000098048, 0x33, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, ...)
	/Users/suhaib/go/pkg/mod/github.com/libgit2/git2go/v30@v30.0.0/blame.go:113 +0x6b
engine/git2go.TestBlame(0xc0000d05a0)
	/Users/suhaib/git/engine/git2go/repo_test.go:101 +0x30d
testing.tRunner(0xc0000d05a0, 0x4376740)
	/usr/local/go/src/testing/testing.go:992 +0xdc
created by testing.(*T).Run
	/usr/local/go/src/testing/testing.go:1043 +0x357

Process finished with exit code 1

The following is a test to reproduce the panic:

func TestBlame(t *testing.T) {
	path := "./repos/neutron"
	repo, err := git.OpenRepository(path)
	if err != nil {
		if !git.IsErrorCode(err, git.ErrNotFound) {
			t.Fatal(err)
		}
		repo, err = git.Clone("https://github.com/openstack/neutron", path, &git.CloneOptions{Bare: true})
		if err != nil {
			t.Fatal(err)
		}
	}

	id, err := git.NewOid("9f1c2488260868ce0c4a5ed60219c92679829281")
	if err != nil {
		t.Fatal(err)
	}

	opts := &git.BlameOptions{
		Flags:              git.BlameNormal,
		MinMatchCharacters: 20,
		NewestCommit:       id,
	}

	blame, err := repo.BlameFile("quantum/manager.py", opts)
	if err != nil {
		t.Fatal(err)
	}

	_, err = blame.HunkByLine(51)
	if err != nil {
		t.Fatal(err)
	}
}

The panic happens because libgit2 returns nill for final_signature on the hunks from this commit. I'm not sure if it intentional behavior from libgit2!

Fixed by #560