Skip to content

Commit 16bc574

Browse files
authored
p2p/dnsdisc: fix crash when iterator closed before first call to Next (#22906)
1 parent 3e79588 commit 16bc574

File tree

2 files changed

+21
-0
lines changed

2 files changed

+21
-0
lines changed

p2p/dnsdisc/client.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -298,6 +298,12 @@ func (it *randomIterator) pickTree() *clientTree {
298298
it.mu.Lock()
299299
defer it.mu.Unlock()
300300

301+
// First check if iterator was closed.
302+
// Need to do this here to avoid nil map access in rebuildTrees.
303+
if it.trees == nil {
304+
return nil
305+
}
306+
301307
// Rebuild the trees map if any links have changed.
302308
if it.lc.changed {
303309
it.rebuildTrees()

p2p/dnsdisc/client_test.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,21 @@ func TestIterator(t *testing.T) {
115115
checkIterator(t, it, nodes)
116116
}
117117

118+
func TestIteratorCloseWithoutNext(t *testing.T) {
119+
tree1, url1 := makeTestTree("t1", nil, nil)
120+
c := NewClient(Config{Resolver: newMapResolver(tree1.ToTXT("t1"))})
121+
it, err := c.NewIterator(url1)
122+
if err != nil {
123+
t.Fatal(err)
124+
}
125+
126+
it.Close()
127+
ok := it.Next()
128+
if ok {
129+
t.Fatal("Next returned true after Close")
130+
}
131+
}
132+
118133
// This test checks if closing randomIterator races.
119134
func TestIteratorClose(t *testing.T) {
120135
nodes := testNodes(nodesSeed1, 500)

0 commit comments

Comments
 (0)