-
Notifications
You must be signed in to change notification settings - Fork 56
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[CN-922] Fix IPv6 address parsing error #964
Conversation
Codecov Report
@@ Coverage Diff @@
## master #964 +/- ##
==========================================
- Coverage 78.33% 78.20% -0.14%
==========================================
Files 385 385
Lines 21184 21406 +222
==========================================
+ Hits 16595 16741 +146
- Misses 3566 3627 +61
- Partials 1023 1038 +15
... and 3 files with indirect coverage changes 📣 We’re building smart automated test selection to slash your CI/CD build times. Learn more |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Some ideas...
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Here are some ideas...
internal/addr.go
Outdated
if i == len(addr)-1 { | ||
return true | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is this check necessary?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It is necessary as we get i+1
as an index of addr
at the rest
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That condition makes the function return true
for 192.168.1.1:
. Is that what was intended?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Returning true
for something like 192.168.1.1
is misleading, and may be an indicator that hasPort
isn't suitable for our case. Maybe moving the code that splits host and port outside of ParseAddr
is better. How about something like:
func ParseAddr(addr string) (string, int, error) {
addr = strings.TrimSpace(addr)
if addr == "" {
return defaultHost, 0, nil
}
host, port, err := splitHostPort(addr)
if err != nil {
return "", 0, ihzerrors.NewClientError("", err, hzerrors.ErrInvalidAddress)
}
if port < 0 || port > 65535 {
return "", 0, ihzerrors.NewClientError("port not in valid range", err, hzerrors.ErrInvalidAddress)
}
return host, port, nil
}
func splitHostPort(addr string) (host string, port int, err error) {
idx := strings.LastIndex(addr, ":")
if idx == -1 {
return addr, 0, nil
}
port, err = strconv.Atoi(addr[idx+1:])
if err != nil {
return "", 0, fmt.Errorf("invalid port")
}
host, _, err = net.SplitHostPort(addr)
if err != nil {
return "", 0, err
}
if host == "" {
host = defaultHost
}
return host, port, nil
}
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What's the panic you're getting?
The following works OK without panics:
func splitHostPort(addr string) (host string, port int, err error) {
idx := strings.LastIndex(addr, ":")
if idx == -1 {
return addr, 0, nil
}
port, err = strconv.Atoi(addr[idx+1:])
if err != nil {
return "", 0, fmt.Errorf("invalid port")
}
host, _, err = net.SplitHostPort(addr)
if err != nil {
return "", 0, err
}
if host == "" {
host = defaultHost
}
return host, port, nil
}
func main() {
host, port, err := splitHostPort("192.168.1.1:")
if err != nil {
log.Fatal(err)
}
fmt.Println(host, port)
}
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It works well, and is better way than checking if address has port separately.
Fixes #963