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-34423: Fix check for overflow when casting from a double to integral types. #8802

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 8 additions & 4 deletions Include/pymath.h
Original file line number Diff line number Diff line change
Expand Up @@ -221,10 +221,14 @@ PyAPI_FUNC(void) _Py_set_387controlword(unsigned short);
#define _Py_IntegralTypeMax(type) ((_Py_IntegralTypeSigned(type)) ? (((((type)1 << (sizeof(type)*CHAR_BIT - 2)) - 1) << 1) + 1) : ~(type)0)
/* Return the minimum value of integral type *type*. */
#define _Py_IntegralTypeMin(type) ((_Py_IntegralTypeSigned(type)) ? -_Py_IntegralTypeMax(type) - 1 : 0)

/* Check whether *v* is in the range of integral type *type*. This is most
* 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_InIntegralTypeRange(type, v) (_Py_IntegralTypeMin(type) <= v && v <= _Py_IntegralTypeMax(type))
* 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. If however sizeof(*v*) == sizeof(*type*) and *v* is a
* floating-point, maximal value of *type* cannot be represented exactly,
* thus the check, to be true needs to use strict less than (<).
*/
#define _Py_InIntegralTypeRange(type, v) (_Py_IntegralTypeMin(type) <= v && v < _Py_IntegralTypeMax(type))

#endif /* Py_PYMATH_H */
1 change: 1 addition & 0 deletions Lib/test/test_time.py
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,7 @@ def test_conversions(self):
def test_sleep(self):
self.assertRaises(ValueError, time.sleep, -2)
self.assertRaises(ValueError, time.sleep, -1)
self.assertRaises(OverflowError, time.sleep, 2**63 / SEC_TO_NS)
time.sleep(1.2)

def test_strftime(self):
Expand Down
1 change: 1 addition & 0 deletions Misc/ACKS
Original file line number Diff line number Diff line change
Expand Up @@ -1830,3 +1830,4 @@ Jelle Zijlstra
Gennadiy Zlobin
Doug Zongker
Peter Åstrand
Michał Radwański
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
``time.sleep(2**63 / 10**9)`` no longer eludes range checks and doesn't
overflow.