[3.15] gh-150599: Prevent bz2 decompressor reuse after errors (GH-150600)

(cherry picked from commit 5755d0f083)

Co-authored-by: Stan Ulbrych <stan@python.org>
This commit is contained in:
Miss Islington (bot) 2026-06-07 18:48:30 +02:00 committed by GitHub
parent a642d1ab38
commit d3ca26983d
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 33 additions and 3 deletions

View file

@ -1032,6 +1032,21 @@ def test_failure(self):
# Previously, a second call could crash due to internal inconsistency
self.assertRaises(Exception, bzd.decompress, self.BAD_DATA * 30)
def test_decompress_after_data_error(self):
data = bytes.fromhex(
"425a6839314159265359000000000000007fffff000000000000000000000000"
"00000000000000000000000000000000000000e0370000000000000000000000"
"000000000000000000000000000000000000000000000000000083f3"
)
bzd = BZ2Decompressor()
with self.assertRaisesRegex(OSError, "Invalid data stream"):
bzd.decompress(data)
# Previously, a second call could crash due to internal inconsistency
self.assertFalse(bzd.needs_input)
self.assertFalse(bzd.eof)
with self.assertRaisesRegex(ValueError, "previous error"):
bzd.decompress(b'\x00' * 18)
@support.refcount_test
def test_refleaks_in___init__(self):
gettotalrefcount = support.get_attribute(sys, 'gettotalrefcount')