From ed7d4b3b12e9ccc01b559f9e1b82d3b31f6fe668 Mon Sep 17 00:00:00 2001 From: Jeromy Date: Mon, 24 Oct 2016 15:31:20 -0700 Subject: [PATCH] guard against empty strings --- multibase.go | 4 ++++ multibase_test.go | 5 +++++ 2 files changed, 9 insertions(+) diff --git a/multibase.go b/multibase.go index 8f1ce04..20d254c 100644 --- a/multibase.go +++ b/multibase.go @@ -31,6 +31,10 @@ func Encode(base int, data []byte) (string, error) { } func Decode(data string) (int, []byte, error) { + if len(data) == 0 { + return 0, nil, fmt.Errorf("cannot decode multibase for zero length string") + } + switch data[0] { case Base58BTC: return Base58BTC, b58.DecodeAlphabet(data[1:], b58.BTCAlphabet), nil diff --git a/multibase_test.go b/multibase_test.go index 5bb001e..8b8b6f1 100644 --- a/multibase_test.go +++ b/multibase_test.go @@ -27,4 +27,9 @@ func TestBase58RoundTrip(t *testing.T) { if !bytes.Equal(buf, out) { t.Fatal("input wasnt the same as output", buf, out) } + + _, _, err = Decode("") + if err == nil { + t.Fatal("shouldnt be able to decode empty string") + } }