Skip to content

Commit

Permalink
readatbuf: add missing check when reading from tail chunk (#259)
Browse files Browse the repository at this point in the history
  • Loading branch information
athre0z authored Nov 29, 2024
1 parent bdecd68 commit cd3963c
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 0 deletions.
3 changes: 3 additions & 0 deletions libpf/readatbuf/readatbuf.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,9 @@ func (reader *Reader) ReadAt(p []byte, off int64) (int, error) {
if err != nil {
return int(writeOffset), err
}
if skipOffset > uint(len(data)) {
return 0, io.EOF
}

copyLen := min(remaining, uint(len(data))-skipOffset)
copy(p[writeOffset:][:copyLen], data[skipOffset:][:copyLen])
Expand Down
16 changes: 16 additions & 0 deletions libpf/readatbuf/readatbuf_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ package readatbuf_test

import (
"bytes"
"io"
"testing"

"github.com/stretchr/testify/require"
Expand All @@ -25,3 +26,18 @@ func TestCaching(t *testing.T) {
testVariant(t, 1346, 11, 55)
testVariant(t, 889, 34, 111)
}

func TestOutOfBoundsTail(t *testing.T) {
buf := bytes.NewReader([]byte{0, 1, 2, 3, 4, 5, 6, 7})
r, err := readatbuf.New(buf, 5, 10)
require.NoError(t, err)
b := make([]byte, 1)
for i := int64(0); i < 32; i++ {
_, err = r.ReadAt(b, i)
if i > 7 {
require.ErrorIs(t, err, io.EOF)
} else {
require.NoError(t, err)
}
}
}

0 comments on commit cd3963c

Please sign in to comment.