mirror of
https://github.com/coredns/coredns.git
synced 2025-12-16 15:25:11 -05:00
fix(file): fix data race in tree Elem.Name (#7574)
Eagerly set name in newElem and make Name() read-only to avoid racy lazy writes under concurrent lookups. Add tests for empty-name comparisons and concurrent access to Less/Name(). In addition, regression tests to CloudDNS plugin. Signed-off-by: Ville Vesilehto <ville@vesilehto.fi>
This commit is contained in:
@@ -3,7 +3,10 @@ package tree
|
||||
import (
|
||||
"sort"
|
||||
"strings"
|
||||
"sync"
|
||||
"testing"
|
||||
|
||||
"github.com/miekg/dns"
|
||||
)
|
||||
|
||||
type set []string
|
||||
@@ -78,3 +81,41 @@ Tests:
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestLess_EmptyVsName(t *testing.T) {
|
||||
if d := less("", "a."); d >= 0 {
|
||||
t.Fatalf("expected < 0, got %d", d)
|
||||
}
|
||||
if d := less("a.", ""); d <= 0 {
|
||||
t.Fatalf("expected > 0, got %d", d)
|
||||
}
|
||||
}
|
||||
|
||||
func TestLess_EmptyVsEmpty(t *testing.T) {
|
||||
if d := less("", ""); d != 0 {
|
||||
t.Fatalf("expected 0, got %d", d)
|
||||
}
|
||||
}
|
||||
|
||||
// Test that concurrent calls to Less (which calls Elem.Name) do not race or panic.
|
||||
// See issue #7561 for reference.
|
||||
func TestLess_ConcurrentNameAccess(t *testing.T) {
|
||||
rr, err := dns.NewRR("a.example. 3600 IN A 1.2.3.4")
|
||||
if err != nil {
|
||||
t.Fatalf("failed to create RR: %v", err)
|
||||
}
|
||||
e := newElem(rr)
|
||||
|
||||
const n = 200
|
||||
var wg sync.WaitGroup
|
||||
wg.Add(n)
|
||||
for range n {
|
||||
go func() {
|
||||
defer wg.Done()
|
||||
// Compare the same name repeatedly; previously this could race due to lazy Name() writes.
|
||||
_ = Less(e, "a.example.")
|
||||
_ = e.Name()
|
||||
}()
|
||||
}
|
||||
wg.Wait()
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user