Skip to content

Commit

Permalink
Properly handle empty address string instead of panic
Browse files Browse the repository at this point in the history
  • Loading branch information
beevik committed Dec 23, 2023
1 parent 7672f86 commit 891bfa5
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 0 deletions.
4 changes: 4 additions & 0 deletions ntp.go
Original file line number Diff line number Diff line change
Expand Up @@ -648,6 +648,10 @@ func dialWrapper(la, ra string,
// fixHostPort examines an address in one of the accepted forms and fixes it
// to include a port number if necessary.
func fixHostPort(address string, defaultPort int) (fixed string, err error) {
if len(address) == 0 {
return "", errors.New("address string is empty")
}

// If the address is wrapped in brackets, append a port if necessary.
if address[0] == '[' {
end := strings.IndexByte(address, ']')
Expand Down
1 change: 1 addition & 0 deletions ntp_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -270,6 +270,7 @@ func TestOfflineFixHostPort(t *testing.T) {
{"[::ffff:192.168.1.1]", "[::ffff:192.168.1.1]:123", ""},
{"[::ffff:192.168.1.1]:123", "[::ffff:192.168.1.1]:123", ""},
{"[::ffff:192.168.1.1]:1000", "[::ffff:192.168.1.1]:1000", ""},
{"", "", "address string is empty"},
}
for _, c := range cases {
fixed, err := fixHostPort(c.address, defaultPort)
Expand Down

0 comments on commit 891bfa5

Please sign in to comment.