gh-86493: Fix possible leaks in some modules initialization (GH-106768)

Fix _ssl, _stat, _testinternalcapi, _threadmodule, cmath, math, posix, time.
This commit is contained in:
Serhiy Storchaka 2023-07-18 10:50:47 +03:00 committed by GitHub
parent 83ac128490
commit 3e65baee72
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
8 changed files with 37 additions and 45 deletions

View File

@ -6118,22 +6118,22 @@ sslmodule_init_versioninfo(PyObject *m)
*/
libver = OpenSSL_version_num();
r = PyLong_FromUnsignedLong(libver);
if (r == NULL || PyModule_AddObject(m, "OPENSSL_VERSION_NUMBER", r))
if (PyModule_Add(m, "OPENSSL_VERSION_NUMBER", r) < 0)
return -1;
parse_openssl_version(libver, &major, &minor, &fix, &patch, &status);
r = Py_BuildValue("IIIII", major, minor, fix, patch, status);
if (r == NULL || PyModule_AddObject(m, "OPENSSL_VERSION_INFO", r))
if (PyModule_Add(m, "OPENSSL_VERSION_INFO", r) < 0)
return -1;
r = PyUnicode_FromString(OpenSSL_version(OPENSSL_VERSION));
if (r == NULL || PyModule_AddObject(m, "OPENSSL_VERSION", r))
if (PyModule_Add(m, "OPENSSL_VERSION", r) < 0)
return -1;
libver = OPENSSL_VERSION_NUMBER;
parse_openssl_version(libver, &major, &minor, &fix, &patch, &status);
r = Py_BuildValue("IIIII", major, minor, fix, patch, status);
if (r == NULL || PyModule_AddObject(m, "_OPENSSL_API_VERSION", r))
if (PyModule_Add(m, "_OPENSSL_API_VERSION", r) < 0)
return -1;
return 0;

View File

