Skip to content

Commit

Permalink
seen: verify arrays (#663)
Browse files Browse the repository at this point in the history
Fixes #662
  • Loading branch information
pelletier authored Nov 10, 2021
1 parent 6445159 commit f27a07d
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 9 deletions.
7 changes: 0 additions & 7 deletions internal/ast/ast.go
Original file line number Diff line number Diff line change
Expand Up @@ -136,17 +136,10 @@ func (n *Node) Key() Iterator {
// Guaranteed to be non-nil.
// Panics if not called on a KeyValue node, or if the Children are malformed.
func (n *Node) Value() *Node {
assertKind(KeyValue, *n)
return n.Child()
}

// Children returns an iterator over a node's children.
func (n *Node) Children() Iterator {
return Iterator{node: n.Child()}
}

func assertKind(k Kind, n Node) {
if n.Kind != k {
panic(fmt.Errorf("method was expecting a %s, not a %s", k, n.Kind))
}
}
35 changes: 33 additions & 2 deletions internal/tracker/seen.go
Original file line number Diff line number Diff line change
Expand Up @@ -232,16 +232,47 @@ func (s *SeenTracker) checkKeyValue(parentIdx int, node *ast.Node) error {
kind := valueKind
var err error

if node.Value().Kind == ast.InlineTable {
value := node.Value()

switch value.Kind {
case ast.InlineTable:
kind = tableKind
err = s.checkInlineTable(parentIdx, node.Value())
err = s.checkInlineTable(parentIdx, value)
case ast.Array:
err = s.checkArray(parentIdx, value)
}

s.entries[parentIdx].kind = kind

return err
}

func (s *SeenTracker) checkArray(parentIdx int, node *ast.Node) error {
set := false
it := node.Children()
for it.Next() {
if set {
s.clear(parentIdx)
}
n := it.Node()
switch n.Kind {
case ast.InlineTable:
err := s.checkInlineTable(parentIdx, n)
if err != nil {
return err
}
set = true
case ast.Array:
err := s.checkArray(parentIdx, n)
if err != nil {
return err
}
set = true
}
}
return nil
}

func (s *SeenTracker) checkInlineTable(parentIdx int, node *ast.Node) error {
it := node.Children()
for it.Next() {
Expand Down
6 changes: 6 additions & 0 deletions unmarshaler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1916,6 +1916,12 @@ func TestIssue658(t *testing.T) {
require.Error(t, err)
}

func TestIssue662(t *testing.T) {
var v map[string]interface{}
err := toml.Unmarshal([]byte("a=[{b=1,b=2}]"), &v)
require.Error(t, err)
}

//nolint:funlen
func TestUnmarshalDecodeErrors(t *testing.T) {
examples := []struct {
Expand Down

0 comments on commit f27a07d

Please sign in to comment.