fs/object: fix memory object out of bounds Seek

This commit is contained in:
Nick Craig-Wood
2025-02-28 17:01:00 +00:00
parent 64b3d1d539
commit ccef29bbff
2 changed files with 29 additions and 4 deletions

View File

@@ -279,19 +279,26 @@ func (o *MemoryObject) SetModTime(ctx context.Context, modTime time.Time) error
// Open opens the file for read. Call Close() on the returned io.ReadCloser
func (o *MemoryObject) Open(ctx context.Context, options ...fs.OpenOption) (io.ReadCloser, error) {
content := o.content
var offset, limit int64 = 0, -1
for _, option := range options {
switch x := option.(type) {
case *fs.RangeOption:
content = o.content[x.Start:x.End]
offset, limit = x.Decode(o.Size())
case *fs.SeekOption:
content = o.content[x.Offset:]
offset = x.Offset
default:
if option.Mandatory() {
fs.Logf(o, "Unsupported mandatory option: %v", option)
}
}
}
content := o.content
offset = max(offset, 0)
if limit < 0 {
content = content[offset:]
} else {
content = content[offset:min(offset+limit, int64(len(content)))]
}
return io.NopCloser(bytes.NewBuffer(content)), nil
}