guard against empty strings
This commit is contained in:
@@ -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
|
||||
|
||||
@@ -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")
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user