8000 gh-86493: Fix possible leaks in some modules initialization · python/cpython@1bec79f · GitHub
[go: up one dir, main page]

Skip to content

Commit 1bec79f

Browse files
gh-86493: Fix possible leaks in some modules initialization
Fix _ssl, _stat, _testinternalcapi, _threadmodule, cmath, math, posix, time.
1 parent 32718f9 commit 1bec79f

File tree

10 files changed

+58
-64
lines changed

10 files changed

+58
-64
lines changed

Include/internal/pycore_modsupport.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ extern PyObject ** _Py_VaBuildStack(
2121
Py_ssize_t *p_nargs);
2222

2323
extern PyObject* _PyModule_CreateInitialized(PyModuleDef*, int apiver);
24+
PyAPI_FUNC(int) _PyModule_AddNew(PyObject *, const char *, PyObject *);
2425

2526
#ifdef __cplusplus
2627
}

Modules/_ssl.c

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
#define OPENSSL_NO_DEPRECATED 1
2727

2828
#include "Python.h"
29+
#include "pycore_modsupport.h" // _PyModule_AddNew()
2930
#include "pycore_weakref.h" // _PyWeakref_GET_REF()
3031

3132
/* Include symbols from _socket module */
@@ -6085,22 +6086,22 @@ sslmodule_init_versioninfo(PyObject *m)
60856086
*/
60866087
libver = OpenSSL_version_num();
60876088
r = PyLong_FromUnsignedLong(libver);
6088-
if (r == NULL || PyModule_AddObject(m, "OPENSSL_VERSION_NUMBER", r))
6089+
if (_PyModule_AddNew(m, "OPENSSL_VERSION_NUMBER", r) < 0)
60896090
return -1;
60906091

60916092
parse_openssl_version(libver, &major, &minor, &fix, &patch, &status);
60926093
r = Py_BuildValue("IIIII", major, minor, fix, patch, status);
6093-
if (r == NULL || PyModule_AddObject(m, "OPENSSL_VERSION_INFO", r))
6094+
if (_PyModule_AddNew(m, "OPENSSL_VERSION_INFO", r) < 0)
60946095
return -1;
60956096

60966097
r = PyUnicode_FromString(OpenSSL_version(OPENSSL_VERSION));
6097-
if (r == NULL || PyModule_AddObject(m, "OPENSSL_VERSION", r))
6098+
if (_PyModule_AddNew(m, "OPENSSL_VERSION", r) < 0)
60986099
return -1;
60996100

61006101
libver = OPENSSL_VERSION_NUMBER;
61016102
parse_openssl_version(libver, &major, &minor, &fix, &patch, &status);
61026103
r = Py_BuildValue("IIIII", major, minor, fix, patch, status);
6103-
if (r == NULL || PyModule_AddObject(m, "_OPENSSL_API_VERSION", r))
6104+
if (_PyModule_AddNew(m, "_OPENSSL_API_VERSION", r) < 0)
61046105
return -1;
61056106

61066107
return 0;

Modules/_stat.c

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@
1313

1414
#include "Python.h"
1515