@ -591,15 +591,15 @@ stat_exec(PyObject *module)
ADD_INT_MACRO(module, FILE_ATTRIBUTE_TEMPORARY);
ADD_INT_MACRO(module, FILE_ATTRIBUTE_VIRTUAL);
if (PyModule_AddObject(module, "IO_REPARSE_TAG_SYMLINK",
if (PyModule_Add(module, "IO_REPARSE_TAG_SYMLINK",
PyLong_FromUnsignedLong(IO_REPARSE_TAG_SYMLINK)) < 0) {
return -1;
}
if (PyModule_AddObject(module, "IO_REPARSE_TAG_MOUNT_POINT",
if (PyModule_Add(module, "IO_REPARSE_TAG_MOUNT_POINT",
PyLong_FromUnsignedLong(IO_REPARSE_TAG_MOUNT_POINT)) < 0) {
return -1;
}
if (PyModule_AddObject(module, "IO_REPARSE_TAG_APPEXECLINK",
if (PyModule_Add(module, "IO_REPARSE_TAG_APPEXECLINK",
PyLong_FromUnsignedLong(IO_REPARSE_TAG_APPEXECLINK)) < 0) {
return -1;
}

View File

@ -1511,12 +1511,12 @@ static PyMethodDef module_functions[] = {
static int
module_exec(PyObject *module)
{
if (PyModule_AddObject(module, "SIZEOF_PYGC_HEAD",
if (PyModule_Add(module, "SIZEOF_PYGC_HEAD",
PyLong_FromSsize_t(sizeof(PyGC_Head))) < 0) {
return 1;
}
if (PyModule_AddObject(module, "SIZEOF_TIME_T",
if (PyModule_Add(module, "SIZEOF_TIME_T",
PyLong_FromSsize_t(sizeof(time_t))) < 0) {
return 1;
}

View File

@ -1671,7 +1671,7 @@ thread_module_exec(PyObject *module)
// Round towards minus infinity
timeout_max = floor(timeout_max);
if (PyModule_AddObject(module, "TIMEOUT_MAX",
if (PyModule_Add(module, "TIMEOUT_MAX",
PyFloat_FromDouble(timeout_max)) < 0) {
return -1;
}

View File

@ -1217,30 +1217,29 @@ static PyMethodDef cmath_methods[] = {
static int
cmath_exec(PyObject *mod)
{
if (PyModule_AddObject(mod, "pi", PyFloat_FromDouble(Py_MATH_PI)) < 0) {
if (PyModule_Add(mod, "pi", PyFloat_FromDouble(Py_MATH_PI)) < 0) {
return -1;
}
if (PyModule_AddObject(mod, "e", PyFloat_FromDouble(Py_MATH_E)) < 0) {
if (PyModule_Add(mod, "e", PyFloat_FromDouble(Py_MATH_E)) < 0) {
return -1;
}
// 2pi
if (PyModule_AddObject(mod, "tau", PyFloat_FromDouble(Py_MATH_TAU)) < 0) {
if (PyModule_Add(mod, "tau", PyFloat_FromDouble(Py_MATH_TAU)) < 0) {
return -1;
}
if (PyModule_AddObject(mod, "inf", PyFloat_FromDouble(Py_INFINITY)) < 0) {
if (PyModule_Add(mod, "inf", PyFloat_FromDouble(Py_INFINITY)) < 0) {
return -1;
}
Py_complex infj = {0.0, Py_INFINITY};
if (PyModule_AddObject(mod, "infj",
PyComplex_FromCComplex(infj)) < 0) {
if (PyModule_Add(mod, "infj", PyComplex_FromCComplex(infj)) < 0) {
return -1;
}
if (PyModule_AddObject(mod, "nan", PyFloat_FromDouble(fabs(Py_NAN))) < 0) {
if (PyModule_Add(mod, "nan", PyFloat_FromDouble(fabs(Py_NAN))) < 0) {
return -1;
}
Py_complex nanj = {0.0, fabs(Py_NAN)};
if (PyModule_AddObject(mod, "nanj", PyComplex_FromCComplex(nanj)) < 0) {
if (PyModule_Add(mod, "nanj", PyComplex_FromCComplex(nanj)) < 0) {
return -1;
}

View File

@ -4037,20 +4037,20 @@ math_exec(PyObject *module)
if (state->str___trunc__ == NULL) {
return -1;
}
if (PyModule_AddObject(module, "pi", PyFloat_FromDouble(Py_MATH_PI)) < 0) {
if (PyModule_Add(module, "pi", PyFloat_FromDouble(Py_MATH_PI)) < 0) {
return -1;
}
if (PyModule_AddObject(module, "e", PyFloat_FromDouble(Py_MATH_E)) < 0) {
if (PyModule_Add(module, "e", PyFloat_FromDouble(Py_MATH_E)) < 0) {
return -1;
}
// 2pi
if (PyModule_AddObject(module, "tau", PyFloat_FromDouble(Py_MATH_TAU)) < 0) {
if (PyModule_Add(module, "tau", PyFloat_FromDouble(Py_MATH_TAU)) < 0) {
return -1;
}
if (PyModule_AddObject(module, "inf", PyFloat_FromDouble(Py_INFINITY)) < 0) {
if (PyModule_Add(module, "inf", PyFloat_FromDouble(Py_INFINITY)) < 0) {
return -1;
}
if (PyModule_AddObject(module, "nan", PyFloat_FromDouble(fabs(Py_NAN))) < 0) {
if (PyModule_Add(module, "nan", PyFloat_FromDouble(fabs(Py_NAN))) < 0) {
return -1;
}
return 0;

View File

@ -13466,7 +13466,7 @@ setup_confname_table(struct constdef *table, size_t tablesize,
}
Py_DECREF(o);
}
return PyModule_AddObject(module, tablename, d);
return PyModule_Add(module, tablename, d);
}
/* Return -1 on failure, 0 on success. */
@ -16781,11 +16781,9 @@ posixmodule_exec(PyObject *m)
#endif
/* Initialize environ dictionary */
PyObject *v = convertenviron();
Py_XINCREF(v);
if (v == NULL || PyModule_AddObject(m, "environ", v) != 0)
if (PyModule_Add(m, "environ", convertenviron()) != 0) {
return -1;
Py_DECREF(v);
}
if (all_ins(m))
return -1;
@ -16900,9 +16898,7 @@ posixmodule_exec(PyObject *m)
Py_DECREF(unicode);
}
PyModule_AddObject(m, "_have_functions", list);
return 0;
return PyModule_Add(m, "_have_functions", list);
}

View File

@ -1790,11 +1790,9 @@ init_timezone(PyObject *m)
return -1;
}
#endif // MS_WINDOWS
PyObject *tzname_obj = Py_BuildValue("(NN)", otz0, otz1);
if (tzname_obj == NULL) {
if (PyModule_Add(m, "tzname", Py_BuildValue("(NN)", otz0, otz1)) < 0) {
return -1;
}
PyModule_AddObject(m, "tzname", tzname_obj);
#else // !HAVE_DECL_TZNAME
static const time_t YEAR = (365 * 24 + 6) * 3600;
time_t t;
@ -1837,10 +1835,9 @@ init_timezone(PyObject *m)
PyModule_AddIntConstant(m, "daylight", janzone != julyzone);
tzname_obj = Py_BuildValue("(zz)", janname, julyname);
}
if (tzname_obj == NULL) {
if (PyModule_Add(m, "tzname", tzname_obj) < 0) {
return -1;
}
PyModule_AddObject(m, "tzname", tzname_obj);
#endif // !HAVE_DECL_TZNAME
if (PyErr_Occurred()) {