diff --git a/cursor.go b/cursor.go index 0e235ff..d9b69fc 100644 --- a/cursor.go +++ b/cursor.go @@ -151,7 +151,7 @@ func (c *Cursor) Seek(key string) { rec, err = c.collection.readRecord(c.collection.Head) c.collection.metaLock.RUnlock() if err != nil { - if atomic.LoadInt64(&c.current.Next) != 0 { + if c.current != nil && atomic.LoadInt64(&c.current.Next) != 0 { c.err = err } c.current = nil diff --git a/lm2_test.go b/lm2_test.go index 89dcf79..08cef60 100644 --- a/lm2_test.go +++ b/lm2_test.go @@ -693,3 +693,22 @@ func TestSeekOverwrittenKey(t *testing.T) { t.Fatal(err) } } + +func TestEmptyCollectionCursorSeekPanic(t *testing.T) { + c, err := NewCollection("/tmp/test_emptycollectioncursorseekpanic.lm2", 100) + if err != nil { + t.Fatal(err) + } + defer c.Destroy() + + cur, err := c.NewCursor() + if err != nil { + t.Fatal(err) + } + + cur.Seek("asdf") + + if cur.Valid() { + t.Errorf("expected cur.Valid() to return false") + } +}