diff --git a/cid.go b/cid.go index baefae8..1be6544 100644 --- a/cid.go +++ b/cid.go @@ -46,6 +46,10 @@ type Cid struct { } func Decode(v string) (*Cid, error) { + if len(v) < 2 { + return nil, fmt.Errorf("cid too short") + } + if len(v) == 46 && v[:2] == "Qm" { hash, err := mh.FromB58String(v) if err != nil { diff --git a/cid_test.go b/cid_test.go index 20f5c88..48bb1e4 100644 --- a/cid_test.go +++ b/cid_test.go @@ -52,6 +52,13 @@ func TestBasicMarshaling(t *testing.T) { assertEqual(t, cid, out2) } +func TestEmptyString(t *testing.T) { + _, err := Decode("") + if err == nil { + t.Fatal("shouldnt be able to parse an empty cid") + } +} + func TestV0Handling(t *testing.T) { old := "QmdfTbBqBPQ7VNxZEYEj14VmRuZBkqFbiwReogJgS1zR1n" diff --git a/package.json b/package.json index 0a5a114..83beafe 100644 --- a/package.json +++ b/package.json @@ -15,9 +15,9 @@ }, { "author": "whyrusleeping", - "hash": "QmYiTi9mKBMjfiup7na7PhJK7QEZPdMTJenLdgFYVQ2NUv", + "hash": "QmUq3H9YpcPphbRj6ct6rBgBE377A8wANP8zPMRqe1WYbf", "name": "go-multibase", - "version": "0.2.0" + "version": "0.2.1" } ], "gxVersion": "0.8.0",