From 91752fd546e12362989c5eb53457e6876830c9b2 Mon Sep 17 00:00:00 2001 From: Jeromy Date: Wed, 4 May 2016 12:11:09 -0700 Subject: [PATCH] a bit more cleanup, use a bytes.Buffer instead of appending bytes --- codec.go | 20 +++++++++++++------- 1 file changed, 13 insertions(+), 7 deletions(-) diff --git a/codec.go b/codec.go index 5193d5b..747f160 100644 --- a/codec.go +++ b/codec.go @@ -1,6 +1,7 @@ package multiaddr import ( + "bytes" "encoding/base32" "encoding/binary" "fmt" @@ -16,7 +17,7 @@ func stringToBytes(s string) ([]byte, error) { // consume trailing slashes s = strings.TrimRight(s, "/") - b := []byte{} + b := new(bytes.Buffer) sp := strings.Split(s, "/") if sp[0] != "" { @@ -31,7 +32,7 @@ func stringToBytes(s string) ([]byte, error) { if p.Code == 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:] if p.Size == 0 { // no length. @@ -41,18 +42,19 @@ func stringToBytes(s string) ([]byte, error) { if len(sp) < 1 { return nil, fmt.Errorf("protocol requires address, none given: %s", p.Name) } + a, err := addressStringToBytes(p, sp[0]) if err != nil { 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:] } - return b, nil + + return b.Bytes(), nil } func validateBytes(b []byte) (err error) { - // panic handler, in case we try accessing bytes incorrectly. for len(b) > 0 { code, n, err := ReadVarintCode(b) b = b[n:] @@ -104,6 +106,10 @@ func bytesToString(b []byte) (ret string, err error) { return "", err } + if len(b) < size || size < 0 { + return "", fmt.Errorf("invalid value for size") + } + a, err := addressBytesToString(p, b[:size]) if err != nil { return "", err @@ -265,7 +271,7 @@ func addressBytesToString(p Protocol, b []byte) (string, error) { return "", err } return m.B58String(), nil + default: + return "", fmt.Errorf("unknown protocol") } - - return "", fmt.Errorf("unknown protocol") }