From d1d7feacc94fbc4cc56d565c0b5bfdb75e513c07 Mon Sep 17 00:00:00 2001 From: Xiang Li Date: Thu, 23 Apr 2015 09:53:33 -0700 Subject: [PATCH] wal: change io.EOF returned by readFull to io.ErrUnexpectedEOF Decoder should return error for any broken block including the one that only contains the length field. We should change io.EOF to io.ErrUnexpectedEOF before return the error. --- wal/decoder.go | 5 +++++ wal/record_test.go | 1 + 2 files changed, 6 insertions(+) diff --git a/wal/decoder.go b/wal/decoder.go index a836ccd684a..f75c919fba6 100644 --- a/wal/decoder.go +++ b/wal/decoder.go @@ -54,6 +54,11 @@ func (d *decoder) decode(rec *walpb.Record) error { } data := make([]byte, l) if _, err = io.ReadFull(d.br, data); err != nil { + // ReadFull returns io.EOF only if no bytes were read + // the decoder should treat this as an ErrUnexpectedEOF instead. + if err == io.EOF { + err = io.ErrUnexpectedEOF + } return err } if err := rec.Unmarshal(data); err != nil { diff --git a/wal/record_test.go b/wal/record_test.go index 84b1248baf7..82849c27612 100644 --- a/wal/record_test.go +++ b/wal/record_test.go @@ -42,6 +42,7 @@ func TestReadRecord(t *testing.T) { }{ {infoRecord, &walpb.Record{Type: 1, Crc: crc32.Checksum(infoData, crcTable), Data: infoData}, nil}, {[]byte(""), &walpb.Record{}, io.EOF}, + {infoRecord[:8], &walpb.Record{}, io.ErrUnexpectedEOF}, {infoRecord[:len(infoRecord)-len(infoData)-8], &walpb.Record{}, io.ErrUnexpectedEOF}, {infoRecord[:len(infoRecord)-len(infoData)], &walpb.Record{}, io.ErrUnexpectedEOF}, {infoRecord[:len(infoRecord)-8], &walpb.Record{}, io.ErrUnexpectedEOF},