mirror of https://github.com/python/cpython
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.
This commit is contained in:
commit
ae02d07bc2
29
Lib/bz2.py
29
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)
|
||||
|
|
28
Lib/lzma.py
28
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)
|
||||
|
|
|
@ -50,6 +50,7 @@ class BaseTest(unittest.TestCase):
|
|||
TEXT = b''.join(TEXT_LINES)
|
||||
DATA = b'BZh91AY&SY.\xc8N\x18\x00\x01>_\x80\x00\x10@\x02\xff\xf0\x01\x07n\x00?\xe7\xff\xe00\x01\x99\xaa\x00\xc0\x03F\x86\x8c#&\x83F\x9a\x03\x06\xa6\xd0\xa6\x93M\x0fQ\xa7\xa8\x06\x804hh\x12$\x11\xa4i4\xf14S\xd2<Q\xb5\x0fH\xd3\xd4\xdd\xd5\x87\xbb\xf8\x94\r\x8f\xafI\x12\xe1\xc9\xf8/E\x00pu\x89\x12]\xc9\xbbDL\nQ\x0e\t1\x12\xdf\xa0\xc0\x97\xac2O9\x89\x13\x94\x0e\x1c7\x0ed\x95I\x0c\xaaJ\xa4\x18L\x10\x05#\x9c\xaf\xba\xbc/\x97\x8a#C\xc8\xe1\x8cW\xf9\xe2\xd0\xd6M\xa7\x8bXa<e\x84t\xcbL\xb3\xa7\xd9\xcd\xd1\xcb\x84.\xaf\xb3\xab\xab\xad`n}\xa0lh\tE,\x8eZ\x15\x17VH>\x88\xe5\xcd9gd6\x0b\n\xe9\x9b\xd5\x8a\x99\xf7\x08.K\x8ev\xfb\xf7xw\xbb\xdf\xa1\x92\xf1\xdd|/";\xa2\xba\x9f\xd5\xb1#A\xb6\xf6\xb3o\xc9\xc5y\\\xebO\xe7\x85\x9a\xbc\xb6f8\x952\xd5\xd7"%\x89>V,\xf7\xa6z\xe2\x9f\xa3\xdf\x11\x11"\xd6E)I\xa9\x13^\xca\xf3r\xd0\x03U\x922\xf26\xec\xb6\xed\x8b\xc3U\x13\x9d\xc5\x170\xa4\xfa^\x92\xacDF\x8a\x97\xd6\x19\xfe\xdd\xb8\xbd\x1a\x9a\x19\xa3\x80ankR\x8b\xe5\xd83]\xa9\xc6\x08\x82f\xf6\xb9"6l$\xb8j@\xc0\x8a\xb0l1..\xbak\x83ls\x15\xbc\xf4\xc1\x13\xbe\xf8E\xb8\x9d\r\xa8\x9dk\x84\xd3n\xfa\xacQ\x07\xb1%y\xaav\xb4\x08\xe0z\x1b\x16\xf5\x04\xe9\xcc\xb9\x08z\x1en7.G\xfc]\xc9\x14\xe1B@\xbb!8`'
|
||||
EMPTY_DATA = b'BZh9\x17rE8P\x90\x00\x00\x00\x00'
|
||||
BAD_DATA = b'this is not a valid bzip2 file'
|
||||
|
||||
def setUp(self):
|
||||
self.filename = support.TESTFN
|
||||
|
@ -80,9 +81,10 @@ class BaseTest(unittest.TestCase):
|
|||
class BZ2FileTest(BaseTest):
|
||||
"Test the BZ2File class."
|
||||
|
||||
def createTempFile(self, streams=1):
|
||||
def createTempFile(self, streams=1, suffix=b""):
|
||||
with open(self.filename, "wb") as f:
|
||||
f.write(self.DATA * streams)
|
||||
f.write(suffix)
|
||||
|
||||
def testBadArgs(self):
|
||||
self.assertRaises(TypeError, BZ2File, 123.456)
|
||||
|
@ -98,6 +100,11 @@ class BZ2FileTest(BaseTest):
|
|||
self.assertRaises(TypeError, bz2f.read, None)
|
||||
self.assertEqual(bz2f.read(), self.TEXT)
|
||||
|
||||
def testReadBadFile(self):
|
||||
self.createTempFile(streams=0, suffix=self.BAD_DATA)
|
||||
with BZ2File(self.filename) as bz2f:
|
||||
self.assertRaises(OSError, bz2f.read)
|
||||
|
||||
def testReadMultiStream(self):
|
||||
self.createTempFile(streams=5)
|
||||
with BZ2File(self.filename) as bz2f:
|
||||
|
@ -117,6 +124,16 @@ class BZ2FileTest(BaseTest):
|
|||
finally:
|
||||
bz2._BUFFER_SIZE = buffer_size
|
||||
|
||||
def testReadTrailingJunk(self):
|
||||
self.createTempFile(suffix=self.BAD_DATA)
|
||||
with BZ2File(self.filename) as bz2f:
|
||||
self.assertEqual(bz2f.read(), self.TEXT)
|
||||
|
||||
def testReadMultiStreamTrailingJunk(self):
|
||||
self.createTempFile(streams=5, suffix=self.BAD_DATA)
|
||||
with BZ2File(self.filename) as bz2f:
|
||||
self.assertEqual(bz2f.read(), self.TEXT * 5)
|
||||
|
||||
def testRead0(self):
|
||||
self.createTempFile()
|
||||
with BZ2File(self.filename) as bz2f:
|
||||
|
@ -714,10 +731,21 @@ class CompressDecompressTest(BaseTest):
|
|||
def testDecompressIncomplete(self):
|
||||
self.assertRaises(ValueError, bz2.decompress, self.DATA[:-10])
|
||||
|
||||
def testDecompressBadData(self):
|
||||
self.assertRaises(OSError, bz2.decompress, self.BAD_DATA)
|
||||
|
||||
def testDecompressMultiStream(self):
|
||||
text = bz2.decompress(self.DATA * 5)
|
||||
self.assertEqual(text, self.TEXT * 5)
|
||||
|
||||
def testDecompressTrailingJunk(self):
|
||||
text = bz2.decompress(self.DATA + self.BAD_DATA)
|
||||
self.assertEqual(text, self.TEXT)
|
||||
|
||||
def testDecompressMultiStreamTrailingJunk(self):
|
||||
text = bz2.decompress(self.DATA * 5 + self.BAD_DATA)
|
||||
self.assertEqual(text, self.TEXT * 5)
|
||||
|
||||
|
||||
class OpenTest(BaseTest):
|
||||
"Test the open function."
|
||||
|
|
|
@ -313,6 +313,8 @@ class CompressDecompressFunctionTestCase(unittest.TestCase):
|
|||
format=lzma.FORMAT_RAW, filters=FILTERS_RAW_4)
|
||||
|
||||
def test_decompress_bad_input(self):
|
||||
with self.assertRaises(LZMAError):
|
||||
lzma.decompress(COMPRESSED_BOGUS)
|
||||
with self.assertRaises(LZMAError):
|
||||
lzma.decompress(COMPRESSED_RAW_1)
|
||||
with self.assertRaises(LZMAError):
|
||||
|
@ -348,6 +350,16 @@ class CompressDecompressFunctionTestCase(unittest.TestCase):
|
|||
ddata = lzma.decompress(COMPRESSED_XZ + COMPRESSED_ALONE)
|
||||
self.assertEqual(ddata, INPUT * 2)
|
||||
|
||||
# Test robust handling of non-LZMA data following the compressed stream(s).
|
||||
|
||||
def test_decompress_trailing_junk(self):
|
||||
ddata = lzma.decompress(COMPRESSED_XZ + COMPRESSED_BOGUS)
|
||||
self.assertEqual(ddata, INPUT)
|
||||
|
||||
def test_decompress_multistream_trailing_junk(self):
|
||||
ddata = lzma.decompress(COMPRESSED_XZ * 3 + COMPRESSED_BOGUS)
|
||||
self.assertEqual(ddata, INPUT * 3)
|
||||
|
||||
|
||||
class TempFile:
|
||||
"""Context manager - creates a file, and deletes it on __exit__."""
|
||||
|
@ -676,6 +688,14 @@ class FileTestCase(unittest.TestCase):
|
|||
finally:
|
||||
lzma._BUFFER_SIZE = saved_buffer_size
|
||||
|
||||
def test_read_trailing_junk(self):
|
||||
with LZMAFile(BytesIO(COMPRESSED_XZ + COMPRESSED_BOGUS)) as f:
|
||||
self.assertEqual(f.read(), INPUT)
|
||||
|
||||
def test_read_multistream_trailing_junk(self):
|
||||
with LZMAFile(BytesIO(COMPRESSED_XZ * 5 + COMPRESSED_BOGUS)) as f:
|
||||
self.assertEqual(f.read(), INPUT * 5)
|
||||
|
||||
def test_read_from_file(self):
|
||||
with TempFile(TESTFN, COMPRESSED_XZ):
|
||||
with LZMAFile(TESTFN) as f:
|
||||
|
@ -719,6 +739,10 @@ class FileTestCase(unittest.TestCase):
|
|||
with LZMAFile(BytesIO(COMPRESSED_XZ)) as f:
|
||||
self.assertRaises(TypeError, f.read, None)
|
||||
|
||||
def test_read_bad_data(self):
|
||||
with LZMAFile(BytesIO(COMPRESSED_BOGUS)) as f:
|
||||
self.assertRaises(LZMAError, f.read)
|
||||
|
||||
def test_read1(self):
|
||||
with LZMAFile(BytesIO(COMPRESSED_XZ)) as f:
|
||||
blocks = []
|
||||
|
@ -1232,6 +1256,8 @@ LAERTES
|
|||
Farewell.
|
||||
"""
|
||||
|
||||
COMPRESSED_BOGUS = b"this is not a valid lzma stream"
|
||||
|
||||
COMPRESSED_XZ = (
|
||||
b"\xfd7zXZ\x00\x00\x04\xe6\xd6\xb4F\x02\x00!\x01\x16\x00\x00\x00t/\xe5\xa3"
|
||||
b"\xe0\x07\x80\x03\xdf]\x00\x05\x14\x07bX\x19\xcd\xddn\x98\x15\xe4\xb4\x9d"
|
||||
|
|
|
@ -18,6 +18,9 @@ Core and Builtins
|
|||
Library
|
||||
-------
|
||||
|
||||
- Issue #19839: Fix regression in bz2 module's handling of non-bzip2 data at
|
||||
EOF, and analogous bug in lzma module.
|
||||
|
||||
- Issue #19138: doctest's IGNORE_EXCEPTION_DETAIL now allows a match when
|
||||
no exception detail exists (no colon following the exception's name, or
|
||||
a colon does follow but no text follows the colon).
|
||||
|
|
Loading…
Reference in New Issue