Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit fced79f

Browse filesBrowse files
[3.11] gh-86493: Fix possible leaks in some modules initialization (GH-106768) (GH-106855) (GH-106863)
[3.11] [3.12] gh-86493: Fix possible leaks in some modules initialization (GH-106768) (GH-106855) Fix _ssl, _stat, _testinternalcapi, _threadmodule, cmath, math, posix, time. (cherry picked from commit 3e65bae). (cherry picked from commit a423ddb)
1 parent 0c47ed7 commit fced79f
Copy full SHA for fced79f

File tree

10 files changed

+45
-62
lines changed
Filter options

10 files changed

+45
-62
lines changed

‎Include/cpython/modsupport.h

Copy file name to clipboardExpand all lines: Include/cpython/modsupport.h
+1Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,5 +103,6 @@ PyAPI_FUNC(PyObject * const *) _PyArg_UnpackKeywordsWithVararg(
103103
(minpos), (maxpos), (minkw), (buf)))
104104

105105
PyAPI_FUNC(PyObject *) _PyModule_CreateInitialized(PyModuleDef*, int apiver);
106+
PyAPI_FUNC(int) _PyModule_Add(PyObject *, const char *, PyObject *);
106107

107108
PyAPI_DATA(const char *) _Py_PackageContext;

‎Modules/_ssl.c

Copy file name to clipboardExpand all lines: Modules/_ssl.c
+4-4Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6130,22 +6130,22 @@ sslmodule_init_versioninfo(PyObject *m)
61306130
*/
61316131
libver = OpenSSL_version_num();
61326132
r = PyLong_FromUnsignedLong(libver);
6133-
if (r == NULL || PyModule_AddObject(m, "OPENSSL_VERSION_NUMBER", r))
6133+
if (_PyModule_Add(m, "OPENSSL_VERSION_NUMBER", r) < 0)
61346134
return -1;
61356135

61366136
parse_openssl_version(libver, &major, &minor, &fix, &patch, &status);
61376137
r = Py_BuildValue("IIIII", major, minor, fix, patch, status);
6138-
if (r == NULL || PyModule_AddObject(m, "OPENSSL_VERSION_INFO", r))
6138+
if (_PyModule_Add(m, "OPENSSL_VERSION_INFO", r) < 0)
61396139
return -1;
61406140

61416141
r = PyUnicode_FromString(OpenSSL_version(OPENSSL_VERSION));
6142-
if (r == NULL || PyModule_AddObject(m, "OPENSSL_VERSION", r))
6142+
if (_PyModule_Add(m, "OPENSSL_VERSION", r) < 0)
61436143
return -1;
61446144

61456145
libver = OPENSSL_VERSION_NUMBER;
61466146
parse_openssl_version(libver, &major, &minor, &fix, &patch, &status);
61476147
r = Py_BuildValue("IIIII", major, minor, fix, patch, status);
6148-
if (r == NULL || PyModule_AddObject(m, "_OPENSSL_API_VERSION", r))
6148+
if (_PyModule_Add(m, "_OPENSSL_API_VERSION", r) < 0)
61496149
return -1;
61506150

61516151
return 0;

‎Modules/_stat.c

Copy file name to clipboardExpand all lines: Modules/_stat.c
+9-9Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -592,17 +592,17 @@ stat_exec(PyObject *module)
592592
ADD_INT_MACRO(module, FILE_ATTRIBUTE_TEMPORARY);
593593
ADD_INT_MACRO(module, FILE_ATTRIBUTE_VIRTUAL);
594594

