summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authormichael boulton <[email protected]>2020-08-14 19:19:21 +0100
committerGitHub <[email protected]>2020-08-14 11:19:21 -0700
commitfc6eaf36388841b16ff004e1d48e887d3f9613dc (patch)
tree605c87ca761838bf339844d1273a1f8b17514ef3
parent462ebd83e0ccba9cd93c05ec12dc3d98064e3d5c (diff)
Fix null pointer dereference in status.ByIndex (#628)
`git_status_byindex` can return a null pointer if there is no statuses.
-rw-r--r--status.go4
-rw-r--r--status_test.go28
2 files changed, 32 insertions, 0 deletions
diff --git a/status.go b/status.go
index e37a96d..3923e1a 100644
--- a/status.go
+++ b/status.go
@@ -6,6 +6,7 @@ package git
import "C"
import (
+ "errors"
"runtime"
"unsafe"
)
@@ -86,6 +87,9 @@ func (statusList *StatusList) ByIndex(index int) (StatusEntry, error) {
return StatusEntry{}, ErrInvalid
}
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)
runtime.KeepAlive(statusList)
diff --git a/status_test.go b/status_test.go
index 17ed94f..d5cd530 100644
--- a/status_test.go
+++ b/status_test.go
@@ -61,3 +61,31 @@ func TestStatusList(t *testing.T) {
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")
+ }
+}