Fix bug #676 found by fuzzer

This commit is contained in:
Vikram Rangnekar 2019-06-16 15:41:28 -04:00
parent 17ad74b4fc
commit 45c283f2a7
2 changed files with 21 additions and 1 deletions

View File

@ -59,7 +59,7 @@ func TestCompile(t *testing.T) {
} }
} }
func TestInvalidCompile(t *testing.T) { func TestInvalidCompile1(t *testing.T) {
qcompile, _ := NewCompiler(Config{}) qcompile, _ := NewCompiler(Config{})
_, err := qcompile.CompileQuery([]byte(`#`)) _, 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) { func TestEmptyCompile(t *testing.T) {
qcompile, _ := NewCompiler(Config{}) qcompile, _ := NewCompiler(Config{})
_, err := qcompile.CompileQuery([]byte(``)) _, err := qcompile.CompileQuery([]byte(``))

View File

@ -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) return nil, fmt.Errorf("16: unexpected value %v (%t)", intf, intf)
} }
// Objects inside a list
if len(node.Name) == 0 { if len(node.Name) == 0 {
pushChildren(st, node.exp, node) pushChildren(st, node.exp, node)
continue continue
@ -686,12 +687,21 @@ func newExp(st *util.Stack, node *Node, usePool bool) (*Exp, error) {
switch name { switch name {
case "and": case "and":
if len(node.Children) == 0 {
return nil, errors.New("missing expression after 'AND' operator")
}
ex.Op = OpAnd ex.Op = OpAnd
pushChildren(st, ex, node) pushChildren(st, ex, node)
case "or": case "or":
if len(node.Children) == 0 {
return nil, errors.New("missing expression after 'OR' operator")
}
ex.Op = OpOr ex.Op = OpOr
pushChildren(st, ex, node) pushChildren(st, ex, node)
case "not": case "not":
if len(node.Children) == 0 {
return nil, errors.New("missing expression after 'NOT' operator")
}
ex.Op = OpNot ex.Op = OpNot
pushChild(st, ex, node) pushChild(st, ex, node)
case "eq", "equals": 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) { func pushChild(st *util.Stack, exp *Exp, node *Node) {
node.Children[0].exp = exp node.Children[0].exp = exp
st.Push(node.Children[0]) st.Push(node.Children[0])
} }
func compileFilter(filter []string) (*Exp, error) { func compileFilter(filter []string) (*Exp, error) {