summaryrefslogtreecommitdiffstats
path: root/Modules/timemodule.c
diff options
context:
space:
mode:
authorHai Shi <shihai1992@gmail.com>2020-03-11 16:56:08 (GMT)
committerGitHub <noreply@github.com>2020-03-11 16:56:08 (GMT)
commit196f1eb6adcfc6a7239330ef508b8bf9dff9940f (patch)
tree692f56b6e802bee036982ab441a00a1477940c90 /Modules/timemodule.c
parentaa0c0808efbfdee813d2829e49030c667da44e72 (diff)
downloadcpython-196f1eb6adcfc6a7239330ef508b8bf9dff9940f.zip
cpython-196f1eb6adcfc6a7239330ef508b8bf9dff9940f.tar.gz
cpython-196f1eb6adcfc6a7239330ef508b8bf9dff9940f.tar.bz2
bpo-1635741: Fix refleaks of time module error handling (GH-18486)
Diffstat (limited to 'Modules/timemodule.c')
-rw-r--r--Modules/timemodule.c63
1 files changed, 45 insertions, 18 deletions
diff --git a/Modules/timemodule.c b/Modules/timemodule.c
index 1709720..f4b3f69 100644
--- a/Modules/timemodule.c
+++ b/Modules/timemodule.c
@@ -1759,52 +1759,78 @@ PyInit_time(void)
/* Set, or reset, module variables like time.timezone */
if (init_timezone(m) < 0) {
- return NULL;
+ goto error;
}
#if defined(HAVE_CLOCK_GETTIME) || defined(HAVE_CLOCK_SETTIME) || defined(HAVE_CLOCK_GETRES)
#ifdef CLOCK_REALTIME
- PyModule_AddIntMacro(m, CLOCK_REALTIME);
+ if (PyModule_AddIntMacro(m, CLOCK_REALTIME) < 0) {
+ goto error;
+ }
#endif
#ifdef CLOCK_MONOTONIC
- PyModule_AddIntMacro(m, CLOCK_MONOTONIC);
+ if (PyModule_AddIntMacro(m, CLOCK_MONOTONIC) < 0) {
+ goto error;
+ }
#endif
#ifdef CLOCK_MONOTONIC_RAW
- PyModule_AddIntMacro(m, CLOCK_MONOTONIC_RAW);
+ if (PyModule_AddIntMacro(m, CLOCK_MONOTONIC_RAW) < 0) {
+ goto error;
+ }
#endif
#ifdef CLOCK_HIGHRES
- PyModule_AddIntMacro(m, CLOCK_HIGHRES);
+ if (PyModule_AddIntMacro(m, CLOCK_HIGHRES) < 0) {
+ goto error;
+ }
#endif
#ifdef CLOCK_PROCESS_CPUTIME_ID
- PyModule_AddIntMacro(m, CLOCK_PROCESS_CPUTIME_ID);
+ if (PyModule_AddIntMacro(m, CLOCK_PROCESS_CPUTIME_ID) < 0) {
+ goto error;
+ }
#endif
#ifdef CLOCK_THREAD_CPUTIME_ID
- PyModule_AddIntMacro(m, CLOCK_THREAD_CPUTIME_ID);
+ if (PyModule_AddIntMacro(m, CLOCK_THREAD_CPUTIME_ID) < 0) {
+ goto error;
+ }
#endif
#ifdef CLOCK_PROF
- PyModule_AddIntMacro(m, CLOCK_PROF);
+ if (PyModule_AddIntMacro(m, CLOCK_PROF) < 0) {
+ goto error;
+ }
#endif
#ifdef CLOCK_BOOTTIME
- PyModule_AddIntMacro(m, CLOCK_BOOTTIME);
+ if (PyModule_AddIntMacro(m, CLOCK_BOOTTIME) < 0) {
+ goto error;
+ }
#endif
#ifdef CLOCK_UPTIME
- PyModule_AddIntMacro(m, CLOCK_UPTIME);
+ if (PyModule_AddIntMacro(m, CLOCK_UPTIME) < 0) {
+ goto error;
+ }
#endif
#ifdef CLOCK_UPTIME_RAW
- PyModule_AddIntMacro(m, CLOCK_UPTIME_RAW);
+ if (PyModule_AddIntMacro(m, CLOCK_UPTIME_RAW) < 0) {
+ goto error;
+ }
#endif
#endif /* defined(HAVE_CLOCK_GETTIME) || defined(HAVE_CLOCK_SETTIME) || defined(HAVE_CLOCK_GETRES) */
if (!initialized) {
if (PyStructSequence_InitType2(&StructTimeType,
- &struct_time_type_desc) < 0)
- return NULL;
+ &struct_time_type_desc) < 0) {
+ goto error;
+ }
+ }
+ if (PyModule_AddIntConstant(m, "_STRUCT_TM_ITEMS", 11)) {
+ goto error;
}
Py_INCREF(&StructTimeType);
- PyModule_AddIntConstant(m, "_STRUCT_TM_ITEMS", 11);
- PyModule_AddObject(m, "struct_time", (PyObject*) &StructTimeType);
+ if (PyModule_AddObject(m, "struct_time", (PyObject*) &StructTimeType)) {
+ Py_DECREF(&StructTimeType);
+ goto error;
+ }
initialized = 1;
#if defined(__linux__) && !defined(__GLIBC__)
@@ -1814,10 +1840,11 @@ PyInit_time(void)
utc_string = tm.tm_zone;
#endif
- if (PyErr_Occurred()) {
- return NULL;
- }
return m;
+
+error:
+ Py_DECREF(m);
+ return NULL;
}
/* Implement pysleep() for various platforms.