Skip to content

Commit

Permalink
Merge pull request #63 from Stebalien/fix/test-bad-cbor
Browse files Browse the repository at this point in the history
fix: don't fail if we try to discard nothing at the end of an object
  • Loading branch information
magik6k authored Mar 2, 2022
2 parents 7a69943 + 207d57c commit 37c43ca
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 0 deletions.
5 changes: 5 additions & 0 deletions utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,11 @@ const maxHeaderSize = 9
// the most common readers we encounter in this library for a significant
// performance boost.
func discard(br io.Reader, n int) error {
// If we're expecting no bytes, don't even try to read. Otherwise, we may read an EOF.
if n == 0 {
return nil
}

switch r := br.(type) {
case *bytes.Buffer:
buf := r.Next(n)
Expand Down
16 changes: 16 additions & 0 deletions utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,22 @@ func TestLinkScan(t *testing.T) {
t.Log(cids)
}

func TestScanForLinksEOFRegression(t *testing.T) {
inp := "82442000000081818242005140"
inpb, err := hex.DecodeString(inp)
if err != nil {
t.Fatal(err)
}

var cids []cid.Cid
if err := ScanForLinks(bytes.NewReader(inpb), func(c cid.Cid) {
cids = append(cids, c)
}); err != nil {
t.Fatal(err)
}
t.Log(cids)
}

func TestDeferredMaxLengthSingle(t *testing.T) {
var header bytes.Buffer
if err := WriteMajorTypeHeader(&header, MajByteString, ByteArrayMaxLen+1); err != nil {
Expand Down

0 comments on commit 37c43ca

Please sign in to comment.