Skip to content

Commit e312e3c

Browse files
committed
gh-118998: Handle errors correctly in tmtotuple in timemodule
1 parent f526314 commit e312e3c

File tree

1 file changed

+17
-10
lines changed

1 file changed

+17
-10
lines changed

Modules/timemodule.c

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -462,7 +462,18 @@ tmtotuple(time_module_state *state, struct tm *p
462462
if (v == NULL)
463463
return NULL;
464464

465-
#define SET(i,val) PyStructSequence_SET_ITEM(v, i, PyLong_FromLong((long) val))
465+
#define SET_ITEM(INDEX, CALL) \
466+
do { \
467+
PyObject *obj = (CALL); \
468+
if (obj == NULL) { \
469+
Py_DECREF(v); \
470+
return NULL; \
471+
} \
472+
PyStructSequence_SET_ITEM(v, (INDEX), obj); \
473+
} while (0)
474+
475+
#define SET(INDEX, VAL) \
476+
SET_ITEM((INDEX), PyLong_FromLong((long) (VAL)))
466477

467478
SET(0, p->tm_year + 1900);
468479
SET(1, p->tm_mon + 1); /* Want January == 1 */
@@ -474,19 +485,15 @@ tmtotuple(time_module_state *state, struct tm *p
474485
SET(7, p->tm_yday + 1); /* Want January, 1 == 1 */
475486
SET(8, p->tm_isdst);
476487
#ifdef HAVE_STRUCT_TM_TM_ZONE
477-
PyStructSequence_SET_ITEM(v, 9,
478-
PyUnicode_DecodeLocale(p->tm_zone, "surrogateescape"));
488+
SET_ITEM(9, PyUnicode_DecodeLocale(p->tm_zone, "surrogateescape"));
479489
SET(10, p->tm_gmtoff);
480490
#else
481-
PyStructSequence_SET_ITEM(v, 9,
482-
PyUnicode_DecodeLocale(zone, "surrogateescape"));
483-
PyStructSequence_SET_ITEM(v, 10, _PyLong_FromTime_t(gmtoff));
491+
SET_ITEM(9, PyUnicode_DecodeLocale(zone, "surrogateescape"));
492+
SET_ITEM(10, _PyLong_FromTime_t(gmtoff));
484493
#endif /* HAVE_STRUCT_TM_TM_ZONE */
494+
485495
#undef SET
486-
if (PyErr_Occurred()) {
487-
Py_XDECREF(v);
488-
return NULL;
489-
}
496+
#undef SET_ITEM
490497

491498
return v;
492499
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy