Skip to content
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

Merged
merged 1 commit into from
Sep 7, 2017

Conversation

benjaminp
Copy link
Contributor

@benjaminp benjaminp commented Sep 6, 2017

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. #
Copy link
Member

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 :-)

Copy link
Contributor Author

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)
Copy link
Member

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.

Copy link
Contributor Author

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)];
Copy link
Member

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.

Copy link
Contributor Author

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))
Copy link
Member

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).

Copy link
Contributor Author

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)
Copy link
Member

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.?

Copy link
Contributor Author

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))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

_Py_IsInTypeRange(type, v)?

@bedevere-bot
Copy link

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 I didn't expect the Spanish Inquisition!. I will then notify any core developers who have left a review that you're ready for them to take another look at this pull request.

@benjaminp benjaminp force-pushed the benjamin-undefined-downcast branch 5 times, most recently from 7e4b497 to 4d7578b Compare September 7, 2017 02:21
@benjaminp benjaminp force-pushed the benjamin-undefined-downcast branch from 4d7578b to c120b8b Compare September 7, 2017 02:33
@benjaminp benjaminp merged commit a853a8b into master Sep 7, 2017
@miss-islington
Copy link
Contributor

🐍🍒⛏🤖 Thanks @benjaminp for the PR, and @benjaminp for merging it 🌮🎉.I'm working now to backport this PR to: 3.6.

@benjaminp benjaminp deleted the benjamin-undefined-downcast branch September 7, 2017 18:14
miss-islington pushed a commit to miss-islington/cpython that referenced this pull request Sep 7, 2017
@bedevere-bot
Copy link

GH-3424 is a backport of this pull request to the 3.6 branch.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants