Skip to content

Commit

Permalink
fix: reader int/long setting head > tail (#385)
Browse files Browse the repository at this point in the history
  • Loading branch information
nrwiersma authored Apr 23, 2024
1 parent 84f9b10 commit 2461d45
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 6 deletions.
16 changes: 10 additions & 6 deletions reader.go
Original file line number Diff line number Diff line change
Expand Up @@ -171,20 +171,22 @@ func (r *Reader) ReadInt() int32 {
}

// Consume what it is in the buffer.
for i, b := range r.buf[r.head:tail] {
var i int
for _, b := range r.buf[r.head:tail] {
v |= uint32(b&0x7f) << s
if b&0x80 == 0 {
r.head += i + 1
return int32((v >> 1) ^ -(v & 1))
}
s += 7
n++
i++
}
if n >= maxIntBufSize {
r.ReportError("ReadInt", "int overflow")
return 0
}
r.head += n
r.head += i
n += i

// We ran out of buffer and are not at the end of the int,
// Read more into the buffer.
Expand Down Expand Up @@ -216,20 +218,22 @@ func (r *Reader) ReadLong() int64 {
}

// Consume what it is in the buffer.
for i, b := range r.buf[r.head:tail] {
var i int
for _, b := range r.buf[r.head:tail] {
v |= uint64(b&0x7f) << s
if b&0x80 == 0 {
r.head += i + 1
return int64((v >> 1) ^ -(v & 1))
}
s += 7
n++
i++
}
if n >= maxLongBufSize {
r.ReportError("ReadLong", "int overflow")
return 0
}
r.head += n
r.head += i
n += i

// We ran out of buffer and are not at the end of the long,
// Read more into the buffer.
Expand Down
24 changes: 24 additions & 0 deletions reader_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -254,6 +254,18 @@ func TestReader_ReadInt(t *testing.T) {
}
}

func TestReader_ReadIntShortReadAcrossBuffer(t *testing.T) {
data := []byte{0xe2, 0xa2, 0xf3, 0xad}
r := avro.NewReader(bytes.NewReader(data), 3)

_ = r.ReadInt()

assert.NotPanics(t, func() {
b := make([]byte, 3)
r.Read(b)
})
}

func TestReader_ReadLong(t *testing.T) {
tests := []struct {
name string
Expand Down Expand Up @@ -359,6 +371,18 @@ func TestReader_ReadLong(t *testing.T) {
}
}

func TestReader_ReadLongShortReadAcrossBuffer(t *testing.T) {
data := []byte{0xfe, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff}
r := avro.NewReader(bytes.NewReader(data), 8)

_ = r.ReadLong()

assert.NotPanics(t, func() {
b := make([]byte, 3)
r.Read(b)
})
}

func TestReader_ReadFloat(t *testing.T) {
tests := []struct {
data []byte
Expand Down

0 comments on commit 2461d45

Please sign in to comment.