Skip to content

Commit

Permalink
Fixes out-of-memory fuzzing issue. (#3469)
Browse files Browse the repository at this point in the history
* Fixes out-of-memory fuzzing issue.

Fixes Issue 28535 in oss-fuzz: loki:fuzz_parse_expr: Out-of-memory in fuzz_parse_expr

Signed-off-by: Cyril Tovena <cyril.tovena@gmail.com>

* Add test case.

Signed-off-by: Cyril Tovena <cyril.tovena@gmail.com>
  • Loading branch information
cyriltovena authored Mar 12, 2021
1 parent a419f9c commit e29d536
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 1 deletion.
8 changes: 7 additions & 1 deletion pkg/logql/lex.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"text/scanner"
"time"
"unicode"
"unicode/utf8"

"github.com/dustin/go-humanize"
"github.com/prometheus/common/model"
Expand Down Expand Up @@ -133,7 +134,12 @@ func (l *lexer) Lex(lval *exprSymType) int {

case scanner.String, scanner.RawString:
var err error
lval.str, err = strutil.Unquote(l.TokenText())
tokenText := l.TokenText()
if !utf8.ValidString(tokenText) {
l.Error("invalid UTF-8 rune")
return 0
}
lval.str, err = strutil.Unquote(tokenText)
if err != nil {
l.Error(err.Error())
return 0
Expand Down
5 changes: 5 additions & 0 deletions pkg/logql/parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1298,6 +1298,11 @@ func TestParse(t *testing.T) {
nil, nil,
),
},
{
in: "{app=~\"\xa0\xa1\"}",
exp: nil,
err: ParseError{msg: "invalid UTF-8 encoding", line: 1, col: 7},
},
{
in: `sum_over_time({app="foo"} |= "bar" | json | latency >= 250ms or ( status_code < 500 and status_code > 200)
| line_format "blip{{ .foo }}blop {{.status_code}}" | label_format foo=bar,status_code="buzz{{.bar}}"[5m])`,
Expand Down

0 comments on commit e29d536

Please sign in to comment.