Skip to content

Commit 3841af4

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

File tree

4 files changed

+43
-84
lines changed

4 files changed

+43
-84
lines changed

Diff for: Modules/_curses_panel.c

+1-3
Original file line numberDiff line numberDiff line change
@@ -670,9 +670,7 @@ _curses_panel_exec(PyObject *mod)
670670
state->PyCursesError = PyErr_NewException(
671671
"_curses_panel.error", NULL, NULL);
672672

673-
Py_INCREF(state->PyCursesError);
674-
if (PyModule_AddObject(mod, "error", state->PyCursesError) < 0) {
675-
Py_DECREF(state->PyCursesError);
673+
if (PyModule_AddObjectRef(mod, "error", state->PyCursesError) < 0) {
676674
return -1;
677675
}
678676

Diff for: Modules/_decimal/_decimal.c

+16-30
Original file line numberDiff line numberDiff line change
@@ -5922,21 +5922,15 @@ PyInit__decimal(void)
59225922

59235923

59245924
/* Add types to the module */
5925-
Py_INCREF(&PyDec_Type);
5926-
CHECK_INT(PyModule_AddObject(m, "Decimal", (PyObject *)&PyDec_Type));
5927-
Py_INCREF(&PyDecContext_Type);
5928-
CHECK_INT(PyModule_AddObject(m, "Context",
5929-
(PyObject *)&PyDecContext_Type));
5930-
Py_INCREF(DecimalTuple);
5931-
CHECK_INT(PyModule_AddObject(m, "DecimalTuple", (PyObject *)DecimalTuple));
5932-
5925+
CHECK_INT(PyModule_AddObjectRef(m, "Decimal", (PyObject *)&PyDec_Type));
5926+
CHECK_INT(PyModule_AddObjectRef(m, "Context", (PyObject *)&PyDecContext_Type));
5927+
CHECK_INT(PyModule_AddObjectRef(m, "DecimalTuple", (PyObject *)DecimalTuple));
59335928

59345929
/* Create top level exception */
59355930
ASSIGN_PTR(DecimalException, PyErr_NewException(
59365931
"decimal.DecimalException",
59375932
PyExc_ArithmeticError, NULL));
5938-
Py_INCREF(DecimalException);
5939-
CHECK_INT(PyModule_AddObject(m, "DecimalException", DecimalException));
5933+
CHECK_INT(PyModule_AddObjectRef(m, "DecimalException", DecimalException));
59405934

59415935
/* Create signal tuple */
59425936
ASSIGN_PTR(SignalTuple, PyTuple_New(SIGNAL_MAP_LEN));
@@ -5976,8 +5970,7 @@ PyInit__decimal(void)
59765970
Py_DECREF(base);
59775971

59785972
/* add to module */
5979-
Py_INCREF(cm->ex);
5980-
CHECK_INT(PyModule_AddObject(m, cm->name, cm->ex));
5973+
CHECK_INT(PyModule_AddObjectRef(m, cm->name, cm->ex));
59815974

59825975
/* add to signal tuple */
59835976
Py_INCREF(cm->ex);
@@ -6007,45 +6000,39 @@ PyInit__decimal(void)
60076000
ASSIGN_PTR(cm->ex, PyErr_NewException(cm->fqname, base, NULL));
60086001
Py_DECREF(base);
60096002

6010-
Py_INCREF(cm->ex);
6011-
CHECK_INT(PyModule_AddObject(m, cm->name, cm->ex));
6003+
CHECK_INT(PyModule_AddObjectRef(m, cm->name, cm->ex));
60126004
}
60136005

60146006

60156007
/* Init default context template first */
60166008
ASSIGN_PTR(default_context_template,
60176009
PyObject_CallObject((PyObject *)&PyDecContext_Type, NULL));
6018-
Py_INCREF(default_context_template);
6019-
CHECK_INT(PyModule_AddObject(m, "DefaultContext",
6020-
default_context_template));
6010+
CHECK_INT(PyModule_AddObjectRef(m, "DefaultContext",
6011+
default_context_template));
60216012

