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

Small fixes to parsing ports #228

Merged
merged 4 commits into from Jan 24, 2024
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions multiaddr_test.go
Expand Up @@ -83,6 +83,7 @@ func TestConstructFails(t *testing.T) {
"/ip4/127.0.0.1/p2p/tcp",
"/unix",
"/ip4/1.2.3.4/tcp/80/unix",
"/ip4/1.2.3.4/tcp/-1",
"/ip4/127.0.0.1/tcp/9090/http/p2p-webcrt-direct",
"/",
"",
Expand Down
35 changes: 16 additions & 19 deletions transcoders.go
Expand Up @@ -132,21 +132,18 @@
var TranscoderPort = NewTranscoderFromFunctions(portStB, portBtS, nil)

func portStB(s string) ([]byte, error) {
i, err := strconv.Atoi(s)
i, err := strconv.ParseUint(s, 10, 16)
if err != nil {
return nil, fmt.Errorf("failed to parse port addr: %s", err)
}
if i >= 65536 {
return nil, fmt.Errorf("failed to parse port addr: %s", "greater than 65536")
}
b := make([]byte, 2)
binary.BigEndian.PutUint16(b, uint16(i))
return b, nil
}

func portBtS(b []byte) (string, error) {
i := binary.BigEndian.Uint16(b)
return strconv.Itoa(int(i)), nil
return strconv.FormatUint(uint64(i), 10), nil
}

var TranscoderOnion = NewTranscoderFromFunctions(onionStB, onionBtS, nil)
Expand All @@ -167,15 +164,12 @@
}

// onion port number
i, err := strconv.Atoi(addr[1])
i, err := strconv.ParseUint(addr[1], 10, 16)
if err != nil {
return nil, fmt.Errorf("failed to parse onion addr: %s", err)
}
if i >= 65536 {
return nil, fmt.Errorf("failed to parse onion addr: %s", "port greater than 65536")
}
if i < 1 {
return nil, fmt.Errorf("failed to parse onion addr: %s", "port less than 1")
if i == 0 {
return nil, fmt.Errorf("failed to parse onion addr: %s", "non-zero port")
}

onionPortBytes := make([]byte, 2)
Expand All @@ -189,7 +183,10 @@
func onionBtS(b []byte) (string, error) {
addr := strings.ToLower(base32.StdEncoding.EncodeToString(b[0:10]))
port := binary.BigEndian.Uint16(b[10:12])
return addr + ":" + strconv.Itoa(int(port)), nil
if port == 0 {
return "", fmt.Errorf("failed to parse onion addr: %s", "non-zero port")
}
return addr + ":" + strconv.FormatUint(uint64(port), 10), nil

Check warning on line 189 in transcoders.go

View check run for this annotation

Codecov / codecov/patch

transcoders.go#L188-L189

Added lines #L188 - L189 were not covered by tests
}

var TranscoderOnion3 = NewTranscoderFromFunctions(onion3StB, onion3BtS, nil)
Expand All @@ -210,15 +207,12 @@
}

// onion port number
i, err := strconv.Atoi(addr[1])
i, err := strconv.ParseUint(addr[1], 10, 16)
if err != nil {
return nil, fmt.Errorf("failed to parse onion addr: %s", err)
}
if i >= 65536 {
return nil, fmt.Errorf("failed to parse onion addr: %s", "port greater than 65536")
}
if i < 1 {
return nil, fmt.Errorf("failed to parse onion addr: %s", "port less than 1")
if i == 0 {
return nil, fmt.Errorf("failed to parse onion addr: %s", "non-zero port")
}

onionPortBytes := make([]byte, 2)
Expand All @@ -232,7 +226,10 @@
func onion3BtS(b []byte) (string, error) {
addr := strings.ToLower(base32.StdEncoding.EncodeToString(b[0:35]))
port := binary.BigEndian.Uint16(b[35:37])
str := addr + ":" + strconv.Itoa(int(port))
if port < 1 {
return "", fmt.Errorf("failed to parse onion addr: %s", "port less than 1")
}
str := addr + ":" + strconv.FormatUint(uint64(port), 10)

Check warning on line 232 in transcoders.go

View check run for this annotation

Codecov / codecov/patch

transcoders.go#L231-L232

Added lines #L231 - L232 were not covered by tests
return str, nil
}

Expand Down