16+
#include "pycore_modsupport.h" // _PyModule_AddNew()
17+
1618
#ifdef __cplusplus
1719
extern "C" {
1820
#endif
@@ -591,17 +593,17 @@ stat_exec(PyObject *module)
591593
ADD_INT_MACRO(module, FILE_ATTRIBUTE_TEMPORARY);
592594
ADD_INT_MACRO(module, FILE_ATTRIBUTE_VIRTUAL);
593595

594-
if (PyModule_AddObject(module, "IO_REPARSE_TAG_SYMLINK",
595-
PyLong_FromUnsignedLong(IO_REPARSE_TAG_SYMLINK)) < 0) {
596-
return -1;
596+
if (_PyModule_AddNew(module, "IO_REPARSE_TAG_SYMLINK",
597+
PyLong_FromUnsignedLong(IO_REPARSE_TAG_SYMLINK)) < 0) {
598+
return -1;
597599
}
598-
if (PyModule_AddObject(module, "IO_REPARSE_TAG_MOUNT_POINT",
599-
PyLong_FromUnsignedLong(IO_REPARSE_TAG_MOUNT_POINT)) < 0) {
600-
return -1;
600+
if (_PyModule_AddNew(module, "IO_REPARSE_TAG_MOUNT_POINT",
601+
PyLong_FromUnsignedLong(IO_REPARSE_TAG_MOUNT_POINT)) < 0) {
602+
return -1;
601603
}
602-
if (PyModule_AddObject(module, "IO_REPARSE_TAG_APPEXECLINK",
603-
PyLong_FromUnsignedLong(IO_REPARSE_TAG_APPEXECLINK)) < 0) {
604-
return -1;
604+
if (_PyModule_AddNew(module, "IO_REPARSE_TAG_APPEXECLINK",
605+
PyLong_FromUnsignedLong(IO_REPARSE_TAG_APPEXECLINK)) < 0) {
606+
return -1;
605607
}
606608
#endif
607609

Modules/_testinternalcapi.c

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
#include "pycore_hashtable.h" // _Py_hashtable_new()
2424
#include "pycore_initconfig.h" // _Py_GetConfigsAsDict()
2525
#include "pycore_interp.h" // _PyInterpreterState_GetConfigCopy()
26+
#include "pycore_modsupport.h" // _PyModule_AddNew()
2627
#include "pycore_pathconfig.h" // _PyPathConfig_ClearGlobal()
2728
#include "pycore_pyerrors.h" // _Py_UTF8_Edit_Cost()
2829
#include "pycore_pystate.h" // _PyThreadState_GET()
@@ -1493,13 +1494,13 @@ static PyMethodDef module_functions[] = {
14931494
static int
14941495
module_exec(PyObject *module)
14951496
{
1496-
if (PyModule_AddObject(module, "SIZEOF_PYGC_HEAD",
1497-
PyLong_FromSsize_t(sizeof(PyGC_Head))) < 0) {
1497+
if (_PyModule_AddNew(module, "SIZEOF_PYGC_HEAD",
1498+
PyLong_FromSsize_t(sizeof(PyGC_Head))) < 0) {
14981499
return 1;
14991500
}
15001501

1501-
if (PyModule_AddObject(module, "SIZEOF_TIME_T",
1502-
PyLong_FromSsize_t(sizeof(time_t))) < 0) {
1502+
if (_PyModule_AddNew(module, "SIZEOF_TIME_T",
1503+
PyLong_FromSsize_t(sizeof(time_t))) < 0) {
15031504
return 1;
15041505
}
15051506

Modules/_threadmodule.c

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
#include "Python.h"
66
#include "pycore_interp.h" // _PyInterpreterState.threads.count
7+
#include "pycore_modsupport.h" // _PyModule_AddNew()
78
#include "pycore_moduleobject.h" // _PyModule_GetState()
89
#include "pycore_pylifecycle.h"
910
#include "pycore_pystate.h" // _PyThreadState_SetCurrent()
@@ -1671,8 +1672,8 @@ thread_module_exec(PyObject *module)
16711672
// Round towards minus infinity
16721673
timeout_max = floor(timeout_max);
16731674

1674-
if (PyModule_AddObject(module, "TIMEOUT_MAX",
1675-
PyFloat_FromDouble(timeout_max)) < 0) {
1675+
if (_PyModule_AddNew(module, "TIMEOUT_MAX",
1676+
PyFloat_FromDouble(timeout_max)) < 0) {
16761677
return -1;
16771678
}
16781679

Modules/cmathmodule.c

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
#include "Python.h"
1010
#include "pycore_complexobject.h" // _Py_c_neg()
11+
#include "pycore_modsupport.h" // _PyModule_AddNew()
1112
#include "pycore_pymath.h" // _PY_SHORT_FLOAT_REPR
1213
/* we need DBL_MAX, DBL_MIN, DBL_EPSILON, DBL_MANT_DIG and FLT_RADIX from
1314
float.h. We assume that FLT_RADIX is either 2 or 16. */
@@ -1217,30 +1218,29 @@ static PyMethodDef cmath_methods[] = {
12171218
static int
12181219
cmath_exec(PyObject *mod)
12191220
{
1220-
if (PyModule_AddObject(mod, "pi", PyFloat_FromDouble(Py_MATH_PI)) < 0) {
1221+
if (_PyModule_AddNew(mod, "pi", PyFloat_FromDouble(Py_MATH_PI)) < 0) {
12211222
return -1;
12221223
}
1223-
if (PyModule_AddObject(mod, "e", PyFloat_FromDouble(Py_MATH_E)) < 0) {
1224+
if (_PyModule_AddNew(mod, "e", PyFloat_FromDouble(Py_MATH_E)) < 0) {
12241225
return -1;
12251226
}
12261227
// 2pi
1227-
if (PyModule_AddObject(mod, "tau", PyFloat_FromDouble(Py_MATH_TAU)) < 0) {
1228+
if (_PyModule_AddNew(mod, "tau", PyFloat_FromDouble(Py_MATH_TAU)) < 0) {
12281229
return -1;
12291230
}
1230-
if (PyModule_AddObject(mod, "inf", PyFloat_FromDouble(Py_INFINITY)) < 0) {
1231+
if (_PyModule_AddNew(mod, "inf", PyFloat_FromDouble(Py_INFINITY)) < 0) {
12311232
return -1;
12321233
}
12331234

12341235
Py_complex infj = {0.0, Py_INFINITY};
1235-
if (PyModule_AddObject(mod, "infj",
1236-
PyComplex_FromCComplex(infj)) < 0) {
1236+
if (_PyModule_AddNew(mod, "infj", PyComplex_FromCComplex(infj)) < 0) {
12371237
return -1;
12381238
}
1239-
if (PyModule_AddObject(mod, "nan", PyFloat_FromDouble(fabs(Py_NAN))) < 0) {
1239+
if (_PyModule_AddNew(mod, "nan", PyFloat_FromDouble(fabs(Py_NAN))) < 0) {
12401240
return -1;
12411241
}
12421242
Py_complex nanj = {0.0, fabs(Py_NAN)};
1243-
if (PyModule_AddObject(mod, "nanj", PyComplex_FromCComplex(nanj)) < 0) {
1243+
if (_PyModule_AddNew(mod, "nanj", PyComplex_FromCComplex(nanj)) < 0) {
12441244
return -1;
12451245
}
12461246

Modules/mathmodule.c

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ raised for division by zero and mod by zero.
6060
#include "pycore_bitutils.h" // _Py_bit_length()
6161
#include "pycore_call.h" // _PyObject_CallNoArgs()
6262
#include "pycore_long.h" // _PyLong_GetZero()
63+
#include "pycore_modsupport.h" // _PyModule_AddNew()
6364
#include "pycore_moduleobject.h" // _PyModule_GetState()
6465
#include "pycore_object.h" // _PyObject_LookupSpecial()
6566
#include "pycore_pymath.h" // _PY_SHORT_FLOAT_REPR
@@ -4037,20 +4038,20 @@ math_exec(PyObject *module)
40374038
if (state->str___trunc__ == NULL) {
40384039
return -1;
40394040
}
4040-
if (PyModule_AddObject(module, "pi", PyFloat_FromDouble(Py_MATH_PI)) < 0) {
4041+
if (_PyModule_AddNew(module, "pi", PyFloat_FromDouble(Py_MATH_PI)) < 0) {
40414042
return -1;
40424043
}
4043-
if (PyModule_AddObject(module, "e", PyFloat_FromDouble(Py_MATH_E)) < 0) {
4044+
if (_PyModule_AddNew(module, "e", PyFloat_FromDouble(Py_MATH_E)) < 0) {
40444045
return -1;
40454046
}
40464047
// 2pi
4047-
if (PyModule_AddObject(module, "tau", PyFloat_FromDouble(Py_MATH_TAU)) < 0) {
4048+
if (_PyModule_AddNew(module, "tau", PyFloat_FromDouble(Py_MATH_TAU)) < 0) {
40484049
return -1;
40494050
}
4050-
if (PyModule_AddObject(module, "inf", PyFloat_FromDouble(Py_INFINITY)) < 0) {
4051+
if (_PyModule_AddNew(module, "inf", PyFloat_FromDouble(Py_INFINITY)) < 0) {
40514052
return -1;
40524053
}
4053-
if (PyModule_AddObject(module, "nan", PyFloat_FromDouble(fabs(Py_NAN))) < 0) {
4054+
if (_PyModule_AddNew(module, "nan", PyFloat_FromDouble(fabs(Py_NAN))) < 0) {
40544055
return -1;
40554056
}
40564057
return 0;

Modules/posixmodule.c

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
#include "pycore_fileutils.h" // _Py_closerange()
1818
#include "pycore_import.h" // _PyImport_ReInitLock()
1919
#include "pycore_initconfig.h" // _PyStatus_EXCEPTION()
20+
#include "pycore_modsupport.h" // _PyModule_AddNew()
2021
#include "pycore_moduleobject.h" // _PyModule_GetState()
2122
#include "pycore_object.h" // _PyObject_LookupSpecial()
2223
#include "pycore_pylifecycle.h" // _PyOS_URandom()
@@ -13466,7 +13467,7 @@ setup_confname_table(struct constdef *table, size_t tablesize,
1346613467
}
1346713468
Py_DECREF(o);
1346813469
}
13469-
return PyModule_AddObject(module, tablename, d);
13470+
return _PyModule_AddNew(module, tablename, d);
1347013471
}
1347113472

1347213473
/* Return -1 on failure, 0 on success. */
@@ -16781,11 +16782,9 @@ posixmodule_exec(PyObject *m)
1678116782
#endif
1678216783

1678316784
/* Initialize environ dictionary */
16784-
PyObject *v = convertenviron();
16785-
Py_XINCREF(v);
16786-
if (v == NULL || PyModule_AddObject(m, "environ", v) != 0)
16785+
if (_PyModule_AddNew(m, "environ", convertenviron()) != 0) {
1678716786
return -1;
16788-
Py_DECREF(v);
16787+
}
1678916788

1679016789
if (all_ins(m))
1679116790
return -1;
@@ -16915,9 +16914,7 @@ posixmodule_exec(PyObject *m)
1691516914
Py_DECREF(unicode);
1691616915
}
1691716916

16918-
PyModule_AddObject(m, "_have_functions", list);
16919-
16920-
return 0;
16917+
return _PyModule_AddNew(m, "_have_functions", list);
1692116918
}
1692216919

1692316920

Modules/timemodule.c

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
#include "Python.h"
44
#include "pycore_fileutils.h" // _Py_BEGIN_SUPPRESS_IPH
5+
#include "pycore_modsupport.h" // _PyModule_AddNew()
56
#include "pycore_moduleobject.h" // _PyModule_GetState()
67
#include "pycore_namespace.h" // _PyNamespace_New()
78
#include "pycore_runtime.h" // _Py_ID()
@@ -1790,11 +1791,9 @@ init_timezone(PyObject *m)
17901791
return -1;
17911792
}
17921793
#endif // MS_WINDOWS
1793-
PyObject *tzname_obj = Py_BuildValue("(NN)", otz0, otz1);
1794-
if (tzname_obj == NULL) {
1794+
if (_PyModule_AddNew(m, "tzname", Py_BuildValue("(NN)", otz0, otz1)) < 0) {
17951795
return -1;
17961796
}
1797-
PyModule_AddObject(m, "tzname", tzname_obj);
17981797
#else // !HAVE_DECL_TZNAME
17991798
static const time_t YEAR = (365 * 24 + 6) * 3600;
18001799
time_t t;
@@ -1837,10 +1836,9 @@ init_timezone(PyObject *m)
18371836
PyModule_AddIntConstant(m, "daylight", janzone != julyzone);
18381837
tzname_obj = Py_BuildValue("(zz)", janname, julyname);
18391838
}
1840-
if (tzname_obj == NULL) {
1839+
if (_PyModule_AddNew(m, "tzname", tzname_obj) < 0) {
18411840
return -1;
18421841
}
1843-
PyModule_AddObject(m, "tzname", tzname_obj);
18441842
#endif // !HAVE_DECL_TZNAME
18451843

18461844
if (PyErr_Occurred()) {

Python/modsupport.c

Lines changed: 11 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
#include "Python.h"
55
#include "pycore_abstract.h" // _PyIndex_Check()
6+
#include "pycore_modsupport.h"
67
#include "pycore_object.h" // _PyType_IsReady()
78

89
typedef double va_double;
@@ -606,13 +607,16 @@ PyModule_AddObjectRef(PyObject *mod, const char *name, PyObject *value)
606607
PyModule_GetName(mod));
607608
return -1;
608609
}
609-
610-
if (PyDict_SetItemString(dict, name, value)) {
611-
return -1;
612-
}
613-
return 0;
610+
return PyDict_SetItemString(dict, name, value);
614611
}
615612

613+
int
614+
_PyModule_AddNew(PyObject *mod, const char *name, PyObject *value)
615+
{
616+
int res = PyModule_AddObjectRef(mod, name, value);
617+
Py_XDECREF(value);
618+
return res;
619+
}
616620

617621
int
618622
PyModule_AddObject(PyObject *mod, const char *name, PyObject *value)
@@ -627,25 +631,13 @@ PyModule_AddObject(PyObject *mod, const char *name, PyObject *value)
627631
int
628632
PyModule_AddIntConstant(PyObject *m, const char *name, long value)
629633
{
630-
PyObject *obj = PyLong_FromLong(value);
631-
if (!obj) {
632-
return -1;
633-
}
634-
int res = PyModule_AddObjectRef(m, name, obj);
635-
Py_DECREF(obj);
636-
return res;
634+
return _PyModule_AddNew(m, name, PyLong_FromLong(value));
637635
}
638636

639637
int
640638
PyModule_AddStringConstant(PyObject *m, const char *name, const char *value)
641639
{
642-
PyObject *obj = PyUnicode_FromString(value);
643-
if (!obj) {
644-
return -1;
645-
}
646-
int res = PyModule_AddObjectRef(m, name, obj);
647-
Py_DECREF(obj);
648-
return res;
640+
return _PyModule_AddNew(m, name, PyUnicode_FromString(value));
649641
}
650642

651643
int

0 commit comments

Comments
 (0)
0