diff options
author | Nadeem Vawda <nadeem.vawda@gmail.com> | 2013-12-04 22:01:15 (GMT) |
---|---|---|
committer | Nadeem Vawda <nadeem.vawda@gmail.com> | 2013-12-04 22:01:15 (GMT) |
commit | 1de19ac7dab2f75a5886656b7272c6fb009ddd2d (patch) | |
tree | 15ed9a2a35dd50f51579f9a00ab457f1749660f0 /Lib/bz2.py | |
parent | a5cc9d68b93bf5fc4dc2c72eee6b2c3ae01abc65 (diff) | |
download | cpython-1de19ac7dab2f75a5886656b7272c6fb009ddd2d.zip cpython-1de19ac7dab2f75a5886656b7272c6fb009ddd2d.tar.gz cpython-1de19ac7dab2f75a5886656b7272c6fb009ddd2d.tar.bz2 |
#19839: Fix regression in bz2 module's handling of non-bzip2 data at EOF.
Diffstat (limited to 'Lib/bz2.py')
-rw-r--r-- | Lib/bz2.py | 29 |
1 files changed, 19 insertions, 10 deletions
@@ -199,8 +199,15 @@ class BZ2File(io.BufferedIOBase): # Continue to next stream. if self._decompressor.eof: self._decompressor = BZ2Decompressor() - - self._buffer = self._decompressor.decompress(rawblock) + try: + self._buffer = self._decompressor.decompress(rawblock) + except OSError: + # Trailing data isn't a valid bzip2 stream. We're done here. + self._mode = _MODE_READ_EOF + self._size = self._pos + return False + else: + self._buffer = self._decompressor.decompress(rawblock) self._buffer_offset = 0 return True @@ -488,17 +495,19 @@ def decompress(data): For incremental decompression, use a BZ2Decompressor object instead. """ - if len(data) == 0: - return b"" - results = [] - while True: + while data: decomp = BZ2Decompressor() - results.append(decomp.decompress(data)) + try: + res = decomp.decompress(data) + except OSError: + if results: + break # Leftover data is not a valid bzip2 stream; ignore it. + else: + raise # Error on the first iteration; bail out. + results.append(res) if not decomp.eof: raise ValueError("Compressed data ended before the " "end-of-stream marker was reached") - if not decomp.unused_data: - return b"".join(results) - # There is unused data left over. Proceed to next stream. data = decomp.unused_data + return b"".join(results) |