Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve EOF handling #36

Merged
merged 2 commits into from
Nov 5, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion car/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ var headerCmd = cli.Command{
}
defer fi.Close()

ch, err := car.ReadHeader(bufio.NewReader(fi))
ch, _, err := car.ReadHeader(bufio.NewReader(fi))
if err != nil {
return err
}
Expand Down
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