diff --git a/pkg/distributor/http.go b/pkg/distributor/http.go index b3ced2aa7d4f..880dce10676b 100644 --- a/pkg/distributor/http.go +++ b/pkg/distributor/http.go @@ -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. diff --git a/pkg/logql/log/jsonexpr/jsonexpr_test.go b/pkg/logql/log/jsonexpr/jsonexpr_test.go index d8240d2b6a47..8357372aa8ee 100644 --- a/pkg/logql/log/jsonexpr/jsonexpr_test.go +++ b/pkg/logql/log/jsonexpr/jsonexpr_test.go @@ -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) { diff --git a/pkg/logql/log/jsonexpr/lexer.go b/pkg/logql/log/jsonexpr/lexer.go index f92a72ef170a..fd30730d1e11 100644 --- a/pkg/logql/log/jsonexpr/lexer.go +++ b/pkg/logql/log/jsonexpr/lexer.go @@ -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 } @@ -118,6 +118,10 @@ func (sc *Scanner) scanStr() string { for { r := sc.read() + if isEndOfInput(r) { + break + } + if r == '"' || r == ']' { break } @@ -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