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:
Ville Vesilehto
2025-09-29 05:49:47 +03:00
committed by GitHub
parent eafc352f58
commit 70fb03f711
4 changed files with 217 additions and 2 deletions

View File

@@ -0,0 +1,41 @@
package tree
import (
"testing"
"github.com/miekg/dns"
)
// Test that Name() falls back to reading from the stored RRs when the cached name is empty.
func TestElemName_FallbackWhenCachedEmpty(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)
}
// Build via newElem to ensure m is populated
e := newElem(rr)
got := e.Name()
want := "a.example."
if got != want {
t.Fatalf("unexpected name; want %q, got %q", want, got)
}
// clear the cached name
e.name = ""
got = e.Name()
want = "a.example."
if got != want {
t.Fatalf("unexpected name; want %q, got %q", want, got)
}
// clear the map
e.m = make(map[uint16][]dns.RR, 0)
got = e.Name()
want = ""
if got != want {
t.Fatalf("unexpected name after clearing RR map; want %q, got %q", want, got)
}
}