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

Fix port string to uint16 parsing #6291

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion common/peerprovider/ringpopprovider/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -294,7 +294,7 @@ func (r *Provider) Subscribe(name string, notifyChannel chan<- *membership.Chang
}

func labelToPort(label string) (uint16, error) {
port, err := strconv.ParseInt(label, 0, 16)
port, err := strconv.ParseUint(label, 10, 16)
if err != nil {
return 0, err
}
Expand Down
40 changes: 40 additions & 0 deletions common/peerprovider/ringpopprovider/provider_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -132,3 +132,43 @@ func (c *TestRingpopCluster) FindHostByAddr(addr string) (HostInfo, bool) {
}
return HostInfo{}, false
}

func TestLabelToPort(t *testing.T) {
tests := []struct {
taylanisikdemir marked this conversation as resolved.
Show resolved Hide resolved
label string
want uint16
wantErr bool
}{
{
label: "0",
want: 0,
},
{
label: "1234",
want: 1234,
},
{
label: "-1",
wantErr: true,
},
{
label: "32768",
want: 32768,
},
{
label: "65535",
want: 65535,
},
{
label: "65536", // greater than max uint16 (2^16-1)
wantErr: true,
},
}

for _, tc := range tests {
got, err := labelToPort(tc.label)
if got != tc.want || (err != nil) != tc.wantErr {
t.Errorf("labelToPort(%v) = %v, %v; want %v, %v", tc.label, got, err, tc.want, tc.wantErr)
}
}
}
Loading