595-
if (PyModule_AddObject(module, "IO_REPARSE_TAG_SYMLINK",
596-
PyLong_FromUnsignedLong(IO_REPARSE_TAG_SYMLINK)) < 0) {
597-
return -1;
595+
if (_PyModule_Add(module, "IO_REPARSE_TAG_SYMLINK",
596+
PyLong_FromUnsignedLong(IO_REPARSE_TAG_SYMLINK)) < 0) {
597+
return -1;
598598
}
599-
if (PyModule_AddObject(module, "IO_REPARSE_TAG_MOUNT_POINT",
600-
PyLong_FromUnsignedLong(IO_REPARSE_TAG_MOUNT_POINT)) < 0) {
601-
return -1;
599+
if (_PyModule_Add(module, "IO_REPARSE_TAG_MOUNT_POINT",
600+
PyLong_FromUnsignedLong(IO_REPARSE_TAG_MOUNT_POINT)) < 0) {
601+
return -1;
602602
}
603-
if (PyModule_AddObject(module, "IO_REPARSE_TAG_APPEXECLINK",
604-
PyLong_FromUnsignedLong(IO_REPARSE_TAG_APPEXECLINK)) < 0) {
605-
return -1;
603+
if (_PyModule_Add(module, "IO_REPARSE_TAG_APPEXECLINK",
604+
PyLong_FromUnsignedLong(IO_REPARSE_TAG_APPEXECLINK)) < 0) {
605+
return -1;
606606
}
607607
#endif
608608

‎Modules/_testinternalcapi.c

Copy file name to clipboardExpand all lines: Modules/_testinternalcapi.c
+1-1Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -682,7 +682,7 @@ PyInit__testinternalcapi(void)
682682
return NULL;
683683
}
684684

685-
if (PyModule_AddObject(module, "SIZEOF_PYGC_HEAD",
685+
if (_PyModule_Add(module, "SIZEOF_PYGC_HEAD",
686686
PyLong_FromSsize_t(sizeof(PyGC_Head))) < 0) {
687687
goto error;
688688
}

‎Modules/_threadmodule.c

Copy file name to clipboardExpand all lines: Modules/_threadmodule.c
+2-2Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1648,8 +1648,8 @@ thread_module_exec(PyObject *module)
16481648
// Round towards minus infinity
16491649
timeout_max = floor(timeout_max);
16501650

1651-
if (PyModule_AddObject(module, "TIMEOUT_MAX",
1652-
PyFloat_FromDouble(timeout_max)) < 0) {
1651+
if (_PyModule_Add(module, "TIMEOUT_MAX",
1652+
PyFloat_FromDouble(timeout_max)) < 0) {
16531653
return -1;
16541654
}
16551655

‎Modules/cmathmodule.c

Copy file name to clipboardExpand all lines: Modules/cmathmodule.c
+7-9Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1264,30 +1264,28 @@ static PyMethodDef cmath_methods[] = {
12641264
static int
12651265
cmath_exec(PyObject *mod)
12661266
{
1267-
if (PyModule_AddObject(mod, "pi", PyFloat_FromDouble(Py_MATH_PI)) < 0) {
1267+
if (_PyModule_Add(mod, "pi", PyFloat_FromDouble(Py_MATH_PI)) < 0) {
12681268
return -1;
12691269
}
1270-
if (PyModule_AddObject(mod, "e", PyFloat_FromDouble(Py_MATH_E)) < 0) {
1270+
if (_PyModule_Add(mod, "e", PyFloat_FromDouble(Py_MATH_E)) < 0) {
12711271
return -1;
12721272
}
12731273
// 2pi
1274-
if (PyModule_AddObject(mod, "tau", PyFloat_FromDouble(Py_MATH_TAU)) < 0) {
1274+
if (_PyModule_Add(mod, "tau", PyFloat_FromDouble(Py_MATH_TAU)) < 0) {
12751275
return -1;
12761276
}
1277-
if (PyModule_AddObject(mod, "inf", PyFloat_FromDouble(m_inf())) < 0) {
1277+
if (_PyModule_Add(mod, "inf", PyFloat_FromDouble(m_inf())) < 0) {
12781278
return -1;
12791279
}
12801280

1281-
if (PyModule_AddObject(mod, "infj",
1282-
PyComplex_FromCComplex(c_infj())) < 0) {
1281+
if (_PyModule_Add(mod, "infj", PyComplex_FromCComplex(c_infj())) < 0) {
12831282
return -1;
12841283
}
12851284
#if _PY_SHORT_FLOAT_REPR == 1
1286-
if (PyModule_AddObject(mod, "nan", PyFloat_FromDouble(m_nan())) < 0) {
1285+
if (_PyModule_Add(mod, "nan", PyFloat_FromDouble(m_nan())) < 0) {
12871286
return -1;
12881287
}
1289-
if (PyModule_AddObject(mod, "nanj",
1290-
PyComplex_FromCComplex(c_nanj())) < 0) {
1288+
if (_PyModule_Add(mod, "nanj", PyComplex_FromCComplex(c_nanj())) < 0) {
12911289
return -1;
12921290
}
12931291
#endif

‎Modules/mathmodule.c

Copy file name to clipboardExpand all lines: Modules/mathmodule.c
+5-5Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3825,21 +3825,21 @@ math_ulp_impl(PyObject *module, double x)
38253825
static int
38263826
math_exec(PyObject *module)
38273827
{
3828-
if (PyModule_AddObject(module, "pi", PyFloat_FromDouble(Py_MATH_PI)) < 0) {
3828+
if (_PyModule_Add(module, "pi", PyFloat_FromDouble(Py_MATH_PI)) < 0) {
38293829
return -1;
38303830
}
3831-
if (PyModule_AddObject(module, "e", PyFloat_FromDouble(Py_MATH_E)) < 0) {
3831+
if (_PyModule_Add(module, "e", PyFloat_FromDouble(Py_MATH_E)) < 0) {
38323832
return -1;
38333833
}
38343834
// 2pi
3835-
if (PyModule_AddObject(module, "tau", PyFloat_FromDouble(Py_MATH_TAU)) < 0) {
3835+
if (_PyModule_Add(module, "tau", PyFloat_FromDouble(Py_MATH_TAU)) < 0) {
38363836
return -1;
38373837
}
3838-
if (PyModule_AddObject(module, "inf", PyFloat_FromDouble(m_inf())) < 0) {
3838+
if (_PyModule_Add(module, "inf", PyFloat_FromDouble(m_inf())) < 0) {
38393839
return -1;
38403840
}
38413841
#if _PY_SHORT_FLOAT_REPR == 1
3842-
if (PyModule_AddObject(module, "nan", PyFloat_FromDouble(m_nan())) < 0) {
3842+
if (_PyModule_Add(module, "nan", PyFloat_FromDouble(m_nan())) < 0) {
38433843
return -1;
38443844
}
38453845
#endif

‎Modules/posixmodule.c

Copy file name to clipboardExpand all lines: Modules/posixmodule.c
+4-8Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12563,7 +12563,7 @@ setup_confname_table(struct constdef *table, size_t tablesize,
1256312563
}
1256412564
Py_DECREF(o);
1256512565
}
12566-
return PyModule_AddObject(module, tablename, d);
12566+
return _PyModule_Add(module, tablename, d);
1256712567
}
1256812568

1256912569
/* Return -1 on failure, 0 on success. */
@@ -15837,11 +15837,9 @@ posixmodule_exec(PyObject *m)
1583715837
#endif
1583815838

1583915839
/* Initialize environ dictionary */
15840-
PyObject *v = convertenviron();
15841-
Py_XINCREF(v);
15842-
if (v == NULL || PyModule_AddObject(m, "environ", v) != 0)
15840+
if (_PyModule_Add(m, "environ", convertenviron()) != 0) {
1584315841
return -1;
15844-
Py_DECREF(v);
15842+
}
1584515843

1584615844
if (all_ins(m))
1584715845
return -1;
@@ -15965,9 +15963,7 @@ posixmodule_exec(PyObject *m)
1596515963
Py_DECREF(unicode);
1596615964
}
1596715965

15968-
PyModule_AddObject(m, "_have_functions", list);
15969-
15970-
return 0;
15966+
return _PyModule_Add(m, "_have_functions", list);
1597115967
}
1597215968

1597315969

‎Modules/timemodule.c

Copy file name to clipboardExpand all lines: Modules/timemodule.c
+2-5Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1775,11 +1775,9 @@ init_timezone(PyObject *m)
17751775
return -1;
17761776
}
17771777
#endif // MS_WINDOWS
1778-
PyObject *tzname_obj = Py_BuildValue("(NN)", otz0, otz1);
1779-
if (tzname_obj == NULL) {
1778+
if (_PyModule_Add(m, "tzname", Py_BuildValue("(NN)", otz0, otz1)) < 0) {
17801779
return -1;
17811780
}
1782-
PyModule_AddObject(m, "tzname", tzname_obj);
17831781
#else // !HAVE_DECL_TZNAME
17841782
static const time_t YEAR = (365 * 24 + 6) * 3600;
17851783
time_t t;
@@ -1822,10 +1820,9 @@ init_timezone(PyObject *m)
18221820
PyModule_AddIntConstant(m, "daylight", janzone != julyzone);
18231821
tzname_obj = Py_BuildValue("(zz)", janname, julyname);
18241822
}
1825-
if (tzname_obj == NULL) {
1823+
if (_PyModule_Add(m, "tzname", tzname_obj) < 0) {
18261824
return -1;
18271825
}
1828-
PyModule_AddObject(m, "tzname", tzname_obj);
18291826
#endif // !HAVE_DECL_TZNAME
18301827

18311828
if (PyErr_Occurred()) {

‎Python/modsupport.c

Copy file name to clipboardExpand all lines: Python/modsupport.c
+10-19Lines changed: 10 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -658,13 +658,16 @@ PyModule_AddObjectRef(PyObject *mod, const char *name, PyObject *value)
658658
PyModule_GetName(mod));
659659
return -1;
660660
}
661-
662-
if (PyDict_SetItemString(dict, name, value)) {
663-
return -1;
664-
}
665-
return 0;
661+
return PyDict_SetItemString(dict, name, value);
666662
}
667663

664+
int
665+
_PyModule_Add(PyObject *mod, const char *name, PyObject *value)
666+
{
667+
int res = PyModule_AddObjectRef(mod, name, value);
668+
Py_XDECREF(value);
669+
return res;
670+
}
668671

669672
int
670673
PyModule_AddObject(PyObject *mod, const char *name, PyObject *value)
@@ -679,25 +682,13 @@ PyModule_AddObject(PyObject *mod, const char *name, PyObject *value)
679682
int
680683
PyModule_AddIntConstant(PyObject *m, const char *name, long value)
681684
{
682-
PyObject *obj = PyLong_FromLong(value);
683-
if (!obj) {
684-
return -1;
685-
}
686-
int res = PyModule_AddObjectRef(m, name, obj);
687-
Py_DECREF(obj);
688-
return res;
685+
return _PyModule_Add(m, name, PyLong_FromLong(value));
689686
}
690687

691688
int
692689
PyModule_AddStringConstant(PyObject *m, const char *name, const char *value)
693690
{
694-
PyObject *obj = PyUnicode_FromString(value);
695-
if (!obj) {
696-
return -1;
697-
}
698-
int res = PyModule_AddObjectRef(m, name, obj);
699-
Py_DECREF(obj);
700-
return res;
691+
return _PyModule_Add(m, name, PyUnicode_FromString(value));
701692
}
702693

703694
int

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.