Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit db60a5b

Browse files
orenmnbrettcannon
authored andcommitted
bpo-31781: Prevent crashes when calling methods of an uninitialized zipimport.zipimporter object (GH-3986)
1 parent 56cb465 commit db60a5b

3 files changed

Lines changed: 34 additions & 0 deletions

File tree

Lib/test/test_zipimport.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -668,6 +668,20 @@ def testBytesPath(self):
668668
with self.assertWarns(DeprecationWarning):
669669
zipimport.zipimporter(memoryview(os.fsencode(filename)))
670670

671+
@support.cpython_only
672+
def testUninitializedZipimporter(self):
673+
# The interpreter shouldn't crash in case of calling methods of an
674+
# uninitialized zipimport.zipimporter object.
675+
zi = zipimport.zipimporter.__new__(zipimport.zipimporter)
676+
self.assertRaises(ValueError, zi.find_module, 'foo')
677+
self.assertRaises(ValueError, zi.find_loader, 'foo')
678+
self.assertRaises(ValueError, zi.load_module, 'foo')
679+
self.assertRaises(ValueError, zi.get_filename, 'foo')
680+
self.assertRaises(ValueError, zi.is_package, 'foo')
681+
self.assertRaises(ValueError, zi.get_data, 'foo')
682+
self.assertRaises(ValueError, zi.get_code, 'foo')
683+
self.assertRaises(ValueError, zi.get_source, 'foo')
684+
671685

672686
@support.requires_zlib
673687
class CompressedZipImportTestCase(UncompressedZipImportTestCase):
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Prevent crashes when calling methods of an uninitialized
2+
``zipimport.zipimporter`` object. Patch by Oren Milman.

Modules/zipimport.c

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -322,6 +322,12 @@ get_module_info(ZipImporter *self, PyObject *fullname)
322322
PyObject *path, *fullpath, *item;
323323
struct st_zip_searchorder *zso;
324324

325+
if (self->prefix == NULL) {
326+
PyErr_SetString(PyExc_ValueError,
327+
"zipimporter.__init__() wasn't called");
328+
return MI_ERROR;
329+
}
330+
325331
subname = get_subname(fullname);
326332
if (subname == NULL)
327333
return MI_ERROR;
@@ -652,6 +658,12 @@ zipimport_zipimporter_get_data_impl(ZipImporter *self, PyObject *path)
652658
PyObject *toc_entry;
653659
Py_ssize_t path_start, path_len, len;
654660

661+
if (self->archive == NULL) {
662+
PyErr_SetString(PyExc_ValueError,
663+
"zipimporter.__init__() wasn't called");
664+
return NULL;
665+
}
666+
655667
#ifdef ALTSEP
656668
path = _PyObject_CallMethodId((PyObject *)&PyUnicode_Type, &PyId_replace,
657669
"OCC", path, ALTSEP, SEP);
@@ -1476,6 +1488,12 @@ get_module_code(ZipImporter *self, PyObject *fullname,
14761488
PyObject *path, *fullpath = NULL;
14771489
struct st_zip_searchorder *zso;
14781490

1491+
if (self->prefix == NULL) {
1492+
PyErr_SetString(PyExc_ValueError,
1493+
"zipimporter.__init__() wasn't called");
1494+
return NULL;
1495+
}
1496+
14791497
subname = get_subname(fullname);
14801498
if (subname == NULL)
14811499
return NULL;

0 commit comments

Comments
 (0)