8000 bpo-1635741: Fix refleaks of time module error handling (GH-18486) · python/cpython@196f1eb · GitHub
[go: up one dir, main page]

Skip to content

Commit 196f1eb

Browse files
authored
bpo-1635741: Fix refleaks of time module error handling (GH-18486)
1 parent aa0c080 commit 196f1eb

File tree

1 file changed

+45
-18
lines changed

1 file changed

+45
-18
lines changed

Modules/timemodule.c

Lines changed: 45 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1759,52 +1759,78 @@ PyInit_time(void)
17591759

17601760
/* Set, or reset, module variables like time.timezone */
17611761
if (init_timezone(m) < 0) {
1762-
return NULL;
1762+
goto error;
17631763
}
17641764

17651765
#if defined(HAVE_CLOCK_GETTIME) || defined(HAVE_CLOCK_SETTIME) || defined(HAVE_CLOCK_GETRES)
17661766

17671767
#ifdef CLOCK_REALTIME
1768-
PyModule_AddIntMacro(m, CLOCK_REALTIME);
1768+
if (PyModule_AddIntMacro(m, CLOCK_REALTIME) < 0) {
1769+
goto error;
1770+
}
17691771
#endif
17701772
#ifdef CLOCK_MONOTONIC
1771-
PyModule_AddIntMacro(m, CLOCK_MONOTONIC);
1773+
if (PyModule_AddIntMacro(m, CLOCK_MONOTONIC) < 0) {
1774+
goto error;
1775+
}
17721776
#endif
17731777
#ifdef CLOCK_MONOTONIC_RAW
1774-
PyModule_AddIntMacro(m, CLOCK_MONOTONIC_RAW);
1778+
if (PyModule_AddIntMacro(m, CLOCK_MONOTONIC_RAW) < 0) {
1779+
goto error;
1780+
}
17751781
#endif
17761782
#ifdef CLOCK_HIGHRES
1777-
PyModule_AddIntMacro(m, CLOCK_HIGHRES);
1783+
if (PyModule_AddIntMacro(m, CLOCK_HIGHRES) < 0) {
1784+
goto error;
1785+
}
17781786
#endif
17791787
#ifdef CLOCK_PROCESS_CPUTIME_ID
1780-
PyModule_AddIntMacro(m, CLOCK_PROCESS_CPUTIME_ID);
1788+
if (PyModule_AddIntMacro(m, CLOCK_PROCESS_CPUTIME_ID) < 0) {
1789+
goto error;
1790+
}
17811791
#endif
17821792
#ifdef CLOCK_THREAD_CPUTIME_ID
1783-
PyModule_AddIntMacro(m, CLOCK_THREAD_CPUTIME_ID);
1793+
if (PyModule_AddIntMacro(m, CLOCK_THREAD_CPUTIME_ID) < 0) {
1794+
goto error;
1795+
}
17841796
#endif
17851797
#ifdef CLOCK_PROF
1786-
PyModule_AddIntMacro(m, CLOCK_PROF);
1798+
if (PyModule_AddIntMacro(m, CLOCK_PROF) < 0) {
1799+
goto error;
1800+
}
17871801
#endif
17881802
#ifdef CLOCK_BOOTTIME
1789-
PyModule_AddIntMacro(m, CLOCK_BOOTTIME);
1803+
if (PyModule_AddIntMacro(m, CLOCK_BOOTTIME) < 0) {
1804+
goto error;
1805+
}
17901806
#endif
17911807
#ifdef CLOCK_UPTIME
1792-
PyModule_AddIntMacro(m, CLOCK_UPTIME);
1808+
if (PyModule_AddIntMacro(m, CLOCK_UPTIME) < 0) {
1809+
goto error;
1810+
}
17931811
#endif
17941812
#ifdef CLOCK_UPTIME_RAW
1795-
PyModule_AddIntMacro(m, CLOCK_UPTIME_RAW);
1813+
if (PyModule_AddIntMacro(m, CLOCK_UPTIME_RAW) < 0) {
1814+
goto error;
1815+
}
17961816
#endif
17971817

17981818
#endif /* defined(HAVE_CLOCK_GETTIME) || defined(HAVE_CLOCK_SETTIME) || defined(HAVE_CLOCK_GETRES) */
17991819

18001820
if (!initialized) {
18011821
if (PyStructSequence_InitType2(&StructTimeType,
1802-
&struct_time_type_desc) < 0)
1803-
return NULL;
1822+
&struct_time_type_desc) < 0) {
1823+
goto error;
8000
1824+
}
1825+
}
1826+
if (PyModule_AddIntConstant(m, "_STRUCT_TM_ITEMS", 11)) {
1827+
goto error;
18041828
}
18051829
Py_INCREF(&StructTimeType);
1806-
PyModule_AddIntConstant(m, "_STRUCT_TM_ITEMS", 11);
1807-
PyModule_AddObject(m, "struct_time", (PyObject*) &StructTimeType);
1830+
if (PyModule_AddObject(m, "struct_time", (PyObject*) &StructTimeType)) {
1831+
Py_DECREF(&StructTimeType);
1832+
goto error;
1833+
}
18081834
initialized = 1;
18091835

18101836
#if defined(__linux__) && !defined(__GLIBC__)
@@ -1814,10 +1840,11 @@ PyInit_time(void)
18141840
utc_string = tm.tm_zone;
18151841
#endif
18161842

1817-
if (PyErr_Occurred()) {
1818-
return NULL;
1819-
}
18201843
return m;
1844+
1845+
error:
1846+
Py_DECREF(m);
1847+
return NULL;
18211848
}
18221849

18231850
/* Implement pysleep() for various platforms.

0 commit comments

Comments
 (0)
0