summaryrefslogtreecommitdiffstats
path: root/Lib/bz2.py
diff options
context:
space:
mode:
authorNadeem Vawda <nadeem.vawda@gmail.com>2013-12-04 22:29:51 (GMT)
committerNadeem Vawda <nadeem.vawda@gmail.com>2013-12-04 22:29:51 (GMT)
commitae02d07bc2939f2f9e7a82543be3bb832ad8b29d (patch)
tree07ebcbf83fc7170a47c95677d76e7febe5837091 /Lib/bz2.py
parentf9c54944dfe2f2594748a0a92938e4765c0c5900 (diff)
parent9c72ebc96be44c4ff66832cbe5e131065ae9d95d (diff)
downloadcpython-ae02d07bc2939f2f9e7a82543be3bb832ad8b29d.zip
cpython-ae02d07bc2939f2f9e7a82543be3bb832ad8b29d.tar.gz
cpython-ae02d07bc2939f2f9e7a82543be3bb832ad8b29d.tar.bz2
Closes #19839: Fix regression in bz2 module's handling of non-bzip2 data at EOF.
Also fix an analogous bug (not a regression) in the lzma module.
Diffstat (limited to 'Lib/bz2.py')
-rw-r--r--Lib/bz2.py29
1 files changed, 19 insertions, 10 deletions
diff --git a/Lib/bz2.py b/Lib/bz2.py
index 6bc611e..6f47bfa 100644
--- a/Lib/bz2.py
+++ b/Lib/bz2.py
@@ -207,8 +207,15 @@ class BZ2File(io.BufferedIOBase):
if self._decompressor.eof:
# Continue to next stream.
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
@@ -496,17 +503,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)