Skip to content

Commit

Permalink
Oid: make sure not to dereference a NULL git_oid
Browse files Browse the repository at this point in the history
Some calls like Reference.Target() can return NULL if the reference is
symbolic. Make sure newOidFromC() can handle these situations.
  • Loading branch information
carlosmn committed Mar 8, 2013
1 parent b57c792 commit 62a1639
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 0 deletions.
4 changes: 4 additions & 0 deletions git.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,10 @@ type Oid struct {
}

func newOidFromC(coid *C.git_oid) *Oid {
if coid == nil {
return nil
}

oid := new(Oid)
copy(oid.bytes[0:20], C.GoBytes(unsafe.Pointer(coid), 20))
return oid
Expand Down
12 changes: 12 additions & 0 deletions reference_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,22 @@ func TestRefModification(t *testing.T) {
checkFatal(t, err)
checkRefType(t, ref, SYMBOLIC)

if target := ref.Target(); target != nil {
t.Fatalf("Expected nil *Oid, got %v", target)
}

ref, err = ref.Resolve()
checkFatal(t, err)
checkRefType(t, ref, OID)

if target := ref.Target(); target == nil {
t.Fatalf("Expected valid target got nil")
}

if target := ref.SymbolicTarget(); target != "" {
t.Fatalf("Expected empty string, got %v", target)
}

if commitId.String() != ref.Target().String() {
t.Fatalf("Wrong ref target")
}
Expand Down

0 comments on commit 62a1639

Please sign in to comment.