graph/iterator: guard against iteration panic
The reflect package panics when any of Key, Value or Next are called
after Next returns false. We don't do that in the other iterators, so
we shouldn't do it here.
MODIFY
graph/iterator/nodes_unordered.go
graph/iterator/nodes_unordered.go
@@ -41,6 +41,9 @@ func (n *Nodes) Len() int {
// Next returns whether the next call of Node will return a valid node.
func (n *Nodes) Next() bool {
+ if n.pos >= n.nodes.Len() {
+ return false
+ }
ok := n.iter.Next()
if ok {
n.pos++

MODIFY
graph/iterator/nodes_unordered_test.go
graph/iterator/nodes_unordered_test.go
@@ -49,6 +49,15 @@ func TestNodesIterate(t *testing.T) {
if !reflect.DeepEqual(got, want) {
t.Errorf("unexpected iterator output for round %d: got:%#v want:%#v", i, got, want)
}
+ func() {
+ defer func() {
+ r := recover()
+ if r != nil {
+ t.Errorf("unexpected panic: %v", r)
+ }
+ }()
+ it.Next()
+ }()
it.Reset()
}
}

graph/iterator: guard against iteration panic

The reflect package panics when any of Key, Value or Next are called
after Next returns false. We don't do that in the other iterators, so
we shouldn't do it here.