Skip to content

Commit 2458cad

Browse files
Fix port string to uint16 parsing (#6291)
1 parent 78eb2c8 commit 2458cad

File tree

2 files changed

+41
-1
lines changed

2 files changed

+41
-1
lines changed

common/peerprovider/ringpopprovider/provider.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -294,7 +294,7 @@ func (r *Provider) Subscribe(name string, notifyChannel chan<- *membership.Chang
294294
}
295295

296296
func labelToPort(label string) (uint16, error) {
297-
port, err := strconv.ParseInt(label, 0, 16)
297+
port, err := strconv.ParseUint(label, 10, 16)
298298
if err != nil {
299299
return 0, err
300300
}

common/peerprovider/ringpopprovider/provider_test.go

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,3 +132,43 @@ func (c *TestRingpopCluster) FindHostByAddr(addr string) (HostInfo, bool) {
132132
}
133133
return HostInfo{}, false
134134
}
135+
136+
func TestLabelToPort(t *testing.T) {
137+
tests := []struct {
138+
label string
139+
want uint16
140+
wantErr bool
141+
}{
142+
{
143+
label: "0",
144+
want: 0,
145+
},
146+
{
147+
label: "1234",
148+
want: 1234,
149+
},
150+
{
151+
label: "-1",
152+
wantErr: true,
153+
},
154+
{
155+
label: "32768",
156+
want: 32768,
157+
},
158+
{
159+
label: "65535",
160+
want: 65535,
161+
},
162+
{
163+
label: "65536", // greater than max uint16 (2^16-1)
164+
wantErr: true,
165+
},
166+
}
167+
168+
for _, tc := range tests {
169+
got, err := labelToPort(tc.label)
170+
if got != tc.want || (err != nil) != tc.wantErr {
171+
t.Errorf("labelToPort(%v) = %v, %v; want %v, %v", tc.label, got, err, tc.want, tc.wantErr)
172+
}
173+
}
174+
}

0 commit comments

Comments
 (0)