-
-
Notifications
You must be signed in to change notification settings - Fork 31.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
bpo-31373: fix undefined floating-point demotions #3396
Conversation
d15a9ef
to
60ec3e7
Compare
Fix several possible instances of undefined behavior due to floating-point | ||
demotions. | ||
|
||
# Write your Misc/NEWS entry below. It should be a simple ReST paragraph. # |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You are not supposed to commit this part :-)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
hmm, blurb hiccup I guess
@@ -858,6 +859,10 @@ convertsimple(PyObject *arg, const char **p_format, va_list *p_va, int flags, | |||
double dval = PyFloat_AsDouble(arg); | |||
if (PyErr_Occurred()) | |||
RETURN_ERR_OCCURRED; | |||
else if (dval > FLT_MAX) | |||
*p = (float)INFINITY; | |||
else if (dval < -FLT_MAX) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would prefer to use FLT_MIN.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
FLT_MIN
is the smallest nonnegative float not what we want here.
goto Overflow; | ||
|
||
unsigned char s[sizeof(float)]; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would be more confident with an assert(sizeof(float) == 4); just in case.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This code only executes if floats are in IEE754 format and are therefore 4 bytes.
int i, incr = 1; | ||
|
||
if (Py_IS_INFINITY(y) && !Py_IS_INFINITY(x)) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't understand why you remove Py_IS_INFINITY(y).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
y
is no longer defined at this point.
Include/pymath.h
Outdated
@@ -215,4 +215,16 @@ PyAPI_FUNC(void) _Py_set_387controlword(unsigned short); | |||
(X) == -Py_HUGE_VAL)) | |||
#endif | |||
|
|||
/* Return whether integral type *type* is signed or not. */ | |||
#define _Py_IntegralSigned(type) ((type)(-1) < 0) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I suggest to put "Type" in the macro names. Maybe _Py_IsTypeSigned(type), _Py_TypeMax(), etc.?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
sg
Include/pymath.h
Outdated
* useful if *v* is floating-point, since demoting a floating-point *v* to an | ||
* integral type that cannot represent *v*'s integral part is undefined | ||
* behavior. */ | ||
#define _Py_IntegralInRange(type, v) (_Py_IntegralMin(type) <= v && v <= _Py_IntegralMax(type)) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
_Py_IsInTypeRange(type, v)?
A Python core developer has requested some changes be made to your pull request before we can consider merging it. If you could please address their requests along with any other requests in other reviews from core developers that would be appreciated. Once you have made the requested changes, please leave a comment on this pull request containing the phrase |
7e4b497
to
4d7578b
Compare
4d7578b
to
c120b8b
Compare
🐍🍒⛏🤖 Thanks @benjaminp for the PR, and @benjaminp for merging it 🌮🎉.I'm working now to backport this PR to: 3.6. |
(cherry picked from commit a853a8b)
GH-3424 is a backport of this pull request to the 3.6 branch. |
https://bugs.python.org/issue31373