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