From a853a8ba7850381d49b284295dd6f0dc491dbe44 Mon Sep 17 00:00:00 2001 From: Benjamin Peterson Date: Thu, 7 Sep 2017 11:13:59 -0700 Subject: bpo-31373: fix undefined floating-point demotions (#3396) --- Objects/floatobject.c | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) (limited to 'Objects/floatobject.c') diff --git a/Objects/floatobject.c b/Objects/floatobject.c index 8c4fe74d1b..fc1ddf4081 100644 --- a/Objects/floatobject.c +++ b/Objects/floatobject.c @@ -2233,13 +2233,15 @@ _PyFloat_Pack4(double x, unsigned char *p, int le) } else { - float y = (float)x; - const unsigned char *s = (unsigned char*)&y; int i, incr = 1; - if (Py_IS_INFINITY(y) && !Py_IS_INFINITY(x)) + if (fabs(x) > FLT_MAX && !Py_IS_INFINITY(x)) goto Overflow; + unsigned char s[sizeof(float)]; + float y = (float)x; + memcpy(s, &y, sizeof(float)); + if ((float_format == ieee_little_endian_format && !le) || (float_format == ieee_big_endian_format && le)) { p += 3; @@ -2247,7 +2249,7 @@ _PyFloat_Pack4(double x, unsigned char *p, int le) } for (i = 0; i < 4; i++) { - *p = *s++; + *p = s[i]; p += incr; } return 0; -- cgit v1.2.1