Skip to content

Commit 0a3b142

Browse files
rvagghacdias
authored andcommitted
fix: error not as pointer
1 parent 65e3baa commit 0a3b142

File tree

2 files changed

+36
-33
lines changed

2 files changed

+36
-33
lines changed

cid.go

Lines changed: 29 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -42,17 +42,17 @@ type ErrInvalidCid struct {
4242
Err error
4343
}
4444

45-
func (e *ErrInvalidCid) Error() string {
45+
func (e ErrInvalidCid) Error() string {
4646
return fmt.Sprintf("invalid cid: %s", e.Err)
4747
}
4848

49-
func (e *ErrInvalidCid) Unwrap() error {
49+
func (e ErrInvalidCid) Unwrap() error {
5050
return e.Err
5151
}
5252

53-
func (e *ErrInvalidCid) Is(err error) bool {
53+
func (e ErrInvalidCid) Is(err error) bool {
5454
switch err.(type) {
55-
case *ErrInvalidCid:
55+
case ErrInvalidCid, *ErrInvalidCid:
5656
return true
5757
default:
5858
return false
@@ -62,7 +62,7 @@ func (e *ErrInvalidCid) Is(err error) bool {
6262
var (
6363
// ErrCidTooShort means that the cid passed to decode was not long
6464
// enough to be a valid Cid
65-
ErrCidTooShort = &ErrInvalidCid{errors.New("cid too short")}
65+
ErrCidTooShort = ErrInvalidCid{errors.New("cid too short")}
6666

6767
// ErrInvalidEncoding means that selected encoding is not supported
6868
// by this Cid version
@@ -112,10 +112,10 @@ func tryNewCidV0(mhash mh.Multihash) (Cid, error) {
112112
// incorrectly detect it as CidV1 in the Version() method
113113
dec, err := mh.Decode(mhash)
114114
if err != nil {
115-
return Undef, &ErrInvalidCid{err}
115+
return Undef, ErrInvalidCid{err}
116116
}
117117
if dec.Code != mh.SHA2_256 || dec.Length != 32 {
118-
return Undef, &ErrInvalidCid{fmt.Errorf("invalid hash for cidv0 %d-%d", dec.Code, dec.Length)}
118+
return Undef, ErrInvalidCid{fmt.Errorf("invalid hash for cidv0 %d-%d", dec.Code, dec.Length)}
119119
}
120120
return Cid{string(mhash)}, nil
121121
}
@@ -199,7 +199,7 @@ func Parse(v interface{}) (Cid, error) {
199199
case Cid:
200200
return v2, nil
201201
default:
202-
return Undef, &ErrInvalidCid{fmt.Errorf("can't parse %+v as Cid", v2)}
202+
return Undef, ErrInvalidCid{fmt.Errorf("can't parse %+v as Cid", v2)}
203203
}
204204
}
205205

@@ -232,15 +232,15 @@ func Decode(v string) (Cid, error) {
232232
if len(v) == 46 && v[:2] == "Qm" {
233233
hash, err := mh.FromB58String(v)
234234
if err != nil {
235-
return Undef, &ErrInvalidCid{err}
235+
return Undef, ErrInvalidCid{err}
236236
}
237237

238238
return tryNewCidV0(hash)
239239
}
240240

241241
_, data, err := mbase.Decode(v)
242242
if err != nil {
243-
return Undef, &ErrInvalidCid{err}
243+
return Undef, ErrInvalidCid{err}
244244
}
245245

246246
return Cast(data)
@@ -262,7 +262,7 @@ func ExtractEncoding(v string) (mbase.Encoding, error) {
262262
// check encoding is valid
263263
_, err := mbase.NewEncoder(encoding)
264264
if err != nil {
265-
return -1, &ErrInvalidCid{err}
265+
return -1, ErrInvalidCid{err}
266266
}
267267

268268
return encoding, nil
@@ -282,11 +282,11 @@ func ExtractEncoding(v string) (mbase.Encoding, error) {
282282
func Cast(data []byte) (Cid, error) {
283283
nr, c, err := CidFromBytes(data)
284284
if err != nil {
285-
return Undef, &ErrInvalidCid{err}
285+
return Undef, ErrInvalidCid{err}
286286
}
287287

288288
if nr != len(data) {
289-
return Undef, &ErrInvalidCid{fmt.Errorf("trailing bytes in data buffer passed to cid Cast")}
289+
return Undef, ErrInvalidCid{fmt.Errorf("trailing bytes in data buffer passed to cid Cast")}
290290
}
291291

292292
return c, nil
@@ -637,34 +637,34 @@ func PrefixFromBytes(buf []byte) (Prefix, error) {
637637
func CidFromBytes(data []byte) (int, Cid, error) {
638638
if len(data) > 2 && data[0] == mh.SHA2_256 && data[1] == 32 {
639639
if len(data) < 34 {
640-
return 0, Undef, &ErrInvalidCid{fmt.Errorf("not enough bytes for cid v0")}
640+
return 0, Undef, ErrInvalidCid{fmt.Errorf("not enough bytes for cid v0")}
641641
}
642642

643643
h, err := mh.Cast(data[:34])
644644
if err != nil {
645-
return 0, Undef, &ErrInvalidCid{err}
645+
return 0, Undef, ErrInvalidCid{err}
646646
}
647647

648648
return 34, Cid{string(h)}, nil
649649
}
650650

651651
vers, n, err := varint.FromUvarint(data)
652652
if err != nil {
653-
return 0, Undef, &ErrInvalidCid{err}
653+
return 0, Undef, ErrInvalidCid{err}
654654
}
655655

656656
if vers != 1 {
657-
return 0, Undef, &ErrInvalidCid{fmt.Errorf("expected 1 as the cid version number, got: %d", vers)}
657+
return 0, Undef, ErrInvalidCid{fmt.Errorf("expected 1 as the cid version number, got: %d", vers)}
658658
}
659659

660660
_, cn, err := varint.FromUvarint(data[n:])
661661
if err != nil {
662-
return 0, Undef, &ErrInvalidCid{err}
662+
return 0, Undef, ErrInvalidCid{err}
663663
}
664664

665665
mhnr, _, err := mh.MHFromBytes(data[n+cn:])
666666
if err != nil {
667-
return 0, Undef, &ErrInvalidCid{err}
667+
return 0, Undef, ErrInvalidCid{err}
668668
}
669669

670670
l := n + cn + mhnr
@@ -727,32 +727,32 @@ func CidFromReader(r io.Reader) (int, Cid, error) {
727727
// The varint package wants a io.ByteReader, so we must wrap our io.Reader.
728728
vers, err := varint.ReadUvarint(br)
729729
if err != nil {
730-
return len(br.dst), Undef, &ErrInvalidCid{err}
730+
return len(br.dst), Undef, ErrInvalidCid{err}
731731
}
732732

733733
// If we have a CIDv0, read the rest of the bytes and cast the buffer.
734734
if vers == mh.SHA2_256 {
735735
if n, err := io.ReadFull(r, br.dst[1:34]); err != nil {
736-
return len(br.dst) + n, Undef, &ErrInvalidCid{err}
736+
return len(br.dst) + n, Undef, ErrInvalidCid{err}
737737
}
738738

739739
br.dst = br.dst[:34]
740740
h, err := mh.Cast(br.dst)
741741
if err != nil {
742-
return len(br.dst), Undef, &ErrInvalidCid{err}
742+
return len(br.dst), Undef, ErrInvalidCid{err}
743743
}
744744

745745
return len(br.dst), Cid{string(h)}, nil
746746
}
747747

748748
if vers != 1 {
749-
return len(br.dst), Undef, &ErrInvalidCid{fmt.Errorf("expected 1 as the cid version number, got: %d", vers)}
749+
return len(br.dst), Undef, ErrInvalidCid{fmt.Errorf("expected 1 as the cid version number, got: %d", vers)}
750750
}
751751

752752
// CID block encoding multicodec.
753753
_, err = varint.ReadUvarint(br)
754754
if err != nil {
755-
return len(br.dst), Undef, &ErrInvalidCid{err}
755+
return len(br.dst), Undef, ErrInvalidCid{err}
756756
}
757757

758758
// We could replace most of the code below with go-multihash's ReadMultihash.
@@ -763,19 +763,19 @@ func CidFromReader(r io.Reader) (int, Cid, error) {
763763
// Multihash hash function code.
764764
_, err = varint.ReadUvarint(br)
765765
if err != nil {
766-
return len(br.dst), Undef, &ErrInvalidCid{err}
766+
return len(br.dst), Undef, ErrInvalidCid{err}
767767
}
768768

769769
// Multihash digest length.
770770
mhl, err := varint.ReadUvarint(br)
771771
if err != nil {
772-
return len(br.dst), Undef, &ErrInvalidCid{err}
772+
return len(br.dst), Undef, ErrInvalidCid{err}
773773
}
774774

775775
// Refuse to make large allocations to prevent OOMs due to bugs.
776776
const maxDigestAlloc = 32 << 20 // 32MiB
777777
if mhl > maxDigestAlloc {
778-
return len(br.dst), Undef, &ErrInvalidCid{fmt.Errorf("refusing to allocate %d bytes for a digest", mhl)}
778+
return len(br.dst), Undef, ErrInvalidCid{fmt.Errorf("refusing to allocate %d bytes for a digest", mhl)}
779779
}
780780

781781
// Fine to convert mhl to int, given maxDigestAlloc.
@@ -794,15 +794,15 @@ func CidFromReader(r io.Reader) (int, Cid, error) {
794794
if n, err := io.ReadFull(r, br.dst[prefixLength:cidLength]); err != nil {
795795
// We can't use len(br.dst) here,
796796
// as we've only read n bytes past prefixLength.
797-
return prefixLength + n, Undef, &ErrInvalidCid{err}
797+
return prefixLength + n, Undef, ErrInvalidCid{err}
798798
}
799799

800800
// This simply ensures the multihash is valid.
801801
// TODO: consider removing this bit, as it's probably redundant;
802802
// for now, it helps ensure consistency with CidFromBytes.
803803
_, _, err = mh.MHFromBytes(br.dst[mhStart:])
804804
if err != nil {
805-
return len(br.dst), Undef, &ErrInvalidCid{err}
805+
return len(br.dst), Undef, ErrInvalidCid{err}
806806
}
807807

808808
return len(br.dst), Cid{string(br.dst)}, nil

cid_test.go

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -228,7 +228,7 @@ func TestEmptyString(t *testing.T) {
228228
if err == nil {
229229
t.Fatal("shouldnt be able to parse an empty cid")
230230
}
231-
if !errors.Is(err, &ErrInvalidCid{}) {
231+
if !errors.Is(err, ErrInvalidCid{}) {
232232
t.Fatal("error must be ErrInvalidCid")
233233
}
234234
}
@@ -286,7 +286,7 @@ func TestV0ErrorCases(t *testing.T) {
286286
if err == nil {
287287
t.Fatal("should have failed to decode that ref")
288288
}
289-
if !errors.Is(err, &ErrInvalidCid{}) {
289+
if !errors.Is(err, ErrInvalidCid{}) {
290290
t.Fatal("error must be ErrInvalidCid")
291291
}
292292
}
@@ -462,6 +462,9 @@ func TestParse(t *testing.T) {
462462
if !strings.Contains(err.Error(), "can't parse 123 as Cid") {
463463
t.Fatalf("expected int error, got %s", err.Error())
464464
}
465+
if !errors.Is(err, ErrInvalidCid{}) {
466+
t.Fatalf("expected ErrInvalidCid, got %s", err.Error())
467+
}
465468

466469
theHash := "QmdfTbBqBPQ7VNxZEYEj14VmRuZBkqFbiwReogJgS1zR1n"
467470
h, err := mh.FromB58String(theHash)
@@ -756,7 +759,7 @@ func TestBadParse(t *testing.T) {
756759
if err == nil {
757760
t.Fatal("expected to fail to parse an invalid CIDv1 CID")
758761
}
759-
if !errors.Is(err, &ErrInvalidCid{}) {
762+
if !errors.Is(err, ErrInvalidCid{}) {
760763
t.Fatal("error must be ErrInvalidCid")
761764
}
762765
}
@@ -780,7 +783,7 @@ func TestErrInvalidCid(t *testing.T) {
780783
t.Fatal("expected error")
781784
}
782785

783-
is := errors.Is(err, &ErrInvalidCid{})
786+
is := errors.Is(err, ErrInvalidCid{})
784787
if !is {
785788
t.Fatal("expected error to be ErrInvalidCid")
786789
}

0 commit comments

Comments
 (0)