From 3c9f29c32287d2af9fe4d66bb0662ae603b43a98 Mon Sep 17 00:00:00 2001 From: Brian Shih Date: Sun, 21 Apr 2024 20:21:00 -0400 Subject: [PATCH 1/3] wrap eof for string --- decoder_native_test.go | 14 ++++++++++++++ reader.go | 4 ++++ 2 files changed, 18 insertions(+) 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/reader.go b/reader.go index 698a684..5d39e89 100644 --- a/reader.go +++ b/reader.go @@ -247,6 +247,10 @@ func (r *Reader) ReadBytes() []byte { // ReadString reads a String from the Reader. func (r *Reader) ReadString() string { b := r.readBytes("string") + if r.Error != nil { + r.Error = fmt.Errorf("reading string: %w", r.Error) + } + if len(b) == 0 { return "" } From 20c4c655102bfab832dd43dc4233a7a40d966ce2 Mon Sep 17 00:00:00 2001 From: Brian Shih Date: Mon, 22 Apr 2024 11:08:18 -0400 Subject: [PATCH 2/3] read ErrUnexpectedEOF --- decoder_test.go | 6 +++--- reader.go | 7 ++++--- 2 files changed, 7 insertions(+), 6 deletions(-) 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 5d39e89..edc1e79 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 } } @@ -174,6 +175,7 @@ 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 } } @@ -216,6 +218,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,9 +250,6 @@ func (r *Reader) ReadBytes() []byte { // ReadString reads a String from the Reader. func (r *Reader) ReadString() string { b := r.readBytes("string") - if r.Error != nil { - r.Error = fmt.Errorf("reading string: %w", r.Error) - } if len(b) == 0 { return "" @@ -288,6 +288,7 @@ func (r *Reader) readBytes(op string) []byte { } buf := make([]byte, size) + r.Read(buf) return buf } From 3b2f6094fa53593f8d980cd8af8fc4873e6d9af0 Mon Sep 17 00:00:00 2001 From: Brian Shih Date: Mon, 22 Apr 2024 11:15:20 -0400 Subject: [PATCH 3/3] nolint on duplicate --- reader.go | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/reader.go b/reader.go index edc1e79..e6b4ca3 100644 --- a/reader.go +++ b/reader.go @@ -139,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 @@ -182,6 +184,8 @@ func (r *Reader) ReadInt() int32 { } // 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