60226013
#ifndef WITH_DECIMAL_CONTEXTVAR
60236014
ASSIGN_PTR(tls_context_key, PyUnicode_FromString("___DECIMAL_CTX__"));
6024-
Py_INCREF(Py_False);
6025-
CHECK_INT(PyModule_AddObject(m, "HAVE_CONTEXTVAR", Py_False));
6015+
CHECK_INT(PyModule_AddObjectRef(m, "HAVE_CONTEXTVAR", Py_False));
60266016
#else
60276017
ASSIGN_PTR(current_context_var, PyContextVar_New("decimal_context", NULL));
6028-
Py_INCREF(Py_True);
6029-
CHECK_INT(PyModule_AddObject(m, "HAVE_CONTEXTVAR", Py_True));
6018+
CHECK_INT(PyModule_AddObjectRef(m, "HAVE_CONTEXTVAR", Py_True));
60306019
#endif
6031-
Py_INCREF(Py_True);
6032-
CHECK_INT(PyModule_AddObject(m, "HAVE_THREADS", Py_True));
6020+
CHECK_INT(PyModule_AddObjectRef(m, "HAVE_THREADS", Py_True));
60336021

60346022
/* Init basic context template */
60356023
ASSIGN_PTR(basic_context_template,
60366024
PyObject_CallObject((PyObject *)&PyDecContext_Type, NULL));
60376025
init_basic_context(basic_context_template);
6038-
Py_INCREF(basic_context_template);
6039-
CHECK_INT(PyModule_AddObject(m, "BasicContext",
6040-
basic_context_template));
6026+
CHECK_INT(PyModule_AddObjectRef(m, "BasicContext",
6027+
basic_context_template));
60416028

60426029
/* Init extended context template */
60436030
ASSIGN_PTR(extended_context_template,
60446031
PyObject_CallObject((PyObject *)&PyDecContext_Type, NULL));
60456032
init_extended_context(extended_context_template);
60466033
Py_INCREF(extended_context_template);
6047-
CHECK_INT(PyModule_AddObject(m, "ExtendedContext",
6048-
extended_context_template));
6034+
CHECK_INT(PyModule_AddObjectRef(m, "ExtendedContext",
6035+
extended_context_template));
60496036

60506037

60516038
/* Init mpd_ssize_t constants */
@@ -6064,8 +6051,7 @@ PyInit__decimal(void)
60646051
/* Init string constants */
60656052
for (i = 0; i < _PY_DEC_ROUND_GUARD; i++) {
60666053
ASSIGN_PTR(round_map[i], PyUnicode_InternFromString(mpd_round_string[i]));
6067-
Py_INCREF(round_map[i]);
6068-
CHECK_INT(PyModule_AddObject(m, mpd_round_string[i], round_map[i]));
6054+
CHECK_INT(PyModule_AddObjectRef(m, mpd_round_string[i], round_map[i]));
60696055
}
60706056

60716057
/* Add specification version number */

Diff for: Modules/posixmodule.c

+22-45
Original file line numberDiff line numberDiff line change
@@ -15849,42 +15849,34 @@ posixmodule_exec(PyObject *m)
1584915849
if (setup_confname_tables(m))
1585015850
return -1;
1585115851

15852-
Py_INCREF(PyExc_OSError);
15853-
PyModule_AddObject(m, "error", PyExc_OSError);
15852+
if (PyModule_AddObjectRef(m, "error", PyExc_OSError) < 0) {
15853+
return -1;
15854+
}
1585415855

