Skip to content

Commit 15a2736

Browse files
committed
Add more check to safely truncate unnecessary data.
Signed-off-by: Kohei Tokunaga <[email protected]>
1 parent 69b6671 commit 15a2736

File tree

2 files changed

+15
-3
lines changed

2 files changed

+15
-3
lines changed

crfs.go

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1060,9 +1060,18 @@ func (h *nodeHandle) Read(ctx context.Context, req *fuse.ReadRequest, resp *fuse
10601060
if err != nil {
10611061
return err
10621062
}
1063-
if nr == 0 {
1064-
// Truncate unncessary data
1065-
chunkData = chunkData[req.Offset:]
1063+
if ce.ChunkOffset < req.Offset && req.Offset < ce.ChunkOffset+ce.ChunkSize {
1064+
1065+
// Data between req.Offset and ce.ChunkOffset is not required, so we truncate it
1066+
// If we've already read first nr bytes we also don't need to read it again. It
1067+
// is possible to occur if we failed to copy full chunkData into resp.Data in a
1068+
// last loop and we are reading same chunk again.
1069+
off := req.Offset - ce.ChunkOffset + int64(nr)
1070+
size := int64(len(chunkData))
1071+
if off > size {
1072+
off = size
1073+
}
1074+
chunkData = chunkData[off:]
10661075
}
10671076
n := copy(resp.Data[nr:], chunkData)
10681077
nr += n

stargz/stargz.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -367,6 +367,9 @@ func (r *Reader) ChunkEntryForOffset(name string, offset int64) (e *TOCEntry, ok
367367
}
368368
ents := r.chunks[name]
369369
if len(ents) < 2 {
370+
if offset >= e.ChunkSize {
371+
return nil, false
372+
}
370373
return e, true
371374
}
372375
i := sort.Search(len(ents), func(i int) bool {

0 commit comments

Comments
 (0)