From e9e4ff1085270a2cd17773479f28fe107c296a99 Mon Sep 17 00:00:00 2001 From: Asdine El Hrychy Date: Fri, 20 Sep 2019 13:02:00 +0200 Subject: [PATCH] Prevent panic when index matcher returns nil tree. Fixes #37 --- go.mod | 2 ++ query/matcher.go | 5 +++++ query/query_test.go | 27 +++++++++++++++++++++++++++ 3 files changed, 34 insertions(+) diff --git a/go.mod b/go.mod index 2ce73aac8..50209ee0f 100644 --- a/go.mod +++ b/go.mod @@ -10,3 +10,5 @@ require ( go.etcd.io/bbolt v1.3.3 // indirect golang.org/x/tools v0.0.0-20190524210228-3d17549cdc6b ) + +go 1.13 diff --git a/query/matcher.go b/query/matcher.go index 027ea2ca4..b739d8c7c 100644 --- a/query/matcher.go +++ b/query/matcher.go @@ -452,6 +452,11 @@ type indexResultTable struct { func (i *indexResultTable) Iterate(fn func([]byte, record.Record) error) error { var err error + // if no results, + if i.tree == nil { + return nil + } + i.tree.Ascend(func(it btree.Item) bool { var rec record.Record recordID := []byte(it.(Item)) diff --git a/query/query_test.go b/query/query_test.go index 5021fd8b9..849b6268d 100644 --- a/query/query_test.go +++ b/query/query_test.go @@ -97,6 +97,33 @@ func TestSelect(t *testing.T) { require.NoError(t, err) }) + t.Run("WithEmptyIndex", func(t *testing.T) { + db, err := genji.New(memory.NewEngine()) + require.NoError(t, err) + + var n int + err = db.Update(func(tx *genji.Tx) error { + tb, err := tx.CreateTable("test") + if err != nil { + return err + } + + _, err = tb.CreateIndex("a", index.Options{}) + if err != nil { + return err + } + + res := Select().From(tb).Where(And(StringField("a").Eq("foo"))).Limit(1).Run(tx) + if res.Err() != nil { + return res.Err() + } + + n, err = res.Count() + return err + }) + require.NoError(t, err) + require.Equal(t, 0, n) + }) } func TestDelete(t *testing.T) {