Skip to content

bpo-31602: Fix an assertion failure in zipimporter.get_source() in case of a bad zlib.decompress() #3784

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
Sep 29, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions Lib/test/test_zipimport.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@
import inspect
import io
from traceback import extract_tb, extract_stack, print_tb
try:
import zlib
except ImportError:
zlib = None

test_src = """\
def get_name():
Expand Down Expand Up @@ -669,6 +673,19 @@ def testBytesPath(self):
class CompressedZipImportTestCase(UncompressedZipImportTestCase):
compression = ZIP_DEFLATED

@support.cpython_only
def test_issue31602(self):
# There shouldn't be an assertion failure in zipimporter.get_source()
# in case of a bad zlib.decompress().
def bad_decompress(*args):
return None
with ZipFile(TEMP_ZIP, 'w') as zip_file:
self.addCleanup(support.unlink, TEMP_ZIP)
zip_file.writestr('bar.py', b'print("hello world")', ZIP_DEFLATED)
zi = zipimport.zipimporter(TEMP_ZIP)
with support.swap_attr(zlib, 'decompress', bad_decompress):
self.assertRaises(TypeError, zi.get_source, 'bar')


class BadFileZipImportTestCase(unittest.TestCase):
def assertZipFailure(self, filename):
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Fix an assertion failure in `zipimporter.get_source()` in case of a bad
`zlib.decompress()`. Patch by Oren Milman.
8 changes: 8 additions & 0 deletions Modules/zipimport.c
Original file line number Diff line number Diff line change
Expand Up @@ -1236,6 +1236,14 @@ get_data(PyObject *archive, PyObject *toc_entry)
data = PyObject_CallFunction(decompress, "Oi", raw_data, -15);
Py_DECREF(decompress);
Py_DECREF(raw_data);
if (data != NULL && !PyBytes_Check(data)) {
PyErr_Format(PyExc_TypeError,
"zlib.decompress() must return a bytes object, not "
"%.200s",
Py_TYPE(data)->tp_name);
Py_DECREF(data);
return NULL;
}
return data;

eof_error:
Expand Down