Commit 91752fd5 authored by Jeromy's avatar Jeromy

a bit more cleanup, use a bytes.Buffer instead of appending bytes

parent 9e13209d
package multiaddr package multiaddr
import ( import (
"bytes"
"encoding/base32" "encoding/base32"
"encoding/binary" "encoding/binary"
"fmt" "fmt"
...@@ -16,7 +17,7 @@ func stringToBytes(s string) ([]byte, error) { ...@@ -16,7 +17,7 @@ func stringToBytes(s string) ([]byte, error) {
// consume trailing slashes // consume trailing slashes
s = strings.TrimRight(s, "/") s = strings.TrimRight(s, "/")
b := []byte{} b := new(bytes.Buffer)
sp := strings.Split(s, "/") sp := strings.Split(s, "/")
if sp[0] != "" { if sp[0] != "" {
...@@ -31,7 +32,7 @@ func stringToBytes(s string) ([]byte, error) { ...@@ -31,7 +32,7 @@ func stringToBytes(s string) ([]byte, error) {
if p.Code == 0 { if p.Code == 0 {
return nil, fmt.Errorf("no protocol with name %s", sp[0]) return nil, fmt.Errorf("no protocol with name %s", sp[0])
} }
b = append(b, CodeToVarint(p.Code)...) b.Write(CodeToVarint(p.Code))
sp = sp[1:] sp = sp[1:]
if p.Size == 0 { // no length. if p.Size == 0 { // no length.
...@@ -41,18 +42,19 @@ func stringToBytes(s string) ([]byte, error) { ...@@ -41,18 +42,19 @@ func stringToBytes(s string) ([]byte, error) {
if len(sp) < 1 { if len(sp) < 1 {
return nil, fmt.Errorf("protocol requires address, none given: %s", p.Name) return nil, fmt.Errorf("protocol requires address, none given: %s", p.Name)
} }
a, err := addressStringToBytes(p, sp[0]) a, err := addressStringToBytes(p, sp[0])
if err != nil { if err != nil {
return nil, fmt.Errorf("failed to parse %s: %s %s", p.Name, sp[0], err) return nil, fmt.Errorf("failed to parse %s: %s %s", p.Name, sp[0], err)
} }
b = append(b, a...) b.Write(a)
sp = sp[1:] sp = sp[1:]
} }
return b, nil
return b.Bytes(), nil
} }
func validateBytes(b []byte) (err error) { func validateBytes(b []byte) (err error) {
// panic handler, in case we try accessing bytes incorrectly.
for len(b) > 0 { for len(b) > 0 {
code, n, err := ReadVarintCode(b) code, n, err := ReadVarintCode(b)
b = b[n:] b = b[n:]
...@@ -104,6 +106,10 @@ func bytesToString(b []byte) (ret string, err error) { ...@@ -104,6 +106,10 @@ func bytesToString(b []byte) (ret string, err error) {
return "", err return "", err
} }
if len(b) < size || size < 0 {
return "", fmt.Errorf("invalid value for size")
}
a, err := addressBytesToString(p, b[:size]) a, err := addressBytesToString(p, b[:size])
if err != nil { if err != nil {
return "", err return "", err
...@@ -265,7 +271,7 @@ func addressBytesToString(p Protocol, b []byte) (string, error) { ...@@ -265,7 +271,7 @@ func addressBytesToString(p Protocol, b []byte) (string, error) {
return "", err return "", err
} }
return m.B58String(), nil return m.B58String(), nil
} default:
return "", fmt.Errorf("unknown protocol") return "", fmt.Errorf("unknown protocol")
}
} }
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment