Skip to content

Commit

Permalink
Harden field visiting parser paths (#611)
Browse files Browse the repository at this point in the history
  • Loading branch information
TristonianJones authored Nov 11, 2022
1 parent d3a9557 commit db2d910
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 1 deletion.
9 changes: 8 additions & 1 deletion parser/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -633,8 +633,14 @@ func (p *parser) VisitIFieldInitializerList(ctx gen.IFieldInitializerListContext
optional := optField.GetOpt() != nil
if !p.enableOptionalSyntax && optional {
p.reportError(optField, "unsupported syntax '?'")
continue
}
fieldName := optField.IDENTIFIER().GetText()
// The field may be empty due to a prior error.
id := optField.IDENTIFIER()
if id == nil {
return []*exprpb.Expr_CreateStruct_Entry{}
}
fieldName := id.GetText()
value := p.Visit(vals[i]).(*exprpb.Expr)
field := p.helper.newObjectField(initID, fieldName, value, optional)
result[i] = field
Expand Down Expand Up @@ -701,6 +707,7 @@ func (p *parser) VisitMapInitializerList(ctx *gen.MapInitializerListContext) any
optional := optKey.GetOpt() != nil
if !p.enableOptionalSyntax && optional {
p.reportError(optKey, "unsupported syntax '?'")
continue
}
key := p.Visit(optKey.Expr()).(*exprpb.Expr)
value := p.Visit(vals[i]).(*exprpb.Expr)
Expand Down
21 changes: 21 additions & 0 deletions parser/parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1686,6 +1686,27 @@ var testCases = []testInfo{
123^#2:*expr.Constant_Int64Value#
)^#1:*expr.Expr_CallExpr#`,
},
{
I: `x{?.`,
Opts: []Option{
ErrorRecoveryLookaheadTokenLimit(10),
ErrorRecoveryLimit(10),
},
E: `
ERROR: <input>:1:3: unsupported syntax '?'
| x{?.
| ..^
ERROR: <input>:1:4: Syntax error: mismatched input '.' expecting IDENTIFIER
| x{?.
| ...^`,
},
{
I: `x{.`,
E: `
ERROR: <input>:1:3: Syntax error: mismatched input '.' expecting {'}', ',', '?', IDENTIFIER}
| x{.
| ..^`,
},
}

type testInfo struct {
Expand Down

0 comments on commit db2d910

Please sign in to comment.