gh-118998: Handle errors correctly in tmtotuple in timemodule (#118999)

This commit is contained in:
Nikita Sobolev 2024-05-14 00:20:59 +03:00 committed by GitHub
parent b04c497f18
commit fc75792594
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -462,7 +462,18 @@ tmtotuple(time_module_state *state, struct tm *p
if (v == NULL) if (v == NULL)
return NULL; return NULL;
#define SET(i,val) PyStructSequence_SET_ITEM(v, i, PyLong_FromLong((long) val)) #define SET_ITEM(INDEX, CALL) \
do { \
PyObject *obj = (CALL); \
if (obj == NULL) { \
Py_DECREF(v); \
return NULL; \
} \
PyStructSequence_SET_ITEM(v, (INDEX), obj); \
} while (0)
#define SET(INDEX, VAL) \
SET_ITEM((INDEX), PyLong_FromLong((long) (VAL)))
SET(0, p->tm_year + 1900); SET(0, p->tm_year + 1900);
SET(1, p->tm_mon + 1); /* Want January == 1 */ SET(1, p->tm_mon + 1); /* Want January == 1 */
@ -474,19 +485,15 @@ tmtotuple(time_module_state *state, struct tm *p
SET(7, p->tm_yday + 1); /* Want January, 1 == 1 */ SET(7, p->tm_yday + 1); /* Want January, 1 == 1 */
SET(8, p->tm_isdst); SET(8, p->tm_isdst);
#ifdef HAVE_STRUCT_TM_TM_ZONE #ifdef HAVE_STRUCT_TM_TM_ZONE
PyStructSequence_SET_ITEM(v, 9, SET_ITEM(9, PyUnicode_DecodeLocale(p->tm_zone, "surrogateescape"));
PyUnicode_DecodeLocale(p->tm_zone, "surrogateescape"));
SET(10, p->tm_gmtoff); SET(10, p->tm_gmtoff);
#else #else
PyStructSequence_SET_ITEM(v, 9, SET_ITEM(9, PyUnicode_DecodeLocale(zone, "surrogateescape"));
PyUnicode_DecodeLocale(zone, "surrogateescape")); SET_ITEM(10, _PyLong_FromTime_t(gmtoff));
PyStructSequence_SET_ITEM(v, 10, _PyLong_FromTime_t(gmtoff));
#endif /* HAVE_STRUCT_TM_TM_ZONE */ #endif /* HAVE_STRUCT_TM_TM_ZONE */
#undef SET #undef SET
if (PyErr_Occurred()) { #undef SET_ITEM
Py_XDECREF(v);
return NULL;
}
return v; return v;
} }