mirror of
				https://github.com/coredns/coredns.git
				synced 2025-10-30 17:53:21 -04: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:
		
							
								
								
									
										41
									
								
								plugin/file/tree/elem_test.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										41
									
								
								plugin/file/tree/elem_test.go
									
									
									
									
									
										Normal 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) | ||||
| 	} | ||||
| } | ||||
		Reference in New Issue
	
	Block a user