Skip to content

Commit

Permalink
JS AST: fix parsing of private identifier tokens, fix nesting error f…
Browse files Browse the repository at this point in the history
…or fuzzing
  • Loading branch information
tdewolff committed Nov 15, 2023
1 parent ef073a3 commit ac235ca
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 3 deletions.
1 change: 1 addition & 0 deletions js/ast_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,7 @@ func TestJS(t *testing.T) {
{"{};;", "{} ;"},
{"{}\n;", "{} ;"},
{"- - --3", "- - --3;"},
{"new\n#c in z", ""},
}

re := regexp.MustCompile("\n *")
Expand Down
5 changes: 3 additions & 2 deletions js/parse.go
Original file line number Diff line number Diff line change
Expand Up @@ -1787,13 +1787,14 @@ func (p *Parser) parseExpression(prec OpPrec) IExpr {
p.in = prevIn
case PrivateIdentifierToken:
if OpCompare < prec || !p.in {
return left
p.fail("expression")
return nil
}
left = &LiteralExpr{p.tt, p.data}
p.next()
if p.tt != InToken {
p.fail("relational expression", InToken)
return left
return nil
}
default:
p.fail("expression")
Expand Down
5 changes: 4 additions & 1 deletion tests/js-string/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ package fuzz

import (
"fmt"
"strings"
"unicode/utf8"

"github.com/tdewolff/parse/v2"
Expand All @@ -22,7 +23,9 @@ func Fuzz(data []byte) int {
src := ast.JSString()
input2 := parse.NewInputString(src)
if ast2, err := js.Parse(input2, o); err != nil {
panic(err)
if !strings.HasPrefix(err.Error(), "too many nested expressions") {
panic(err)
}
} else if src2 := ast2.JSString(); src != src2 {
fmt.Println("JS1:", src)
fmt.Println("JS2:", src2)
Expand Down

0 comments on commit ac235ca

Please sign in to comment.