From 6279be073b9a3637fc73606549539a7a8f1905cc Mon Sep 17 00:00:00 2001 From: Patrick Meyer Date: Sat, 29 Feb 2020 05:33:42 +0100 Subject: [PATCH] lang: Prevent struct types with duplicate field names The previous fix for #591 in 70eecd5 didn't address all issues concerning duplicate struct field names. It still crashed for inputs like `$d []struct{x int; x float}`. Note the different types but duplicate names. --- lang/interpret_test/TestAstFunc2/struct-duplicate4.output | 1 + lang/interpret_test/TestAstFunc2/struct-duplicate4/main.mcl | 1 + lang/parser.y | 4 ++-- 3 files changed, 4 insertions(+), 2 deletions(-) create mode 100644 lang/interpret_test/TestAstFunc2/struct-duplicate4.output create mode 100644 lang/interpret_test/TestAstFunc2/struct-duplicate4/main.mcl diff --git a/lang/interpret_test/TestAstFunc2/struct-duplicate4.output b/lang/interpret_test/TestAstFunc2/struct-duplicate4.output new file mode 100644 index 00000000..5d8fb049 --- /dev/null +++ b/lang/interpret_test/TestAstFunc2/struct-duplicate4.output @@ -0,0 +1 @@ +# err: err1: parser: `syntax error: unexpected $end, expecting EQUALS` @1:6 diff --git a/lang/interpret_test/TestAstFunc2/struct-duplicate4/main.mcl b/lang/interpret_test/TestAstFunc2/struct-duplicate4/main.mcl new file mode 100644 index 00000000..7cc41f75 --- /dev/null +++ b/lang/interpret_test/TestAstFunc2/struct-duplicate4/main.mcl @@ -0,0 +1 @@ +$d []struct{x int;x float} diff --git a/lang/parser.y b/lang/parser.y index 569a1d1f..6ea9c5cf 100644 --- a/lang/parser.y +++ b/lang/parser.y @@ -1180,14 +1180,14 @@ type: strs := []string{} for _, arg := range $3.args { s := fmt.Sprintf("%s %s", arg.Name, arg.Type.String()) - if _, exists := names[s]; exists { + if _, exists := names[arg.Name]; exists { // duplicate field name used err := fmt.Errorf("duplicate struct field of `%s`", s) // this will ultimately cause a parser error to occur... yylex.Error(fmt.Sprintf("%s: %+v", ErrParseSetType, err)) break // we must skip, because code continues! } - names[s] = struct{}{} + names[arg.Name] = struct{}{} strs = append(strs, s) }