Skip to content

Commit 5bf7f5c

Browse files
[3.12] gh-118998: Handle errors correctly in tmtotuple in timemodule (GH-118999) (#119019)
gh-118998: Handle errors correctly in `tmtotuple` in `timemodule` (GH-118999) (cherry picked from commit fc75792) Co-authored-by: Nikita Sobolev <[email protected]>
1 parent 275ec39 commit 5bf7f5c

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
@@ -481,7 +481,18 @@ tmtotuple(time_module_state *state, struct tm *p
481481
if (v == NULL)
482482
return NULL;
483483

484-
#define SET(i,val) PyStructSequence_SET_ITEM(v, i, PyLong_FromLong((long) val))
484+
#define SET_ITEM(INDEX, CALL) \
485+
do { \
486+
PyObject *obj = (CALL); \
487+
if (obj == NULL) { \
488+
Py_DECREF(v); \
489+
return NULL; \
490+
} \
491+
PyStructSequence_SET_ITEM(v, (INDEX), obj); \
492+
} while (0)
493+
494+
#define SET(INDEX, VAL) \
495+
SET_ITEM((INDEX), PyLong_FromLong((long) (VAL)))
485496

486497
SET(0, p->tm_year + 1900);
487498
SET(1, p->tm_mon + 1); /* Want January == 1 */
@@ -493,19 +504,15 @@ tmtotuple(time_module_state *state, struct tm *p
493504
SET(7, p->tm_yday + 1); /* Want January, 1 == 1 */
494505
SET(8, p->tm_isdst);
495506
#ifdef HAVE_STRUCT_TM_TM_ZONE
496-
PyStructSequence_SET_ITEM(v, 9,
497-
PyUnicode_DecodeLocale(p->tm_zone, "surrogateescape"));
507+
SET_ITEM(9, PyUnicode_DecodeLocale(p->tm_zone, "surrogateescape"));
498508
SET(10, p->tm_gmtoff);
499509
#else
500-
PyStructSequence_SET_ITEM(v, 9,
501-
PyUnicode_DecodeLocale(zone, "surrogateescape"));
502-
PyStructSequence_SET_ITEM(v, 10, _PyLong_FromTime_t(gmtoff));
510+
SET_ITEM(9, PyUnicode_DecodeLocale(zone, "surrogateescape"));
511+
SET_ITEM(10, _PyLong_FromTime_t(gmtoff));
503512
#endif /* HAVE_STRUCT_TM_TM_ZONE */
513+
504514
#undef SET
505-
if (PyErr_Occurred()) {
506-
Py_XDECREF(v);
507-
return NULL;
508-
}
515+
#undef SET_ITEM
509516

510517
return v;
511518
}

0 commit comments

Comments
 (0)