1585515856
#if defined(HAVE_WAITID) && !defined(__APPLE__)
1585615857
waitid_result_desc.name = MODNAME ".waitid_result";
15857-
PyObject *WaitidResultType = (PyObject *)PyStructSequence_NewType(&waitid_result_desc);
15858-
if (WaitidResultType == NULL) {
15858+
state->WaitidResultType = (PyObject *)PyStructSequence_NewType(&waitid_result_desc);
15859+
if (PyModule_AddObjectRef(m, "waitid_result", state->WaitidResultType) < 0) {
1585915860
return -1;
1586015861
}
15861-
Py_INCREF(WaitidResultType);
15862-
PyModule_AddObject(m, "waitid_result", WaitidResultType);
15863-
state->WaitidResultType = WaitidResultType;
1586415862
#endif
1586515863

1586615864
stat_result_desc.name = "os.stat_result"; /* see issue #19209 */
1586715865
stat_result_desc.fields[7].name = PyStructSequence_UnnamedField;
1586815866
stat_result_desc.fields[8].name = PyStructSequence_UnnamedField;
1586915867
stat_result_desc.fields[9].name = PyStructSequence_UnnamedField;
15870-
PyObject *StatResultType = (PyObject *)PyStructSequence_NewType(&stat_result_desc);
15871-
if (StatResultType == NULL) {
15868+
state->StatResultType = (PyObject *)PyStructSequence_NewType(&stat_result_desc);
15869+
if (PyModule_AddObjectRef(m, "stat_result", state->StatResultType) < 0) {
1587215870
return -1;
1587315871
}
15874-
Py_INCREF(StatResultType);
15875-
PyModule_AddObject(m, "stat_result", StatResultType);
15876-
state->StatResultType = StatResultType;
15877-
structseq_new = ((PyTypeObject *)StatResultType)->tp_new;
15878-
((PyTypeObject *)StatResultType)->tp_new = statresult_new;
15872+
structseq_new = ((PyTypeObject *)state->StatResultType)->tp_new;
15873+
((PyTypeObject *)state->StatResultType)->tp_new = statresult_new;
1587915874

1588015875
statvfs_result_desc.name = "os.statvfs_result"; /* see issue #19209 */
15881-
PyObject *StatVFSResultType = (PyObject *)PyStructSequence_NewType(&statvfs_result_desc);
15882-
if (StatVFSResultType == NULL) {
15876+
state->StatVFSResultType = (PyObject *)PyStructSequence_NewType(&statvfs_result_desc);
15877+
if (PyModule_AddObjectRef(m, "statvfs_result", state->StatVFSResultType) < 0) {
1588315878
return -1;
1588415879
}
15885-
Py_INCREF(StatVFSResultType);
15886-
PyModule_AddObject(m, "statvfs_result", StatVFSResultType);
15887-
state->StatVFSResultType = StatVFSResultType;
1588815880
#ifdef NEED_TICKS_PER_SECOND
1588915881
# if defined(HAVE_SYSCONF) && defined(_SC_CLK_TCK)
1589015882
ticks_per_second = sysconf(_SC_CLK_TCK);
@@ -15897,24 +15889,18 @@ posixmodule_exec(PyObject *m)
1589715889

1589815890
#if defined(HAVE_SCHED_SETPARAM) || defined(HAVE_SCHED_SETSCHEDULER) || defined(POSIX_SPAWN_SETSCHEDULER) || defined(POSIX_SPAWN_SETSCHEDPARAM)
1589915891
sched_param_desc.name = MODNAME ".sched_param";
15900-
PyObject *SchedParamType = (PyObject *)PyStructSequence_NewType(&sched_param_desc);
15901-
if (SchedParamType == NULL) {
15892+
state->SchedParamType = (PyObject *)PyStructSequence_NewType(&sched_param_desc);
15893+
if (PyModule_AddObjectRef(m, "sched_param", state->SchedParamType) < 0) {
1590215894
return -1;
1590315895
}
15904-
Py_INCREF(SchedParamType);
15905-
PyModule_AddObject(m, "sched_param", SchedParamType);
15906-
state->SchedParamType = SchedParamType;
15907-
((PyTypeObject *)SchedParamType)->tp_new = os_sched_param;
15896+
((PyTypeObject *)state->SchedParamType)->tp_new = os_sched_param;
1590815897
#endif
1590915898

1591015899
/* initialize TerminalSize_info */
15911-
PyObject *TerminalSizeType = (PyObject *)PyStructSequence_NewType(&TerminalSize_desc);
15912-
if (TerminalSizeType == NULL) {
15900+
state->TerminalSizeType = (PyObject *)PyStructSequence_NewType(&TerminalSize_desc);
15901+
if (PyModule_AddObjectRef(m, "terminal_size", state->TerminalSizeType) < 0) {
1591315902
return -1;
1591415903
}
15915-
Py_INCREF(TerminalSizeType);
15916-
PyModule_AddObject(m, "terminal_size", TerminalSizeType);
15917-
state->TerminalSizeType = TerminalSizeType;
1591815904

1591915905
/* initialize scandir types */
1592015906
PyObject *ScandirIteratorType = PyType_FromModuleAndSpec(m, &ScandirIteratorType_spec, NULL);
@@ -15923,30 +15909,21 @@ posixmodule_exec(PyObject *m)
1592315909
}
1592415910
state->ScandirIteratorType = ScandirIteratorType;
1592515911

15926-
PyObject *DirEntryType = PyType_FromModuleAndSpec(m, &DirEntryType_spec, NULL);
15927-
if (DirEntryType == NULL) {
15912+
state->DirEntryType = PyType_FromModuleAndSpec(m, &DirEntryType_spec, NULL);
15913+
if (PyModule_AddObjectRef(m, "DirEntry", state->DirEntryType) < 0) {
1592815914
return -1;
1592915915
}
15930-
Py_INCREF(DirEntryType);
15931-
PyModule_AddObject(m, "DirEntry", DirEntryType);
15932-
state->DirEntryType = DirEntryType;
1593315916

1593415917
times_result_desc.name = MODNAME ".times_result";
15935-
PyObject *TimesResultType = (PyObject *)PyStructSequence_NewType(&times_result_desc);
15936-
if (TimesResultType == NULL) {
15918+
state->TimesResultType = (PyObject *)PyStructSequence_NewType(&times_result_desc);
15919+
if (PyModule_AddObjectRef(m, "times_result", state->TimesResultType) < 0) {
1593715920
return -1;
1593815921
}
15939-
Py_INCREF(TimesResultType);
15940-
PyModule_AddObject(m, "times_result", TimesResultType);
15941-
state->TimesResultType = TimesResultType;
1594215922

15943-
PyTypeObject *UnameResultType = PyStructSequence_NewType(&uname_result_desc);
15944-
if (UnameResultType == NULL) {
15923+
state->UnameResultType = (PyObject *)PyStructSequence_NewType(&uname_result_desc);
15924+
if (PyModule_AddObjectRef(m, "uname_result", state->UnameResultType) < 0) {
1594515925
return -1;
1594615926
}
15947-
Py_INCREF(UnameResultType);
15948-
PyModule_AddObject(m, "uname_result", (PyObject *)UnameResultType);
15949-
state->UnameResultType = (PyObject *)UnameResultType;
1595015927

1595115928
if ((state->billion = PyLong_FromLong(1000000000)) == NULL)
1595215929
return -1;

Diff for: Modules/xxsubtype.c

+4-6
Original file line numberDiff line numberDiff line change
@@ -279,14 +279,12 @@ xxsubtype_exec(PyObject* m)
279279
if (PyType_Ready(&spamdict_type) < 0)
280280
return -1;
281281

282-
Py_INCREF(&spamlist_type);
283-
if (PyModule_AddObject(m, "spamlist",
284-
(PyObject *) &spamlist_type) < 0)
282+
if (PyModule_AddObjectRef(m, "spamlist",
283+
(PyObject *) &spamlist_type) < 0)
285284
return -1;
286285

287-
Py_INCREF(&spamdict_type);
288-
if (PyModule_AddObject(m, "spamdict",
289-
(PyObject *) &spamdict_type) < 0)
286+
if (PyModule_AddObjectRef(m, "spamdict",
287+
(PyObject *) &spamdict_type) < 0)
290288
return -1;
291289
return 0;
292290
}

0 commit comments

Comments
 (0)