Skip to content

Commit 970cb8e

Browse files
[3.12] gh-86493: Fix possible leaks in modules initialization: _curses_panel, _decimal, posix, xxsubtype (GH-106767) (#106849)
(cherry picked from commit 7454923)
1 parent d671c65 commit 970cb8e

File tree

4 files changed

+41
-60
lines changed

4 files changed

+41
-60
lines changed

Modules/_curses_panel.c

+1-2
Original file line numberDiff line numberDiff line change
@@ -662,8 +662,7 @@ _curses_panel_exec(PyObject *mod)
662662
state->PyCursesError = PyErr_NewException(
663663
"_curses_panel.error", NULL, NULL);
664664

665-
if (PyModule_AddObject(mod, "error", Py_NewRef(state->PyCursesError)) < 0) {
666-
Py_DECREF(state->PyCursesError);
665+
if (PyModule_AddObjectRef(mod, "error", state->PyCursesError) < 0) {
667666
return -1;
668667
}
669668

Modules/_decimal/_decimal.c

+16-17
Original file line numberDiff line numberDiff line change
@@ -5871,16 +5871,15 @@ PyInit__decimal(void)
58715871
ASSIGN_PTR(m, PyModule_Create(&_decimal_module));
58725872

58735873
/* Add types to the module */
5874-
CHECK_INT(PyModule_AddObject(m, "Decimal", Py_NewRef(&PyDec_Type)));
5875-
CHECK_INT(PyModule_AddObject(m, "Context",
5876-
Py_NewRef(&PyDecContext_Type)));
5877-
CHECK_INT(PyModule_AddObject(m, "DecimalTuple", Py_NewRef(DecimalTuple)));
5874+
CHECK_INT(PyModule_AddObjectRef(m, "Decimal", (PyObject *)&PyDec_Type));
5875+
CHECK_INT(PyModule_AddObjectRef(m, "Context", (PyObject *)&PyDecContext_Type));
5876+
CHECK_INT(PyModule_AddObjectRef(m, "DecimalTuple", (PyObject *)DecimalTuple));
58785877

58795878
/* Create top level exception */
58805879
ASSIGN_PTR(DecimalException, PyErr_NewException(
58815880
"decimal.DecimalException",
58825881
PyExc_ArithmeticError, NULL));
5883-
CHECK_INT(PyModule_AddObject(m, "DecimalException", Py_NewRef(DecimalException)));
5882+
CHECK_INT(PyModule_AddObjectRef(m, "DecimalException", DecimalException));
58845883

58855884
/* Create signal tuple */
58865885
ASSIGN_PTR(SignalTuple, PyTuple_New(SIGNAL_MAP_LEN));
@@ -5920,7 +5919,7 @@ PyInit__decimal(void)
59205919
Py_DECREF(base);
59215920

59225921
/* add to module */
5923-
CHECK_INT(PyModule_AddObject(m, cm->name, Py_NewRef(cm->ex)));
5922+
CHECK_INT(PyModule_AddObjectRef(m, cm->name, cm->ex));
59245923

59255924
/* add to signal tuple */
59265925
PyTuple_SET_ITEM(SignalTuple, i, Py_NewRef(cm->ex));
@@ -5949,38 +5948,38 @@ PyInit__decimal(void)
59495948
ASSIGN_PTR(cm->ex, PyErr_NewException(cm->fqname, base, NULL));
59505949
Py_DECREF(base);
59515950

5952-
CHECK_INT(PyModule_AddObject(m, cm->name, Py_NewRef(cm->ex)));
5951+
CHECK_INT(PyModule_AddObjectRef(m, cm->name, cm->ex));
59535952
}
59545953

59555954

59565955
/* Init default context template first */
59575956
ASSIGN_PTR(default_context_template,
59585957
PyObject_CallObject((PyObject *)&PyDecContext_Type, NULL));
5959-
CHECK_INT(PyModule_AddObject(m, "DefaultContext",
5960-
Py_NewRef(default_context_template)));
5958+
CHECK_INT(PyModule_AddObjectRef(m, "DefaultContext",
5959+
default_context_template));
59615960

59625961
#ifndef WITH_DECIMAL_CONTEXTVAR
59635962
ASSIGN_PTR(tls_context_key, PyUnicode_FromString("___DECIMAL_CTX__"));
5964-
CHECK_INT(PyModule_AddObject(m, "HAVE_CONTEXTVAR", Py_NewRef(Py_False)));
5963+
CHECK_INT(PyModule_AddObjectRef(m, "HAVE_CONTEXTVAR", Py_False));
59655964
#else
59665965
ASSIGN_PTR(current_context_var, PyContextVar_New("decimal_context", NULL));
5967-
CHECK_INT(PyModule_AddObject(m, "HAVE_CONTEXTVAR", Py_NewRef(Py_True)));
5966+
CHECK_INT(PyModule_AddObjectRef(m, "HAVE_CONTEXTVAR", Py_True));
59685967
#endif
5969-
CHECK_INT(PyModule_AddObject(m, "HAVE_THREADS", Py_NewRef(Py_True)));
5968+
CHECK_INT(PyModule_AddObjectRef(m, "HAVE_THREADS", Py_True));
59705969

59715970
/* Init basic context template */
59725971
ASSIGN_PTR(basic_context_template,
59735972
PyObject_CallObject((PyObject *)&PyDecContext_Type, NULL));
59745973
init_basic_context(basic_context_template);
5975-
CHECK_INT(PyModule_AddObject(m, "BasicContext",
5976-
Py_NewRef(basic_context_template)));
5974+
CHECK_INT(PyModule_AddObjectRef(m, "BasicContext",
5975+
basic_context_template));
59775976

59785977
/* Init extended context template */
59795978
ASSIGN_PTR(extended_context_template,
59805979
PyObject_CallObject((PyObject *)&PyDecContext_Type, NULL));
59815980
init_extended_context(extended_context_template);
5982-
CHECK_INT(PyModule_AddObject(m, "ExtendedContext",
5983-
Py_NewRef(extended_context_template)));
5981+
CHECK_INT(PyModule_AddObjectRef(m, "ExtendedContext",
5982+
extended_context_template));
59845983

59855984

59865985
/* Init mpd_ssize_t constants */
@@ -5999,7 +5998,7 @@ PyInit__decimal(void)
59995998
/* Init string constants */
60005999
for (i = 0; i < _PY_DEC_ROUND_GUARD; i++) {
60016000
ASSIGN_PTR(round_map[i], PyUnicode_InternFromString(mpd_round_string[i]));
6002-
CHECK_INT(PyModule_AddObject(m, mpd_round_string[i], Py_NewRef(round_map[i])));
6001+
CHECK_INT(PyModule_AddObjectRef(m, mpd_round_string[i], round_map[i]));
60036002
}
60046003

60056004
/* Add specification version number */

Modules/posixmodule.c

+22-37
Original file line numberDiff line numberDiff line change
@@ -16790,57 +16790,49 @@ posixmodule_exec(PyObject *m)
1679016790
if (setup_confname_tables(m))
1679116791
return -1;
1679216792

16793-
PyModule_AddObject(m, "error", Py_NewRef(PyExc_OSError));
16793+
if (PyModule_AddObjectRef(m, "error", PyExc_OSError) < 0) {
16794+
return -1;
16795+
}
1679416796

1679516797
#if defined(HAVE_WAITID) && !defined(__APPLE__)
1679616798
waitid_result_desc.name = MODNAME ".waitid_result";
16797-
PyObject *WaitidResultType = (PyObject *)PyStructSequence_NewType(&waitid_result_desc);
16798-
if (WaitidResultType == NULL) {
16799+
state->WaitidResultType = (PyObject *)PyStructSequence_NewType(&waitid_result_desc);
16800+
if (PyModule_AddObjectRef(m, "waitid_result", state->WaitidResultType) < 0) {
1679916801
return -1;
1680016802
}
16801-
PyModule_AddObject(m, "waitid_result", Py_NewRef(WaitidResultType));
16802-
state->WaitidResultType = WaitidResultType;
1680316803
#endif
1680416804

1680516805
stat_result_desc.name = "os.stat_result"; /* see issue #19209 */
1680616806
stat_result_desc.fields[7].name = PyStructSequence_UnnamedField;
1680716807
stat_result_desc.fields[8].name = PyStructSequence_UnnamedField;
1680816808
stat_result_desc.fields[9].name = PyStructSequence_UnnamedField;
16809-
PyObject *StatResultType = (PyObject *)PyStructSequence_NewType(&stat_result_desc);
16810-
if (StatResultType == NULL) {
16809+
state->StatResultType = (PyObject *)PyStructSequence_NewType(&stat_result_desc);
16810+
if (PyModule_AddObjectRef(m, "stat_result", state->StatResultType) < 0) {
1681116811
return -1;
1681216812
}
16813-
PyModule_AddObject(m, "stat_result", Py_NewRef(StatResultType));
16814-
state->StatResultType = StatResultType;
16815-
state->statresult_new_orig = ((PyTypeObject *)StatResultType)->tp_new;
16816-
((PyTypeObject *)StatResultType)->tp_new = statresult_new;
16813+
state->statresult_new_orig = ((PyTypeObject *)state->StatResultType)->tp_new;
16814+
((PyTypeObject *)state->StatResultType)->tp_new = statresult_new;
1681716815

1681816816
statvfs_result_desc.name = "os.statvfs_result"; /* see issue #19209 */
16819-
PyObject *StatVFSResultType = (PyObject *)PyStructSequence_NewType(&statvfs_result_desc);
16820-
if (StatVFSResultType == NULL) {
16817+
state->StatVFSResultType = (PyObject *)PyStructSequence_NewType(&statvfs_result_desc);
16818+
if (PyModule_AddObjectRef(m, "statvfs_result", state->StatVFSResultType) < 0) {
1682116819
return -1;
1682216820
}
16823-
PyModule_AddObject(m, "statvfs_result", Py_NewRef(StatVFSResultType));
16824-
state->StatVFSResultType = StatVFSResultType;
1682516821

1682616822
#if defined(HAVE_SCHED_SETPARAM) || defined(HAVE_SCHED_SETSCHEDULER) || defined(POSIX_SPAWN_SETSCHEDULER) || defined(POSIX_SPAWN_SETSCHEDPARAM)
1682716823
sched_param_desc.name = MODNAME ".sched_param";
16828-
PyObject *SchedParamType = (PyObject *)PyStructSequence_NewType(&sched_param_desc);
16829-
if (SchedParamType == NULL) {
16824+
state->SchedParamType = (PyObject *)PyStructSequence_NewType(&sched_param_desc);
16825+
if (PyModule_AddObjectRef(m, "sched_param", state->SchedParamType) < 0) {
1683016826
return -1;
1683116827
}
16832-
PyModule_AddObject(m, "sched_param", Py_NewRef(SchedParamType));
16833-
state->SchedParamType = SchedParamType;
16834-
((PyTypeObject *)SchedParamType)->tp_new = os_sched_param;
16828+
((PyTypeObject *)state->SchedParamType)->tp_new = os_sched_param;
1683516829
#endif
1683616830

1683716831
/* initialize TerminalSize_info */
16838-
PyObject *TerminalSizeType = (PyObject *)PyStructSequence_NewType(&TerminalSize_desc);
16839-
if (TerminalSizeType == NULL) {
16832+
state->TerminalSizeType = (PyObject *)PyStructSequence_NewType(&TerminalSize_desc);
16833+
if (PyModule_AddObjectRef(m, "terminal_size", state->TerminalSizeType) < 0) {
1684016834
return -1;
1684116835
}
16842-
PyModule_AddObject(m, "terminal_size", Py_NewRef(TerminalSizeType));
16843-
state->TerminalSizeType = TerminalSizeType;
1684416836

1684516837
/* initialize scandir types */
1684616838
PyObject *ScandirIteratorType = PyType_FromModuleAndSpec(m, &ScandirIteratorType_spec, NULL);
@@ -16849,28 +16841,21 @@ posixmodule_exec(PyObject *m)
1684916841
}
1685016842
state->ScandirIteratorType = ScandirIteratorType;
1685116843

16852-
PyObject *DirEntryType = PyType_FromModuleAndSpec(m, &DirEntryType_spec, NULL);
16853-
if (DirEntryType == NULL) {
16844+
state->DirEntryType = PyType_FromModuleAndSpec(m, &DirEntryType_spec, NULL);
16845+
if (PyModule_AddObjectRef(m, "DirEntry", state->DirEntryType) < 0) {
1685416846
return -1;
1685516847
}
16856-
PyModule_AddObject(m, "DirEntry", Py_NewRef(DirEntryType));
16857-
state->DirEntryType = DirEntryType;
1685816848

1685916849
times_result_desc.name = MODNAME ".times_result";
16860-
PyObject *TimesResultType = (PyObject *)PyStructSequence_NewType(&times_result_desc);
16861-
if (TimesResultType == NULL) {
16850+
state->TimesResultType = (PyObject *)PyStructSequence_NewType(&times_result_desc);
16851+
if (PyModule_AddObjectRef(m, "times_result", state->TimesResultType) < 0) {
1686216852
return -1;
1686316853
}
16864-
PyModule_AddObject(m, "times_result", Py_NewRef(TimesResultType));
16865-
state->TimesResultType = TimesResultType;
1686616854

16867-
PyTypeObject *UnameResultType = PyStructSequence_NewType(&uname_result_desc);
16868-
if (UnameResultType == NULL) {
16855+
state->UnameResultType = (PyObject *)PyStructSequence_NewType(&uname_result_desc);
16856+
if (PyModule_AddObjectRef(m, "uname_result", state->UnameResultType) < 0) {
1686916857
return -1;
1687016858
}
16871-
;
16872-
PyModule_AddObject(m, "uname_result", Py_NewRef(UnameResultType));
16873-
state->UnameResultType = (PyObject *)UnameResultType;
1687416859

1687516860
if ((state->billion = PyLong_FromLong(1000000000)) == NULL)
1687616861
return -1;

Modules/xxsubtype.c

+2-4
Original file line numberDiff line numberDiff line change
@@ -274,12 +274,10 @@ xxsubtype_exec(PyObject* m)
274274
if (PyType_Ready(&spamdict_type) < 0)
275275
return -1;
276276

277-
if (PyModule_AddObject(m, "spamlist",
278-
Py_NewRef(&spamlist_type)) < 0)
277+
if (PyModule_AddObjectRef(m, "spamlist", (PyObject *)&spamlist_type) < 0)
279278
return -1;
280279

281-
if (PyModule_AddObject(m, "spamdict",
282-
Py_NewRef(&spamdict_type)) < 0)
280+
if (PyModule_AddObjectRef(m, "spamdict", (PyObject *)&spamdict_type) < 0)
283281
return -1;
284282
return 0;
285283
}

0 commit comments

Comments
 (0)