Skip to content

Commit 8c75548

Browse files
committed
query: handle parse function not consuming all of input
Previously we silently ignored it. For example a search like `(foo))` would just work since we would only parse `(foo)`. We now report back to the user the problem. Note: we already had special handling for unbalanced parenthesis like this `((foo)`. Test Plan: added more test cases. Especially tried to explore areas we could validly not consume the whole input but couldn't find one.
1 parent 7ffb936 commit 8c75548

File tree

2 files changed

+25
-1
lines changed

2 files changed

+25
-1
lines changed

query/parse.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,11 +76,15 @@ func isSpace(c byte) bool {
7676
func Parse(qStr string) (Q, error) {
7777
b := []byte(qStr)
7878

79-
qs, _, err := parseExprList(b)
79+
qs, n, err := parseExprList(b)
8080
if err != nil {
8181
return nil, err
8282
}
8383

84+
if n != len(b) {
85+
return nil, fmt.Errorf("query: extra tokens found at end input: %q", b[n:])
86+
}
87+
8488
q, err := parseOperators(qs)
8589
if err != nil {
8690
return nil, err

query/parse_test.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,27 @@ func TestParseQuery(t *testing.T) {
125125
{"or abc", nil},
126126
{"def or or abc", nil},
127127

128+
// unbalanced parentheses
129+
{"(", nil},
130+
{"((", nil},
131+
{"(((", nil},
132+
{")", nil},
133+
{"))", nil},
134+
{")))", nil},
135+
{"foo)", nil},
136+
{"foo))", nil},
137+
{"foo)))", nil},
138+
{"(foo", nil},
139+
{"((foo", nil},
140+
{"(((foo", nil},
141+
{"(foo))", nil},
142+
{"(((foo))", nil},
143+
128144
{"", &Const{Value: true}},
145+
146+
// whitespace
147+
{" ( ) ", &Const{Value: true}},
148+
{" ( foo ) ", &Substring{Pattern: "foo"}},
129149
} {
130150
got, err := Parse(c.in)
131151
if (c.want == nil) != (err != nil) {

0 commit comments

Comments
 (0)