Explorar o código

Merge pull request #4275 from ThomasWaldmann/fix-empty-segment-crash-master

recover_segment: handle too small segment files correctly, see #4272
TW %!s(int64=6) %!d(string=hai) anos
pai
achega
422d9cf170
Modificáronse 1 ficheiros con 7 adicións e 1 borrados
  1. 7 1
      src/borg/repository.py

+ 7 - 1
src/borg/repository.py

@@ -1393,8 +1393,14 @@ class LoggedIO:
             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)