summaryrefslogtreecommitdiffstats
path: root/Lib/lzma.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/lzma.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/lzma.py')
-rw-r--r--Lib/lzma.py28
1 files changed, 21 insertions, 7 deletions
diff --git a/Lib/lzma.py b/Lib/lzma.py
index 011ad27..f1d3958 100644
--- a/Lib/lzma.py
+++ b/Lib/lzma.py
@@ -225,11 +225,18 @@ class LZMAFile(io.BufferedIOBase):
raise EOFError("Compressed file ended before the "
"end-of-stream marker was reached")
- # Continue to next stream.
if self._decompressor.eof:
+ # Continue to next stream.
self._decompressor = LZMADecompressor(**self._init_args)
-
- self._buffer = self._decompressor.decompress(rawblock)
+ try:
+ self._buffer = self._decompressor.decompress(rawblock)
+ except LZMAError:
+ # Trailing data isn't a valid compressed stream; ignore it.
+ self._mode = _MODE_READ_EOF
+ self._size = self._pos
+ return False
+ else:
+ self._buffer = self._decompressor.decompress(rawblock)
self._buffer_offset = 0
return True
@@ -487,11 +494,18 @@ def decompress(data, format=FORMAT_AUTO, memlimit=None, filters=None):
results = []
while True:
decomp = LZMADecompressor(format, memlimit, filters)
- results.append(decomp.decompress(data))
+ try:
+ res = decomp.decompress(data)
+ except LZMAError:
+ if results:
+ break # Leftover data is not a valid LZMA/XZ stream; ignore it.
+ else:
+ raise # Error on the first iteration; bail out.
+ results.append(res)
if not decomp.eof:
raise LZMAError("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
+ if not data:
+ break
+ return b"".join(results)