guard against empty strings

This commit is contained in:
Jeromy
2016-10-24 15:31:20 -07:00
parent 2c9be8ec36
commit ed7d4b3b12
2 changed files with 9 additions and 0 deletions

View File

@@ -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

View File

@@ -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")
}
}