lang: Prevent struct types with duplicate fields

Struct types with duplicate fields are invalid types and weren't caught
by the parser. This fixes the issue and adds some associated tests. It
also checks and tests for duplicate struct value field names.

As a technical side-note, this doesn't change the lang/types/ functions
to remove panics-- the signatures are simplified to make their use
simple, and we intentionally panic if they're used incorrectly. In this
case, one was being used without having previously validated the input.

Thanks to Patrick Meyer for finding this issue via fuzzing!
This commit is contained in:
James Shubin
2020-02-27 18:52:02 -05:00
parent 380d03257f
commit 70eecd5289
11 changed files with 75 additions and 8 deletions

View File

@@ -976,6 +976,8 @@ func TestAstFunc1(t *testing.T) {
func TestAstFunc2(t *testing.T) {
const magicError = "# err: "
const magicError1 = "err1: "
const magicError9 = "err9: " // TODO: rename
// TODO: move them all down by one
const magicError2 = "err2: "
const magicError3 = "err3: "
const magicError4 = "err4: "
@@ -1010,6 +1012,8 @@ func TestAstFunc2(t *testing.T) {
type errs struct {
fail1 bool
fail9 bool // TODO: rename
// TODO: move them all down by one
fail2 bool
fail3 bool
fail4 bool
@@ -1068,6 +1072,8 @@ func TestAstFunc2(t *testing.T) {
// if the graph file has a magic error string, it's a failure
errStr := ""
fail1 := false
fail9 := false // TODO: rename
// TODO: move them all down by one
fail2 := false
fail3 := false
fail4 := false
@@ -1082,6 +1088,12 @@ func TestAstFunc2(t *testing.T) {
str = errStr
fail1 = true
}
if strings.HasPrefix(str, magicError9) { // TODO: rename
errStr = strings.TrimPrefix(str, magicError9)
str = errStr
fail9 = true
}
// TODO: move them all down by one
if strings.HasPrefix(str, magicError2) {
errStr = strings.TrimPrefix(str, magicError2)
str = errStr
@@ -1117,6 +1129,8 @@ func TestAstFunc2(t *testing.T) {
expstr: str,
errs: errs{
fail1: fail1,
fail9: fail9, // TODO: rename
// TODO: move them all down by one
fail2: fail2,
fail3: fail3,
fail4: fail4,
@@ -1156,6 +1170,8 @@ func TestAstFunc2(t *testing.T) {
name, path, fail, expstr, errs := tc.name, tc.path, tc.fail, strings.Trim(tc.expstr, "\n"), tc.errs
src := dir + path // location of the test
fail1 := errs.fail1
fail9 := errs.fail9 // TODO: rename
// TODO: move them all down by one
fail2 := errs.fail2
fail3 := errs.fail3
fail4 := errs.fail4
@@ -1282,11 +1298,28 @@ func TestAstFunc2(t *testing.T) {
},
}
// some of this might happen *after* interpolate in SetScope or Unify...
if err := ast.Init(data); err != nil {
err = ast.Init(data)
if (!fail || !fail9) && err != nil {
t.Errorf("test #%d: FAIL", index)
t.Errorf("test #%d: could not init and validate AST: %+v", index, err)
return
}
if fail9 && err != nil {
// TODO: %+v instead?
s := fmt.Sprintf("%s", err) // convert to string
if s != expstr {
t.Errorf("test #%d: FAIL", index)
t.Errorf("test #%d: expected different error", index)
t.Logf("test #%d: err: %s", index, s)
t.Logf("test #%d: exp: %s", index, expstr)
}
return // fail happened during lex parse, don't run init/interpolate!
}
if fail9 && err == nil {
t.Errorf("test #%d: FAIL", index)
t.Errorf("test #%d: Init passed, expected fail", index)
return
}
iast, err := ast.Interpolate()
if err != nil {