Skip to content

Commit

Permalink
feat: handle mid-varint EOF case as UnexpectedEOF
Browse files Browse the repository at this point in the history
  • Loading branch information
rvagg committed Nov 5, 2020
1 parent b8a3c26 commit c2f1ff2
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 0 deletions.
65 changes: 65 additions & 0 deletions car_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package car
import (
"bytes"
"context"
"encoding/hex"
"io"
"testing"

cid "github.com/ipfs/go-cid"
Expand Down Expand Up @@ -160,3 +162,66 @@ func TestRoundtripSelective(t *testing.T) {
require.False(t, has)
}
}

func TestEOFHandling(t *testing.T) {
// fixture is a clean single-block, single-root CAR
fixture, err := hex.DecodeString("3aa265726f6f747381d82a58250001711220151fe9e73c6267a7060c6f6c4cca943c236f4b196723489608edb42a8b8fa80b6776657273696f6e012c01711220151fe9e73c6267a7060c6f6c4cca943c236f4b196723489608edb42a8b8fa80ba165646f646779f5")
if err != nil {
t.Fatal(err)
}

load := func(t *testing.T, byts []byte) *CarReader {
cr, err := NewCarReader(bytes.NewReader(byts))
if err != nil {
t.Fatal(err)
}

blk, err := cr.Next()
if err != nil {
t.Fatal(err)
}
if blk.Cid().String() != "bafyreiavd7u6opdcm6tqmddpnrgmvfb4enxuwglhenejmchnwqvixd5ibm" {
t.Fatal("unexpected CID")
}

return cr
}

t.Run("CleanEOF", func(t *testing.T) {
cr := load(t, fixture)

blk, err := cr.Next()
if err != io.EOF {
t.Fatal("Didn't get expected EOF")
}
if blk != nil {
t.Fatal("EOF returned expected block")
}
})

t.Run("BadVarint", func(t *testing.T) {
fixtureBadVarint := append(fixture, 160)
cr := load(t, fixtureBadVarint)

blk, err := cr.Next()
if err != io.ErrUnexpectedEOF {
t.Fatal("Didn't get unexpected EOF")
}
if blk != nil {
t.Fatal("EOF returned unexpected block")
}
})

t.Run("TruncatedBlock", func(t *testing.T) {
fixtureTruncatedBlock := append(fixture, 100, 0, 0)
cr := load(t, fixtureTruncatedBlock)

blk, err := cr.Next()
if err != io.ErrUnexpectedEOF {
t.Fatal("Didn't get unexpected EOF")
}
if blk != nil {
t.Fatal("EOF returned unexpected block")
}
})
}
7 changes: 7 additions & 0 deletions util/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,8 +100,15 @@ func LdSize(d ...[]byte) uint64 {
}

func LdRead(r *bufio.Reader) ([]byte, error) {
if _, err := r.Peek(1); err != nil { // no more blocks, likely clean io.EOF
return nil, err
}

l, err := binary.ReadUvarint(r)
if err != nil {
if err == io.EOF {
return nil, io.ErrUnexpectedEOF // don't silently pretend this is a clean EOF
}
return nil, err
}

Expand Down

0 comments on commit c2f1ff2

Please sign in to comment.