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

fix: handle short read errors on string #381

Closed
wants to merge 3 commits into from
Closed
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
14 changes: 14 additions & 0 deletions decoder_native_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand Down
6 changes: 3 additions & 3 deletions decoder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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}
Expand All @@ -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) {
Expand All @@ -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)
}
Expand Down
9 changes: 9 additions & 0 deletions reader.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
}
}
Expand Down Expand Up @@ -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 ""
}
Expand Down Expand Up @@ -284,6 +292,7 @@ func (r *Reader) readBytes(op string) []byte {
}

buf := make([]byte, size)

r.Read(buf)
return buf
}
Expand Down
Loading