From 6ad382b53d1b248bb0da3ba5930c0f63d8055a56 Mon Sep 17 00:00:00 2001 From: Yong Tang Date: Tue, 29 Sep 2026 20:11:44 -0700 Subject: [PATCH] plugin/etcd: Stop target stripping at the end of a name (#8584) Stops stripping when the next-label operation reaches the end sentinel in DNS Signed-off-by: Yong Tang --- plugin/etcd/msg/service.go | 3 +++ plugin/etcd/msg/service_test.go | 22 +++++++++++++++++++++- 2 files changed, 24 insertions(+), 1 deletion(-) diff --git a/plugin/etcd/msg/service.go b/plugin/etcd/msg/service.go index f3ac45ffa..153316a34 100644 --- a/plugin/etcd/msg/service.go +++ b/plugin/etcd/msg/service.go @@ -165,6 +165,9 @@ func targetStrip(name string, targetStrip int) string { offset, end := 0, false for range targetStrip { offset, end = dns.NextLabel(name, offset) + if end { + break + } } if end { // We overshot the name, use the original one. diff --git a/plugin/etcd/msg/service_test.go b/plugin/etcd/msg/service_test.go index 4c997c5bc..cbdf19337 100644 --- a/plugin/etcd/msg/service_test.go +++ b/plugin/etcd/msg/service_test.go @@ -1,6 +1,9 @@ package msg -import "testing" +import ( + "testing" + "time" +) func TestSplit255(t *testing.T) { xs := split255("abc") @@ -29,6 +32,23 @@ func TestSplit255(t *testing.T) { } } +func TestTargetStripLargeValueReturns(t *testing.T) { + const name = "b.z1.test." + done := make(chan string, 1) + go func() { + done <- targetStrip(name, int(^uint(0)>>1)) + }() + + select { + case got := <-done: + if got != name { + t.Fatalf("targetStrip returned %q, want %q", got, name) + } + case <-time.After(time.Second): + t.Fatal("targetStrip did not return after reaching the end of the name") + } +} + func TestGroup(t *testing.T) { // Key are in the wrong order, but for this test it does not matter. sx := Group(