]> granicus.if.org Git - python/commitdiff
Issue #1159051: GzipFile now raises EOFError when reading a corrupted file
authorSerhiy Storchaka <storchaka@gmail.com>
Tue, 22 Jan 2013 15:11:07 +0000 (17:11 +0200)
committerSerhiy Storchaka <storchaka@gmail.com>
Tue, 22 Jan 2013 15:11:07 +0000 (17:11 +0200)
with truncated header or footer.
Added tests for reading truncated gzip, bzip2, and lzma files.

1  2 
Lib/gzip.py
Lib/test/test_bz2.py
Misc/NEWS

diff --cc Lib/gzip.py
index 698f0c2747ebdc69542febf00ab37051bc7e84bb,d7da02ca1bec51df33ba074a80479795f460a60e..d5b5743ff62b85ee032caf5334c87acda75620fe
@@@ -284,18 -291,14 +291,14 @@@ class GzipFile(io.BufferedIOBase)
      def _read_gzip_header(self):
          magic = self.fileobj.read(2)
          if magic == b'':
-             raise EOFError("Reached EOF")
+             return False
  
          if magic != b'\037\213':
 -            raise IOError('Not a gzipped file')
 +            raise OSError('Not a gzipped file')
-         method = ord( self.fileobj.read(1) )
+         method, flag, self.mtime = struct.unpack("<BBIxx", self._read_exact(8))
          if method != 8:
 -            raise IOError('Unknown compression method')
 +            raise OSError('Unknown compression method')
-         flag = ord( self.fileobj.read(1) )
-         self.mtime = read32(self.fileobj)
-         # extraflag = self.fileobj.read(1)
-         # os = self.fileobj.read(1)
-         self.fileobj.read(2)
  
          if flag & FEXTRA:
              # Read & discard the extra field, if present
          # We check the that the computed CRC and size of the
          # uncompressed data matches the stored values.  Note that the size
          # stored is the true file size mod 2**32.
-         crc32 = read32(self.fileobj)
-         isize = read32(self.fileobj)  # may exceed 2GB
+         crc32, isize = struct.unpack("<II", self._read_exact(8))
          if crc32 != self.crc:
 -            raise IOError("CRC check failed %s != %s" % (hex(crc32),
 +            raise OSError("CRC check failed %s != %s" % (hex(crc32),
                                                           hex(self.crc)))
          elif isize != (self.size & 0xffffffff):
 -            raise IOError("Incorrect length of data produced")
 +            raise OSError("Incorrect length of data produced")
  
          # Gzip files can be padded with zeroes and still have archives.
          # Consume all zero bytes and set the file position to the first
Simple merge
diff --cc Misc/NEWS
Simple merge