Skip to content
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

Merged
merged 8 commits into from
Aug 18, 2023
Merged

Conversation

semihbkgr
Copy link
Member

Fixes #963

@semihbkgr semihbkgr requested a review from yuce August 4, 2023 10:51
@codecov-commenter
Copy link

codecov-commenter commented Aug 4, 2023

Codecov Report

Merging #964 (fe8fd67) into master (7ef6939) will decrease coverage by 0.14%.
Report is 1 commits behind head on master.
The diff coverage is 76.21%.

@@            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     
Files Changed Coverage Δ
client_internals_test_utils.go 69.23% <0.00%> (-2.77%) ⬇️
cluster/cloud_config.go 100.00% <ø> (ø)
internal/cluster/connection.go 37.50% <0.00%> (-0.18%) ⬇️
internal/cloud/cloud_discovery.go 53.42% <13.33%> (-14.76%) ⬇️
internal/rest/http_error.go 57.14% <37.50%> (-13.45%) ⬇️
internal/sql/driver/result.go 85.26% <50.00%> (-4.85%) ⬇️
internal/serialization/default_compact_writer.go 83.89% <66.19%> (-10.12%) ⬇️
internal/rest/http_client.go 82.08% <78.94%> (-2.92%) ⬇️
internal/sql/driver/sql_service.go 72.26% <83.33%> (-0.95%) ⬇️
internal/serialization/default_compact_reader.go 86.95% <99.03%> (+2.16%) ⬆️
... and 5 more

... and 3 files with indirect coverage changes

📣 We’re building smart automated test selection to slash your CI/CD build times. Learn more

Copy link
Collaborator

@yuce yuce left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Some ideas...

internal/addr.go Outdated Show resolved Hide resolved
internal/addr.go Outdated Show resolved Hide resolved
internal/addr_test.go Show resolved Hide resolved
internal/addr.go Show resolved Hide resolved
@semihbkgr semihbkgr requested a review from yuce August 16, 2023 07:49
Copy link
Collaborator

@yuce yuce left a 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 Show resolved Hide resolved
internal/addr.go Outdated
Comment on lines 58 to 60
if i == len(addr)-1 {
return true
}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this check necessary?

Copy link
Member Author

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

Copy link
Collaborator

@yuce yuce Aug 18, 2023

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?

Copy link
Collaborator

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 ParseAddris 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
}

Copy link
Collaborator

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)
}

Copy link
Member Author

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.

internal/addr.go Outdated Show resolved Hide resolved
@semihbkgr semihbkgr requested a review from yuce August 17, 2023 14:32
@yuce yuce merged commit cc9175e into hazelcast:master Aug 18, 2023
7 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Error on connection IPv6 cluster
4 participants