Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix panic compiling record expression with duplicate field #4789

Merged
merged 2 commits into from
Oct 2, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions compiler/semantic/expr.go
Original file line number Diff line number Diff line change
Expand Up @@ -155,10 +155,15 @@ func (a *analyzer) semExpr(e ast.Expr) (dag.Expr, error) {
Where: where,
}, nil
case *ast.RecordExpr:
fields := map[string]struct{}{}
var out []dag.RecordElem
for _, elem := range e.Elems {
switch elem := elem.(type) {
case *ast.Field:
if _, ok := fields[elem.Name]; ok {
return nil, fmt.Errorf("record expression: %w", &zed.DuplicateFieldError{Name: elem.Name})
}
fields[elem.Name] = struct{}{}
e, err := a.semExpr(elem.Value)
if err != nil {
return nil, err
Expand All @@ -169,6 +174,10 @@ func (a *analyzer) semExpr(e ast.Expr) (dag.Expr, error) {
Value: e,
})
case *ast.ID:
if _, ok := fields[elem.Name]; ok {
return nil, fmt.Errorf("record expression: %w", &zed.DuplicateFieldError{Name: elem.Name})
}
fields[elem.Name] = struct{}{}
v, err := a.semID(elem)
if err != nil {
return nil, err
Expand Down
3 changes: 3 additions & 0 deletions compiler/ztests/record-dup-field.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
zed: yield {x:1,x:2}

errorRE: 'record expression: duplicate field: "x"'