mem: stricter handling of out of bounds requests

This commit is contained in:
Michael Eischer 2024-05-11 00:12:13 +02:00
parent d40f23e716
commit cfc420664a
2 changed files with 9 additions and 4 deletions

View File

@ -96,7 +96,7 @@ func TestDry(t *testing.T) {
}
case "load":
data := ""
err = step.be.Load(ctx, handle, 100, 0, func(rd io.Reader) error {
err = step.be.Load(ctx, handle, 0, 0, func(rd io.Reader) error {
buf, err := io.ReadAll(rd)
data = string(buf)
return err

View File

@ -43,6 +43,7 @@ func NewFactory() location.Factory {
}
var errNotFound = fmt.Errorf("not found")
var errTooSmall = errors.New("access beyond end of file")
const connectionCount = 2
@ -69,6 +70,10 @@ func (be *MemoryBackend) IsNotExist(err error) bool {
return errors.Is(err, errNotFound)
}
func (be *MemoryBackend) IsPermanentError(err error) bool {
return be.IsNotExist(err) || errors.Is(err, errTooSmall)
}
// Save adds new Data to the backend.
func (be *MemoryBackend) Save(ctx context.Context, h backend.Handle, rd backend.RewindReader) error {
be.m.Lock()
@ -131,12 +136,12 @@ func (be *MemoryBackend) openReader(ctx context.Context, h backend.Handle, lengt
}
buf := be.data[h]
if offset > int64(len(buf)) {
return nil, errors.New("offset beyond end of file")
if offset+int64(length) > int64(len(buf)) {
return nil, errTooSmall
}
buf = buf[offset:]
if length > 0 && len(buf) > length {
if length > 0 {
buf = buf[:length]
}