diff --git a/internal/cache/file.go b/internal/cache/file.go index 9a5046451..4239a383c 100644 --- a/internal/cache/file.go +++ b/internal/cache/file.go @@ -6,6 +6,7 @@ import ( "path/filepath" "github.com/pkg/errors" + "github.com/restic/restic/internal/crypto" "github.com/restic/restic/internal/debug" "github.com/restic/restic/internal/fs" "github.com/restic/restic/internal/restic" @@ -50,6 +51,18 @@ func (c *Cache) Load(h restic.Handle, length int, offset int64) (io.ReadCloser, return nil, errors.Wrap(err, "Open") } + fi, err := f.Stat() + if err != nil { + _ = f.Close() + return nil, errors.Wrap(err, "Stat") + } + + if fi.Size() <= crypto.Extension { + _ = f.Close() + _ = c.Remove(h) + return nil, errors.New("cached file is truncated, removing") + } + if offset > 0 { if _, err = f.Seek(offset, io.SeekStart); err != nil { f.Close()