1
0
Fork 0
mirror of https://github.com/borgbackup/borg.git synced 2025-02-23 06:31:58 +00:00

recover_segment: handle too small segment files correctly, see #4272

nothing left to recover there, but at least we must not crash in mmap().
This commit is contained in:
Thomas Waldmann 2019-01-29 08:42:33 +01:00
parent 2bcff382cb
commit 2c94d5ba58

View file

@ -1393,8 +1393,14 @@ def recover_segment(self, segment, filename):
del self.fds[segment]
backup_filename = filename + '.beforerecover'
os.rename(filename, backup_filename)
if os.path.getsize(backup_filename) < MAGIC_LEN + self.header_fmt.size:
# this is either a zero-byte file (which would crash mmap() below) or otherwise
# just too small to be a valid non-empty segment file, so do a shortcut here:
with open(filename, 'wb') as fd:
fd.write(MAGIC)
return
with open(backup_filename, 'rb') as backup_fd:
# note: file must not be 0 size (windows can't create 0 size mapping)
# note: file must not be 0 size or mmap() will crash.
with mmap.mmap(backup_fd.fileno(), 0, access=mmap.ACCESS_READ) as mm:
# memoryview context manager is problematic, see https://bugs.python.org/issue35686
data = memoryview(mm)