From 81e9382ebdb8c6b3079f3a83cc2e75c0b5ef339a Mon Sep 17 00:00:00 2001 From: sobolevn Date: Wed, 13 Mar 2024 11:20:24 +0300 Subject: [PATCH 1/2] gh-116714: Handle errors correctly in `PyFloat_GetInfo` --- Objects/floatobject.c | 24 ++++++++++++++++-------- 1 file changed, 16 insertions(+), 8 deletions(-) diff --git a/Objects/floatobject.c b/Objects/floatobject.c index 37d2d312a6a0b7..96c39d7df16b56 100644 --- a/Objects/floatobject.c +++ b/Objects/floatobject.c @@ -98,10 +98,21 @@ 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 +127,8 @@ PyFloat_GetInfo(void) SetIntFlag(FLT_ROUNDS); #undef SetIntFlag #undef SetDblFlag +#undef SetFlag - if (PyErr_Occurred()) { - Py_CLEAR(floatinfo); - return NULL; - } return floatinfo; } From 134d32dc5eb0650fcf12caa6aa35abdfd042fdbf Mon Sep 17 00:00:00 2001 From: Nikita Sobolev Date: Wed, 13 Mar 2024 15:07:09 +0300 Subject: [PATCH 2/2] Update Objects/floatobject.c Co-authored-by: Serhiy Storchaka --- Objects/floatobject.c | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/Objects/floatobject.c b/Objects/floatobject.c index 96c39d7df16b56..96227f2cf7d76f 100644 --- a/Objects/floatobject.c +++ b/Objects/floatobject.c @@ -108,11 +108,8 @@ PyFloat_GetInfo(void) PyStructSequence_SET_ITEM(floatinfo, pos++, flag); \ } while (0) -#define SetIntFlag(FLAG) \ - SetFlag(PyLong_FromLong((FLAG))) - -#define SetDblFlag(FLAG) \ - SetFlag(PyFloat_FromDouble((FLAG))) +#define SetIntFlag(FLAG) SetFlag(PyLong_FromLong((FLAG))) +#define SetDblFlag(FLAG) SetFlag(PyFloat_FromDouble((FLAG))) SetDblFlag(DBL_MAX); SetIntFlag(DBL_MAX_EXP);