Skip to content

Commit

Permalink
Querier/Ingester: Fixing json expression parser bug (#3928)
Browse files Browse the repository at this point in the history
* Fixing json expression parser bug

Signed-off-by: Danny Kopping <danny.kopping@grafana.com>

* UNRELATED CHANGE: pulling main to fix linter issue

Signed-off-by: Danny Kopping <danny.kopping@grafana.com>
  • Loading branch information
Danny Kopping authored Jul 1, 2021
1 parent 22dcfcd commit 71889dc
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 2 deletions.
3 changes: 2 additions & 1 deletion pkg/distributor/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,10 @@ import (

util_log "github.com/cortexproject/cortex/pkg/util/log"
"github.com/go-kit/kit/log/level"
"github.com/grafana/loki/pkg/loghttp/push"
"github.com/weaveworks/common/httpgrpc"
"github.com/weaveworks/common/user"

"github.com/grafana/loki/pkg/loghttp/push"
)

// PushHandler reads a snappy-compressed proto from the HTTP body.
Expand Down
6 changes: 6 additions & 0 deletions pkg/logql/log/jsonexpr/jsonexpr_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,12 @@ func TestJSONExpressionParser(t *testing.T) {
nil,
fmt.Errorf("syntax error: unexpected DOT, expecting FIELD"),
},
{
"syntax error on key access",
`["key`,
nil,
fmt.Errorf("syntax error: unexpected $end, expecting RSB"),
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
Expand Down
11 changes: 10 additions & 1 deletion pkg/logql/log/jsonexpr/lexer.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ func (sc *Scanner) scanField() string {
break
}

if r == '.' || r == scanner.EOF || r == rune(0) {
if r == '.' || isEndOfInput(r) {
sc.unread()
break
}
Expand All @@ -118,6 +118,10 @@ func (sc *Scanner) scanStr() string {

for {
r := sc.read()
if isEndOfInput(r) {
break
}

if r == '"' || r == ']' {
break
}
Expand Down Expand Up @@ -150,6 +154,11 @@ func (sc *Scanner) scanInt() (int, error) {
return strconv.Atoi(string(number))
}

// input is either terminated by EOF or null byte
func isEndOfInput(r rune) bool {
return r == scanner.EOF || r == rune(0)
}

func (sc *Scanner) read() rune {
ch, _, _ := sc.buf.ReadRune()
return ch
Expand Down

0 comments on commit 71889dc

Please sign in to comment.