From 2e091a4ee477cf4a707ea1c05cfbf92183cf01ec Mon Sep 17 00:00:00 2001 From: Yanhu007 Date: Tue, 14 Apr 2026 08:39:48 +0800 Subject: [PATCH] fix: prevent panic in getHostByName on DNS lookup failure getHostByName discards the error from net.LookupHost and unconditionally indexes into the result slice. When DNS resolution fails, the slice is nil and rand.Intn(0) panics, crashing the entire program. Return the input hostname unchanged when lookup fails or returns no addresses, instead of panicking. Partially addresses #473 (Finding 3: CWE-476) --- network.go | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/network.go b/network.go index 108d78a9..366f4262 100644 --- a/network.go +++ b/network.go @@ -6,7 +6,9 @@ import ( ) func getHostByName(name string) string { - addrs, _ := net.LookupHost(name) - //TODO: add error handing when release v3 comes out + addrs, err := net.LookupHost(name) + if err != nil || len(addrs) == 0 { + return name + } return addrs[rand.Intn(len(addrs))] }