Skip to content

Commit e13eb4e

Browse files
Ignore empty hostnames
1 parent 4225997 commit e13eb4e

3 files changed

Lines changed: 16 additions & 6 deletions

File tree

filter.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -425,7 +425,7 @@ func asSRVMatchers(matcherStrings []string) (srvMatchers, error) {
425425
return nil, fmt.Errorf("parse port %q in service %q", portStr, m)
426426
}
427427

428-
matchers = append(matchers, &srvMatcher{Service: strings.ToLower(service), Port: uint16(port)}) //nolint:gosec
428+
matchers = append(matchers, &srvMatcher{Service: strings.ToLower(service), Port: uint16(port)})
429429
default:
430430
return nil, fmt.Errorf("SRV matcher contains more than one colon: %q", m)
431431
}

hostinfo/host_info.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -163,11 +163,7 @@ func (c *Cache) toIPv4(ip net.IP) net.IP {
163163
return ipv4
164164
}
165165

166-
if ipv4 == nil {
167-
return c.lookupUsingExternalHostnames(ip, mac)
168-
}
169-
170-
return ipv4
166+
return c.lookupUsingExternalHostnames(ip, mac)
171167
}
172168

173169
func (c *Cache) lookupUsingExternalHostnames(ip net.IP, mac net.HardwareAddr) net.IP {
@@ -315,6 +311,10 @@ func appendUnique(oldElements []string, newElements ...string) []string {
315311
}
316312

317313
for _, el := range newElements {
314+
if strings.TrimSpace(el) == "" {
315+
continue
316+
}
317+
318318
el := strings.TrimRight(el, ".")
319319

320320
_, ok := present[el]

hostinfo/host_info_test.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,16 @@ func TestCacheHostInfos(t *testing.T) {
4747
"192.168.56.100", "test")
4848
}
4949

50+
func TestAddHostnamesIgnoreEmpty(t *testing.T) {
51+
c := NewCache()
52+
53+
ipv4 := mustParseIP(t, "192.168.56.101")
54+
55+
c.AddHostnamesForIP(ipv4, []string{"foo", "", "bar", " "})
56+
57+
assertContainsExactly(t, c.HostInfos(ipv4), "foo", "bar")
58+
}
59+
5060
func TestCacheSaveMACFromIP(t *testing.T) {
5161
c := NewCache()
5262

0 commit comments

Comments
 (0)