fix for out of range reads
This commit is contained in:
@@ -108,12 +108,10 @@ func (c *ChunkReadAt) doReadAt(p []byte, offset int64) (n int, err error) {
|
|||||||
|
|
||||||
glog.V(4).Infof("doReadAt [%d,%d), n:%v, err:%v", offset, offset+int64(len(p)), n, err)
|
glog.V(4).Infof("doReadAt [%d,%d), n:%v, err:%v", offset, offset+int64(len(p)), n, err)
|
||||||
|
|
||||||
if remaining > 0 {
|
if remaining > 0 && c.fileSize > startOffset {
|
||||||
glog.V(4).Infof("zero2 [%d,%d)", n, n+int(remaining))
|
delta := int(min(remaining, c.fileSize - startOffset))
|
||||||
n += int(remaining)
|
glog.V(4).Infof("zero2 [%d,%d)", n, n+delta)
|
||||||
if n > int(c.fileSize - offset){
|
n += delta
|
||||||
n = int(c.fileSize - offset)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if offset+int64(n) >= c.fileSize {
|
if offset+int64(n) >= c.fileSize {
|
||||||
|
|||||||
@@ -77,6 +77,11 @@ func testReadAt(t *testing.T, readerAt *ChunkReadAt, offset int64, size int, exp
|
|||||||
data := make([]byte, size)
|
data := make([]byte, size)
|
||||||
n, err := readerAt.ReadAt(data, offset)
|
n, err := readerAt.ReadAt(data, offset)
|
||||||
|
|
||||||
|
for _, d := range data {
|
||||||
|
fmt.Printf("%x", d)
|
||||||
|
}
|
||||||
|
fmt.Println()
|
||||||
|
|
||||||
if expected != n {
|
if expected != n {
|
||||||
t.Errorf("unexpected read size: %d, expect: %d", n, expected)
|
t.Errorf("unexpected read size: %d, expect: %d", n, expected)
|
||||||
}
|
}
|
||||||
@@ -84,10 +89,6 @@ func testReadAt(t *testing.T, readerAt *ChunkReadAt, offset int64, size int, exp
|
|||||||
t.Errorf("unexpected read error: %v, expect: %v", err, expectedErr)
|
t.Errorf("unexpected read error: %v, expect: %v", err, expectedErr)
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, d := range data {
|
|
||||||
fmt.Printf("%x", d)
|
|
||||||
}
|
|
||||||
fmt.Println()
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestReaderAt0(t *testing.T) {
|
func TestReaderAt0(t *testing.T) {
|
||||||
@@ -119,4 +120,6 @@ func TestReaderAt0(t *testing.T) {
|
|||||||
testReadAt(t, readerAt, 3, 16, 7, io.EOF)
|
testReadAt(t, readerAt, 3, 16, 7, io.EOF)
|
||||||
testReadAt(t, readerAt, 3, 5, 5, nil)
|
testReadAt(t, readerAt, 3, 5, 5, nil)
|
||||||
|
|
||||||
|
testReadAt(t, readerAt, 11, 5, 0, io.EOF)
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user