Skip to content

Commit 891bfa5

Browse files
committed
Properly handle empty address string instead of panic
1 parent 7672f86 commit 891bfa5

File tree

2 files changed

+5
-0
lines changed

2 files changed

+5
-0
lines changed

ntp.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -648,6 +648,10 @@ func dialWrapper(la, ra string,
648648
// fixHostPort examines an address in one of the accepted forms and fixes it
649649
// to include a port number if necessary.
650650
func fixHostPort(address string, defaultPort int) (fixed string, err error) {
651+
if len(address) == 0 {
652+
return "", errors.New("address string is empty")
653+
}
654+
651655
// If the address is wrapped in brackets, append a port if necessary.
652656
if address[0] == '[' {
653657
end := strings.IndexByte(address, ']')

ntp_test.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -270,6 +270,7 @@ func TestOfflineFixHostPort(t *testing.T) {
270270
{"[::ffff:192.168.1.1]", "[::ffff:192.168.1.1]:123", ""},
271271
{"[::ffff:192.168.1.1]:123", "[::ffff:192.168.1.1]:123", ""},
272272
{"[::ffff:192.168.1.1]:1000", "[::ffff:192.168.1.1]:1000", ""},
273+
{"", "", "address string is empty"},
273274
}
274275
for _, c := range cases {
275276
fixed, err := fixHostPort(c.address, defaultPort)

0 commit comments

Comments
 (0)