From fcd49b4f47f1edd9a2717f6619da7e7af8ea73cf Mon Sep 17 00:00:00 2001 From: Nikita Sobolev Date: Wed, 13 Mar 2024 15:38:03 +0300 Subject: [PATCH] gh-116714: Handle errors correctly in `PyFloat_GetInfo` (#116715) Co-authored-by: Serhiy Storchaka --- Objects/floatobject.c | 21 +++++++++++++-------- 1 file changed, 13 insertions(+), 8 deletions(-) diff --git a/Objects/floatobject.c b/Objects/floatobject.c index 37d2d312a6a..96227f2cf7d 100644 --- a/Objects/floatobject.c +++ b/Objects/floatobject.c @@ -98,10 +98,18 @@ PyFloat_GetInfo(void) return NULL; } -#define SetIntFlag(flag) \ - PyStructSequence_SET_ITEM(floatinfo, pos++, PyLong_FromLong(flag)) -#define SetDblFlag(flag) \ - PyStructSequence_SET_ITEM(floatinfo, pos++, PyFloat_FromDouble(flag)) +#define SetFlag(CALL) \ + do { \ + PyObject *flag = (CALL); \ + if (flag == NULL) { \ + Py_CLEAR(floatinfo); \ + return NULL; \ + } \ + PyStructSequence_SET_ITEM(floatinfo, pos++, flag); \ + } while (0) + +#define SetIntFlag(FLAG) SetFlag(PyLong_FromLong((FLAG))) +#define SetDblFlag(FLAG) SetFlag(PyFloat_FromDouble((FLAG))) SetDblFlag(DBL_MAX); SetIntFlag(DBL_MAX_EXP); @@ -116,11 +124,8 @@ PyFloat_GetInfo(void) SetIntFlag(FLT_ROUNDS); #undef SetIntFlag #undef SetDblFlag +#undef SetFlag - if (PyErr_Occurred()) { - Py_CLEAR(floatinfo); - return NULL; - } return floatinfo; }