From 45c283f2a704d6179dae7ae385966f8dc92c6f0d Mon Sep 17 00:00:00 2001 From: Vikram Rangnekar Date: Sun, 16 Jun 2019 15:41:28 -0400 Subject: [PATCH] Fix bug #676 found by fuzzer --- qcode/parse_test.go | 11 ++++++++++- qcode/qcode.go | 11 +++++++++++ 2 files changed, 21 insertions(+), 1 deletion(-) diff --git a/qcode/parse_test.go b/qcode/parse_test.go index 3fef81c..6edd9ca 100644 --- a/qcode/parse_test.go +++ b/qcode/parse_test.go @@ -59,7 +59,7 @@ func TestCompile(t *testing.T) { } } -func TestInvalidCompile(t *testing.T) { +func TestInvalidCompile1(t *testing.T) { qcompile, _ := NewCompiler(Config{}) _, err := qcompile.CompileQuery([]byte(`#`)) @@ -68,6 +68,15 @@ func TestInvalidCompile(t *testing.T) { } } +func TestInvalidCompile2(t *testing.T) { + qcompile, _ := NewCompiler(Config{}) + _, err := qcompile.CompileQuery([]byte(`{u(where:{not:0})}`)) + + if err == nil { + t.Fatal(errors.New("expecting an error")) + } +} + func TestEmptyCompile(t *testing.T) { qcompile, _ := NewCompiler(Config{}) _, err := qcompile.CompileQuery([]byte(``)) diff --git a/qcode/qcode.go b/qcode/qcode.go index 731c0aa..d61519d 100644 --- a/qcode/qcode.go +++ b/qcode/qcode.go @@ -419,6 +419,7 @@ func (com *Compiler) compileArgNode(node *Node, usePool bool) (*Exp, error) { return nil, fmt.Errorf("16: unexpected value %v (%t)", intf, intf) } + // Objects inside a list if len(node.Name) == 0 { pushChildren(st, node.exp, node) continue @@ -686,12 +687,21 @@ func newExp(st *util.Stack, node *Node, usePool bool) (*Exp, error) { switch name { case "and": + if len(node.Children) == 0 { + return nil, errors.New("missing expression after 'AND' operator") + } ex.Op = OpAnd pushChildren(st, ex, node) case "or": + if len(node.Children) == 0 { + return nil, errors.New("missing expression after 'OR' operator") + } ex.Op = OpOr pushChildren(st, ex, node) case "not": + if len(node.Children) == 0 { + return nil, errors.New("missing expression after 'NOT' operator") + } ex.Op = OpNot pushChild(st, ex, node) case "eq", "equals": @@ -848,6 +858,7 @@ func pushChildren(st *util.Stack, exp *Exp, node *Node) { func pushChild(st *util.Stack, exp *Exp, node *Node) { node.Children[0].exp = exp st.Push(node.Children[0]) + } func compileFilter(filter []string) (*Exp, error) {