Skip to content

Commit eae5eef

Browse files
authored
Merge pull request #3 from bill-rich/catch_oob_make
Catch out of bounds slice size
2 parents ab02be3 + 57dc952 commit eae5eef

File tree

1 file changed

+11
-2
lines changed

1 file changed

+11
-2
lines changed

disk_buffer_reader.go

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,9 @@ func (dbr *DiskBufferReader) Read(out []byte) (int, error) {
5454

5555
// Will need the difference of the requested bytes and how many are read.
5656
bytesToRead := int(int64(outLen) + dbr.index - dbr.bytesRead)
57+
if bytesToRead <= 0 || bytesToRead > len(out) {
58+
return 0, fmt.Errorf("unexpected number of new bytes to read. Expected 0 < n <= %d. Got n=%d", len(out), bytesToRead)
59+
}
5760
readerBytes := make([]byte, bytesToRead)
5861

5962
// Read the bytes from the reader.
@@ -144,8 +147,11 @@ func (dbr *DiskBufferReader) Seek(offset int64, whence int) (int64, error) {
144147
trashBytes := make([]byte, 1024)
145148
for {
146149
_, err := dbr.Read(trashBytes)
147-
if errors.Is(err, io.EOF) {
148-
break
150+
if err != nil {
151+
if errors.Is(err, io.EOF) {
152+
break
153+
}
154+
return dbr.index, err
149155
}
150156
}
151157
if dbr.index+offset < 0 {
@@ -161,6 +167,9 @@ func (dbr *DiskBufferReader) Seek(offset int64, whence int) (int64, error) {
161167
// ReadAt reads len(p) bytes into p starting at offset off in the underlying input source.
162168
func (dbr *DiskBufferReader) ReadAt(out []byte, offset int64) (int, error) {
163169
startIndex, err := dbr.Seek(offset, io.SeekStart)
170+
if err != nil {
171+
return 0, err
172+
}
164173
switch {
165174
case startIndex != offset:
166175
return 0, io.EOF

0 commit comments

Comments
 (0)