diff --git a/decoder_native_test.go b/decoder_native_test.go index 398bed1..c8e5e55 100644 --- a/decoder_native_test.go +++ b/decoder_native_test.go @@ -359,6 +359,20 @@ func TestDecoder_String(t *testing.T) { assert.Equal(t, "foo", str) } +func TestDecoder_StringEOF(t *testing.T) { + defer ConfigTeardown() + + data := []byte{0x08} + schema := "string" + dec, err := avro.NewDecoder(schema, bytes.NewReader(data)) + require.NoError(t, err) + + var str string + err = dec.Decode(&str) + + require.Error(t, err) +} + func TestDecoder_StringInvalidSchema(t *testing.T) { defer ConfigTeardown() diff --git a/decoder_test.go b/decoder_test.go index bda9d27..ac59d95 100644 --- a/decoder_test.go +++ b/decoder_test.go @@ -68,7 +68,7 @@ func TestDecoder_DecodeNilPtr(t *testing.T) { assert.Error(t, err) } -func TestDecoder_DecodeEOFDoesntReturnError(t *testing.T) { +func TestDecoder_DecodeEOF(t *testing.T) { defer ConfigTeardown() data := []byte{0xE2} @@ -78,7 +78,7 @@ func TestDecoder_DecodeEOFDoesntReturnError(t *testing.T) { var i int err := dec.Decode(&i) - assert.NoError(t, err) + assert.Error(t, err) } func TestUnmarshal(t *testing.T) { @@ -87,7 +87,7 @@ func TestUnmarshal(t *testing.T) { schema := avro.MustParse("int") var i int - err := avro.Unmarshal(schema, []byte{0xE2}, &i) + err := avro.Unmarshal(schema, []byte{0x13}, &i) assert.NoError(t, err) } diff --git a/reader.go b/reader.go index 698a684..e6b4ca3 100644 --- a/reader.go +++ b/reader.go @@ -117,6 +117,7 @@ func (r *Reader) Read(b []byte) { for read < size { if r.head == r.tail { if !r.loadMore() { + r.Error = io.ErrUnexpectedEOF return } } @@ -138,6 +139,8 @@ func (r *Reader) ReadBool() bool { } // ReadInt reads an Int from the Reader. +// +//nolint:dupl // Not the same as ReadLong func (r *Reader) ReadInt() int32 { if r.Error != nil { return 0 @@ -174,12 +177,15 @@ func (r *Reader) ReadInt() int32 { // We ran out of buffer and are not at the end of the int, // Read more into the buffer. if !r.loadMore() { + r.Error = io.ErrUnexpectedEOF return 0 } } } // ReadLong reads a Long from the Reader. +// +//nolint:dupl // Not the same as ReadInt func (r *Reader) ReadLong() int64 { if r.Error != nil { return 0 @@ -216,6 +222,7 @@ func (r *Reader) ReadLong() int64 { // We ran out of buffer and are not at the end of the long, // Read more into the buffer. if !r.loadMore() { + r.Error = io.ErrUnexpectedEOF return 0 } } @@ -247,6 +254,7 @@ func (r *Reader) ReadBytes() []byte { // ReadString reads a String from the Reader. func (r *Reader) ReadString() string { b := r.readBytes("string") + if len(b) == 0 { return "" } @@ -284,6 +292,7 @@ func (r *Reader) readBytes(op string) []byte { } buf := make([]byte, size) + r.Read(buf) return buf }