diff --git a/server/wal/codec/v2.go b/server/wal/codec/v2.go index 3cd9e911..187cb60d 100644 --- a/server/wal/codec/v2.go +++ b/server/wal/codec/v2.go @@ -205,7 +205,7 @@ func (v *V2) RecoverIndex(buf []byte, startFileOffset uint32, baseEntryOffset in index = BorrowEmptyIndexBuf() - for newFileOffset < maxSize { + for newFileOffset+v.HeaderSize <= maxSize { var payloadSize uint32 var payloadCrc uint32 var err error diff --git a/server/wal/codec/v2_test.go b/server/wal/codec/v2_test.go index 3e06eec4..f5ba2d50 100644 --- a/server/wal/codec/v2_test.go +++ b/server/wal/codec/v2_test.go @@ -319,3 +319,14 @@ func TestV2_ReadWithValidation(t *testing.T) { assert.NoError(t, err) assert.EqualValues(t, wPayloadCrc, rPayloadCrc) } + +func TestV2_RecoveryWithNotEnoughBuf(t *testing.T) { + buf := make([]byte, 16) + payloadSize := uint32(len(buf)) - v2.HeaderSize - 1 + payload := bytes.Repeat([]byte("A"), int(payloadSize)) + _, wPayloadCrc := v2.WriteRecord(buf, 0, 0, payload) + _, rLastCrc, _, entryOffset, err := v2.RecoverIndex(buf, 0, 0, nil) + assert.NoError(t, err) + assert.EqualValues(t, wPayloadCrc, rLastCrc) + assert.EqualValues(t, entryOffset, 0) +}