Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Fixing now, as it is quick, and it gets in the way of the primary change coming in a later commit.
  • Loading branch information
AndrewSisley committed Jan 30, 2025
1 parent 2764a49 commit c072c1f
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 43 deletions.
6 changes: 6 additions & 0 deletions badger/iter.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,12 @@ func (it *iterator) Next() (bool, error) {
return it.restart()
}

if !it.i.Valid() {
// `it.i.Next()` will panic if we attempt to call it having already reached
// the iterator end, so we much return before trying to make that call if we can.
return false, nil
}

it.i.Next()
return it.valid(), nil
}
Expand Down
44 changes: 1 addition & 43 deletions test/integration/iterator/reverse_next_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@ import (
"github.com/sourcenetwork/corekv"
"github.com/sourcenetwork/corekv/test/action"
"github.com/sourcenetwork/corekv/test/integration"
"github.com/sourcenetwork/corekv/test/state"
"github.com/stretchr/testify/require"
)

func TestIteratorReverseNext(t *testing.T) {
Expand All @@ -28,48 +26,8 @@ func TestIteratorReverseNext(t *testing.T) {
test.Execute(t)
}

// This test documents undesirable behaviour, issue:
// https://github.com/sourcenetwork/corekv/issues/19
func TestIteratorReverseNext_BeyondEnd_Badger(t *testing.T) {
func TestIteratorReverseNext_BeyondEnd(t *testing.T) {
test := &integration.Test{
SupportedStoreTypes: []state.StoreType{
state.BadgerStoreType,
},
Actions: []action.Action{
action.Set([]byte("k1"), []byte("v1")),
action.Set([]byte("k3"), nil),
action.Set([]byte("k4"), []byte("v4")),
action.Set([]byte("k2"), []byte("v2")),
&action.Iterator{
IterOptions: corekv.IterOptions{
Reverse: true,
},
ChildActions: []action.IteratorAction{
action.Next(true),
action.Next(true),
action.Next(true),
action.Next(true),
action.Next(false),
action.Next(false),
},
},
},
}

require.PanicsWithError(
t,
"runtime error: invalid memory address or nil pointer dereference",
func() {
test.Execute(t)
},
)
}

func TestIteratorReverseNext_BeyondEnd_Memory(t *testing.T) {
test := &integration.Test{
SupportedStoreTypes: []state.StoreType{
state.MemoryStoreType,
},
Actions: []action.Action{
action.Set([]byte("k1"), []byte("v1")),
action.Set([]byte("k3"), nil),
Expand Down

0 comments on commit c072c1f

Please sign in to comment.