Skip to content

Commit

Permalink
p2p/dnsdisc: fix crash when iterator closed before first call to Next (
Browse files Browse the repository at this point in the history
  • Loading branch information
fjl authored May 20, 2021
1 parent 3e79588 commit 16bc574
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 0 deletions.
6 changes: 6 additions & 0 deletions p2p/dnsdisc/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -298,6 +298,12 @@ func (it *randomIterator) pickTree() *clientTree {
it.mu.Lock()
defer it.mu.Unlock()

// First check if iterator was closed.
// Need to do this here to avoid nil map access in rebuildTrees.
if it.trees == nil {
return nil
}

// Rebuild the trees map if any links have changed.
if it.lc.changed {
it.rebuildTrees()
Expand Down
15 changes: 15 additions & 0 deletions p2p/dnsdisc/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,21 @@ func TestIterator(t *testing.T) {
checkIterator(t, it, nodes)
}

func TestIteratorCloseWithoutNext(t *testing.T) {
tree1, url1 := makeTestTree("t1", nil, nil)
c := NewClient(Config{Resolver: newMapResolver(tree1.ToTXT("t1"))})
it, err := c.NewIterator(url1)
if err != nil {
t.Fatal(err)
}

it.Close()
ok := it.Next()
if ok {
t.Fatal("Next returned true after Close")
}
}

// This test checks if closing randomIterator races.
func TestIteratorClose(t *testing.T) {
nodes := testNodes(nodesSeed1, 500)
Expand Down

0 comments on commit 16bc574

Please sign in to comment.