Skip to content

Commit fc6eaf3

Browse files
authored
Fix null pointer dereference in status.ByIndex (libgit2#628)
`git_status_byindex` can return a null pointer if there is no statuses.
1 parent 462ebd8 commit fc6eaf3

File tree

2 files changed

+32
-0
lines changed

2 files changed

+32
-0
lines changed

status.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ package git
66
import "C"
77

88
import (
9+
"errors"
910
"runtime"
1011
"unsafe"
1112
)
@@ -86,6 +87,9 @@ func (statusList *StatusList) ByIndex(index int) (StatusEntry, error) {
8687
return StatusEntry{}, ErrInvalid
8788
}
8889
ptr := C.git_status_byindex(statusList.ptr, C.size_t(index))
90+
if ptr == nil {
91+
return StatusEntry{}, errors.New("index out of Bounds")
92+
}
8993
entry := statusEntryFromC(ptr)
9094
runtime.KeepAlive(statusList)
9195

status_test.go

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,3 +61,31 @@ func TestStatusList(t *testing.T) {
6161
t.Fatal("Incorrect entry path: ", entry.IndexToWorkdir.NewFile.Path)
6262
}
6363
}
64+
65+
func TestStatusNothing(t *testing.T) {
66+
t.Parallel()
67+
repo := createTestRepo(t)
68+
defer cleanupTestRepo(t, repo)
69+
70+
seedTestRepo(t, repo)
71+
72+
opts := &StatusOptions{
73+
Show: StatusShowIndexAndWorkdir,
74+
Flags: StatusOptIncludeUntracked | StatusOptRenamesHeadToIndex | StatusOptSortCaseSensitively,
75+
}
76+
77+
statusList, err := repo.StatusList(opts)
78+
checkFatal(t, err)
79+
80+
entryCount, err := statusList.EntryCount()
81+
checkFatal(t, err)
82+
83+
if entryCount != 0 {
84+
t.Fatal("expected no statuses in empty repo")
85+
}
86+
87+
_, err = statusList.ByIndex(0)
88+
if err == nil {
89+
t.Error("expected error getting status by index")
90+
}
91+
}

0 commit comments

Comments
 (0)