From 5b2c47f6b76d70f5321681fa9ae77d7160c5e560 Mon Sep 17 00:00:00 2001 From: Michael Boulton Date: Fri, 14 Aug 2020 10:56:14 +0100 Subject: [PATCH 1/7] Fix null pointer dereference in getting status by index --- status.go | 3 +++ 1 file changed, 3 insertions(+) diff --git a/status.go b/status.go index e37a96d..c61aedb 100644 --- a/status.go +++ b/status.go @@ -86,6 +86,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{}, MakeGitError(C.int(ErrNotFound)) + } entry := statusEntryFromC(ptr) runtime.KeepAlive(statusList) -- 2.45.2 From a2b8cf952a58cfad876ea14795994a7fb805ce98 Mon Sep 17 00:00:00 2001 From: Michael Boulton Date: Fri, 14 Aug 2020 11:05:58 +0100 Subject: [PATCH 2/7] Add test for no statuses --- status_test.go | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/status_test.go b/status_test.go index 17ed94f..9b82907 100644 --- a/status_test.go +++ b/status_test.go @@ -61,3 +61,30 @@ 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{} + opts.Show = StatusShowIndexAndWorkdir + opts.Flags = StatusOptIncludeUntracked | StatusOptRenamesHeadToIndex | StatusOptSortCaseSensitively + + statusList, err := repo.StatusList(opts) + checkFatal(t, err) + + entryCount, err := statusList.EntryCount() + checkFatal(t, err) + + if entryCount != 0 { + t.Error("expected no statuses in empty repo") + } + + _, err = statusList.ByIndex(0) + if err == nil { + t.Error("expected error getting status by index") + } +} -- 2.45.2 From 1e38283adb523af1615a31cf433a7acd0b25c78e Mon Sep 17 00:00:00 2001 From: Michael Boulton Date: Fri, 14 Aug 2020 11:11:35 +0100 Subject: [PATCH 3/7] Fix test failure --- status.go | 2 ++ 1 file changed, 2 insertions(+) diff --git a/status.go b/status.go index c61aedb..4e1196c 100644 --- a/status.go +++ b/status.go @@ -87,6 +87,8 @@ func (statusList *StatusList) ByIndex(index int) (StatusEntry, error) { } ptr := C.git_status_byindex(statusList.ptr, C.size_t(index)) if ptr == nil { + runtime.LockOSThread() + defer runtime.UnlockOSThread() return StatusEntry{}, MakeGitError(C.int(ErrNotFound)) } entry := statusEntryFromC(ptr) -- 2.45.2 From faa4e293bac16edcb585cf8c747fb49c61d378df Mon Sep 17 00:00:00 2001 From: michael boulton <61595820+mbfr@users.noreply.github.com> Date: Fri, 14 Aug 2020 15:05:34 +0100 Subject: [PATCH 4/7] Update status.go Add suggested error message on return Co-authored-by: lhchavez --- status.go | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/status.go b/status.go index 4e1196c..570de3a 100644 --- a/status.go +++ b/status.go @@ -87,9 +87,7 @@ func (statusList *StatusList) ByIndex(index int) (StatusEntry, error) { } ptr := C.git_status_byindex(statusList.ptr, C.size_t(index)) if ptr == nil { - runtime.LockOSThread() - defer runtime.UnlockOSThread() - return StatusEntry{}, MakeGitError(C.int(ErrNotFound)) + return StatusEntry{}, fmt.Errorf("Index out of Bounds") } entry := statusEntryFromC(ptr) runtime.KeepAlive(statusList) -- 2.45.2 From a63936f29e4366cec607fc71bde94edd186411c8 Mon Sep 17 00:00:00 2001 From: michael boulton <61595820+mbfr@users.noreply.github.com> Date: Fri, 14 Aug 2020 15:07:37 +0100 Subject: [PATCH 5/7] Update status_test.go Move setting struct values into struct initialisation Co-authored-by: lhchavez --- status_test.go | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/status_test.go b/status_test.go index 9b82907..c5af3b0 100644 --- a/status_test.go +++ b/status_test.go @@ -69,9 +69,10 @@ func TestStatusNothing(t *testing.T) { seedTestRepo(t, repo) - opts := &StatusOptions{} - opts.Show = StatusShowIndexAndWorkdir - opts.Flags = StatusOptIncludeUntracked | StatusOptRenamesHeadToIndex | StatusOptSortCaseSensitively + opts := &StatusOptions{ + Show: StatusShowIndexAndWorkdir, + Flags: StatusOptIncludeUntracked | StatusOptRenamesHeadToIndex | StatusOptSortCaseSensitively, + } statusList, err := repo.StatusList(opts) checkFatal(t, err) -- 2.45.2 From e22f95bd3c7f735b4b0cccf3db15a0b33537fd1a Mon Sep 17 00:00:00 2001 From: michael boulton <61595820+mbfr@users.noreply.github.com> Date: Fri, 14 Aug 2020 15:08:13 +0100 Subject: [PATCH 6/7] Update status_test.go Early exit test if preconditions are not met Co-authored-by: lhchavez --- status_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/status_test.go b/status_test.go index c5af3b0..d5cd530 100644 --- a/status_test.go +++ b/status_test.go @@ -81,7 +81,7 @@ func TestStatusNothing(t *testing.T) { checkFatal(t, err) if entryCount != 0 { - t.Error("expected no statuses in empty repo") + t.Fatal("expected no statuses in empty repo") } _, err = statusList.ByIndex(0) -- 2.45.2 From 1a200ed2e812f87a1bba5d2caae725793235448c Mon Sep 17 00:00:00 2001 From: Michael Boulton Date: Fri, 14 Aug 2020 15:10:09 +0100 Subject: [PATCH 7/7] Fix error return --- status.go | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/status.go b/status.go index 570de3a..3923e1a 100644 --- a/status.go +++ b/status.go @@ -6,6 +6,7 @@ package git import "C" import ( + "errors" "runtime" "unsafe" ) @@ -87,7 +88,7 @@ func (statusList *StatusList) ByIndex(index int) (StatusEntry, error) { } ptr := C.git_status_byindex(statusList.ptr, C.size_t(index)) if ptr == nil { - return StatusEntry{}, fmt.Errorf("Index out of Bounds") + return StatusEntry{}, errors.New("index out of Bounds") } entry := statusEntryFromC(ptr) runtime.KeepAlive(statusList) -- 2.45.2