Fix null pointer dereference in status.ByIndex #628
|
@ -6,6 +6,7 @@ package git
|
||||||
import "C"
|
import "C"
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"errors"
|
||||||
"runtime"
|
"runtime"
|
||||||
"unsafe"
|
"unsafe"
|
||||||
)
|
)
|
||||||
|
@ -86,6 +87,9 @@ func (statusList *StatusList) ByIndex(index int) (StatusEntry, error) {
|
||||||
return StatusEntry{}, ErrInvalid
|
return StatusEntry{}, ErrInvalid
|
||||||
}
|
}
|
||||||
ptr := C.git_status_byindex(statusList.ptr, C.size_t(index))
|
ptr := C.git_status_byindex(statusList.ptr, C.size_t(index))
|
||||||
|
if ptr == nil {
|
||||||
|
return StatusEntry{}, errors.New("index out of Bounds")
|
||||||
|
}
|
||||||
entry := statusEntryFromC(ptr)
|
entry := statusEntryFromC(ptr)
|
||||||
runtime.KeepAlive(statusList)
|
runtime.KeepAlive(statusList)
|
||||||
|
|
||||||
|
|
|
@ -61,3 +61,31 @@ func TestStatusList(t *testing.T) {
|
||||||
t.Fatal("Incorrect entry path: ", entry.IndexToWorkdir.NewFile.Path)
|
t.Fatal("Incorrect entry path: ", entry.IndexToWorkdir.NewFile.Path)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestStatusNothing(t *testing.T) {
|
||||||
|
t.Parallel()
|
||||||
|
repo := createTestRepo(t)
|
||||||
|
defer cleanupTestRepo(t, repo)
|
||||||
|
|
||||||
|
seedTestRepo(t, repo)
|
||||||
|
|
||||||
|
opts := &StatusOptions{
|
||||||
|
Show: StatusShowIndexAndWorkdir,
|
||||||
|
Flags: StatusOptIncludeUntracked | StatusOptRenamesHeadToIndex | StatusOptSortCaseSensitively,
|
||||||
|
}
|
||||||
|
|
||||||
|
statusList, err := repo.StatusList(opts)
|
||||||
|
checkFatal(t, err)
|
||||||
|
|
||||||
|
entryCount, err := statusList.EntryCount()
|
||||||
|
checkFatal(t, err)
|
||||||
|
|
||||||
|
if entryCount != 0 {
|
||||||
|
t.Fatal("expected no statuses in empty repo")
|
||||||
|
}
|
||||||
|
|
||||||
|
_, err = statusList.ByIndex(0)
|
||||||
|
if err == nil {
|
||||||
|
t.Error("expected error getting status by index")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in New Issue