mirror of
https://github.com/python/cpython.git
synced 2025-11-27 05:44:16 +00:00
zipimport: remove arbitrary length limit from message formats
PyErr_Format() and PyUnicode_FromFormat() are able to allocate the right buffer size and to catch memory allocation failures.
This commit is contained in:
parent
9e40fad193
commit
07298a1f04
1 changed files with 9 additions and 14 deletions
|
|
@ -194,10 +194,10 @@ zipimporter_repr(ZipImporter *self)
|
||||||
if (self->archive == NULL)
|
if (self->archive == NULL)
|
||||||
return PyUnicode_FromString("<zipimporter object \"???\">");
|
return PyUnicode_FromString("<zipimporter object \"???\">");
|
||||||
else if (self->prefix != NULL && PyUnicode_GET_SIZE(self->prefix) != 0)
|
else if (self->prefix != NULL && PyUnicode_GET_SIZE(self->prefix) != 0)
|
||||||
return PyUnicode_FromFormat("<zipimporter object \"%.300U%c%.150U\">",
|
return PyUnicode_FromFormat("<zipimporter object \"%U%c%U\">",
|
||||||
self->archive, SEP, self->prefix);
|
self->archive, SEP, self->prefix);
|
||||||
else
|
else
|
||||||
return PyUnicode_FromFormat("<zipimporter object \"%.300U\">",
|
return PyUnicode_FromFormat("<zipimporter object \"%U\">",
|
||||||
self->archive);
|
self->archive);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -415,8 +415,7 @@ zipimporter_is_package(PyObject *obj, PyObject *args)
|
||||||
if (mi == MI_ERROR)
|
if (mi == MI_ERROR)
|
||||||
return NULL;
|
return NULL;
|
||||||
if (mi == MI_NOT_FOUND) {
|
if (mi == MI_NOT_FOUND) {
|
||||||
PyErr_Format(ZipImportError, "can't find module '%.200s'",
|
PyErr_Format(ZipImportError, "can't find module '%s'", fullname);
|
||||||
fullname);
|
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
return PyBool_FromLong(mi == MI_PACKAGE);
|
return PyBool_FromLong(mi == MI_PACKAGE);
|
||||||
|
|
@ -502,8 +501,7 @@ zipimporter_get_source(PyObject *obj, PyObject *args)
|
||||||
if (mi == MI_ERROR)
|
if (mi == MI_ERROR)
|
||||||
return NULL;
|
return NULL;
|
||||||
if (mi == MI_NOT_FOUND) {
|
if (mi == MI_NOT_FOUND) {
|
||||||
PyErr_Format(ZipImportError, "can't find module '%.200s'",
|
PyErr_Format(ZipImportError, "can't find module '%s'", fullname);
|
||||||
fullname);
|
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
subname = get_subname(fullname);
|
subname = get_subname(fullname);
|
||||||
|
|
@ -737,23 +735,20 @@ read_directory(PyObject *archive_obj)
|
||||||
|
|
||||||
fp = _Py_fopen(archive_obj, "rb");
|
fp = _Py_fopen(archive_obj, "rb");
|
||||||
if (fp == NULL) {
|
if (fp == NULL) {
|
||||||
PyErr_Format(ZipImportError, "can't open Zip file: "
|
PyErr_Format(ZipImportError, "can't open Zip file: '%U'", archive_obj);
|
||||||
"'%.200U'", archive_obj);
|
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
fseek(fp, -22, SEEK_END);
|
fseek(fp, -22, SEEK_END);
|
||||||
header_position = ftell(fp);
|
header_position = ftell(fp);
|
||||||
if (fread(endof_central_dir, 1, 22, fp) != 22) {
|
if (fread(endof_central_dir, 1, 22, fp) != 22) {
|
||||||
fclose(fp);
|
fclose(fp);
|
||||||
PyErr_Format(ZipImportError, "can't read Zip file: "
|
PyErr_Format(ZipImportError, "can't read Zip file: '%U'", archive_obj);
|
||||||
"'%.200U'", archive_obj);
|
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
if (get_long((unsigned char *)endof_central_dir) != 0x06054B50) {
|
if (get_long((unsigned char *)endof_central_dir) != 0x06054B50) {
|
||||||
/* Bad: End of Central Dir signature */
|
/* Bad: End of Central Dir signature */
|
||||||
fclose(fp);
|
fclose(fp);
|
||||||
PyErr_Format(ZipImportError, "not a Zip file: "
|
PyErr_Format(ZipImportError, "not a Zip file: '%U'", archive_obj);
|
||||||
"'%.200U'", archive_obj);
|
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -1021,7 +1016,7 @@ unmarshal_code(char *pathname, PyObject *data, time_t mtime)
|
||||||
if (!PyCode_Check(code)) {
|
if (!PyCode_Check(code)) {
|
||||||
Py_DECREF(code);
|
Py_DECREF(code);
|
||||||
PyErr_Format(PyExc_TypeError,
|
PyErr_Format(PyExc_TypeError,
|
||||||
"compiled module %.200s is not a code object",
|
"compiled module %s is not a code object",
|
||||||
pathname);
|
pathname);
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
@ -1209,7 +1204,7 @@ get_module_code(ZipImporter *self, char *fullname,
|
||||||
return code;
|
return code;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
PyErr_Format(ZipImportError, "can't find module '%.200s'", fullname);
|
PyErr_Format(ZipImportError, "can't find module '%s'", fullname);
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue