Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion Include/internal/pycore_time.h
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,8 @@ PyAPI_FUNC(int) _PyTime_ObjectToTimespec(
PyAPI_FUNC(PyTime_t) _PyTime_FromSeconds(int seconds);

// Create a timestamp from a number of seconds in double.
extern int _PyTime_FromSecondsDouble(
// Export for '_testinternalcapi' shared extension.
PyAPI_FUNC(int) _PyTime_FromSecondsDouble(
double seconds,
_PyTime_round_t round,
PyTime_t *result);
Expand Down
9 changes: 9 additions & 0 deletions Lib/test/test_os/test_os.py
Original file line number Diff line number Diff line change
Expand Up @@ -4555,6 +4555,15 @@ def test_timerfd_ns_select(self):
total_time_ns = initial_expiration_ns + interval_ns * (count - 1)
self.assertGreater(t, total_time_ns - self.CLOCK_RES_NS)

def test_settime_nan(self):
fd = self.timerfd_create(time.CLOCK_REALTIME)

with self.assertRaises(ValueError):
os.timerfd_settime(fd, initial=float("nan"))
with self.assertRaises(ValueError):
os.timerfd_settime(fd, interval=float("nan"))


class OSErrorTests(unittest.TestCase):
def setUp(self):
class Str(str):
Expand Down
21 changes: 19 additions & 2 deletions Lib/test/test_time.py
Original file line number Diff line number Diff line change
Expand Up @@ -1123,10 +1123,27 @@ def c_int_filter(secs):
lambda secs: secs * SEC_TO_NS,
value_filter=c_int_filter)

# Min/max must raise OverflowError
_PyTime_FromSeconds(_testcapi.INT_MIN)
_PyTime_FromSeconds(_testcapi.INT_MAX)

def test_FromSecondsDouble(self):
from _testinternalcapi import _PyTime_FromSecondsDouble

self.check_int_rounding(
_PyTime_FromSecondsDouble,
# C double to int conversion loses precision outside the range
# [-2**52 // SEC_TO_NS; 2**52 // SEC_TO_NS]
lambda secs: int(float(secs) * SEC_TO_NS))

self.check_float_rounding(
_PyTime_FromSecondsDouble,
lambda ns: self.decimal_round(ns * SEC_TO_NS))

# test nan
for time_rnd, _ in ROUNDING_MODES:
with self.assertRaises(TypeError):
_PyTime_FromSeconds(float('nan'))
with self.assertRaises(ValueError):
_PyTime_FromSecondsDouble(float('nan'), time_rnd)

def test_FromSecondsObject(self):
from _testinternalcapi import _PyTime_FromSecondsObject
Expand Down
31 changes: 31 additions & 0 deletions Modules/_testinternalcapi/pytime.c
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#endif


// Test _PyTime_FromSeconds()
static PyObject *
test_pytime_fromseconds(PyObject *self, PyObject *args)
{
Expand All @@ -34,6 +35,26 @@ check_time_rounding(int round)
return 0;
}

// Test _PyTime_FromSecondsDouble()
static PyObject *
test_pytime_fromsecondsdouble(PyObject *self, PyObject *args)
{
double seconds;
int round;
if (!PyArg_ParseTuple(args, "di", &seconds, &round)) {
return NULL;
}
if (check_time_rounding(round) < 0) {
return NULL;
}
PyTime_t ts;
if (_PyTime_FromSecondsDouble(seconds, round, &ts) < 0) {
return NULL;
}
return PyLong_FromInt64(ts);
}

// Test _PyTime_FromSecondsObject()
static PyObject *
test_pytime_fromsecondsobject(PyObject *self, PyObject *args)
{
Expand All @@ -52,6 +73,7 @@ test_pytime_fromsecondsobject(PyObject *self, PyObject *args)
return PyLong_FromInt64(ts);
}

// Test _PyTime_AsTimeval()
static PyObject *
test_PyTime_AsTimeval(PyObject *self, PyObject *args)
{
Expand Down Expand Up @@ -79,6 +101,7 @@ test_PyTime_AsTimeval(PyObject *self, PyObject *args)
return Py_BuildValue("Nl", seconds, (long)tv.tv_usec);
}

// Test _PyTime_AsTimeval_clamup()
static PyObject *
test_PyTime_AsTimeval_clamp(PyObject *self, PyObject *args)
{
Expand All @@ -105,6 +128,7 @@ test_PyTime_AsTimeval_clamp(PyObject *self, PyObject *args)
}

#ifdef HAVE_CLOCK_GETTIME
// Test _PyTime_AsTimespec()
static PyObject *
test_PyTime_AsTimespec(PyObject *self, PyObject *args)
{
Expand All @@ -123,6 +147,7 @@ test_PyTime_AsTimespec(PyObject *self, PyObject *args)
return Py_BuildValue("Nl", _PyLong_FromTime_t(ts.tv_sec), ts.tv_nsec);
}

// Test _PyTime_AsTimespec_clamp()
static PyObject *
test_PyTime_AsTimespec_clamp(PyObject *self, PyObject *args)
{
Expand All @@ -140,6 +165,7 @@ test_PyTime_AsTimespec_clamp(PyObject *self, PyObject *args)
}
#endif

// Test _PyTime_AsMilliseconds()
static PyObject *
test_PyTime_AsMilliseconds(PyObject *self, PyObject *args)
{
Expand All @@ -159,6 +185,7 @@ test_PyTime_AsMilliseconds(PyObject *self, PyObject *args)
return PyLong_FromInt64(ms);
}

// Test _PyTime_AsMicroseconds()
static PyObject *
test_PyTime_AsMicroseconds(PyObject *self, PyObject *args)
{
Expand All @@ -178,6 +205,7 @@ test_PyTime_AsMicroseconds(PyObject *self, PyObject *args)
return PyLong_FromInt64(us);
}

// Test _PyTime_ObjectToTime_t()
static PyObject *
test_pytime_object_to_time_t(PyObject *self, PyObject *args)
{
Expand All @@ -196,6 +224,7 @@ test_pytime_object_to_time_t(PyObject *self, PyObject *args)
return _PyLong_FromTime_t(sec);
}

// Test _PyTime_ObjectToTimeval()
static PyObject *
test_pytime_object_to_timeval(PyObject *self, PyObject *args)
{
Expand All @@ -215,6 +244,7 @@ test_pytime_object_to_timeval(PyObject *self, PyObject *args)
return Py_BuildValue("Nl", _PyLong_FromTime_t(sec), usec);
}

// Test _PyTime_ObjectToTimespec()
static PyObject *
test_pytime_object_to_timespec(PyObject *self, PyObject *args)
{
Expand Down Expand Up @@ -244,6 +274,7 @@ static PyMethodDef TestMethods[] = {
{"_PyTime_AsTimeval", test_PyTime_AsTimeval, METH_VARARGS},
{"_PyTime_AsTimeval_clamp", test_PyTime_AsTimeval_clamp, METH_VARARGS},
{"_PyTime_FromSeconds", test_pytime_fromseconds, METH_VARARGS},
{"_PyTime_FromSecondsDouble", test_pytime_fromsecondsdouble, METH_VARARGS},
{"_PyTime_FromSecondsObject", test_pytime_fromsecondsobject, METH_VARARGS},
{"_PyTime_ObjectToTime_t", test_pytime_object_to_time_t, METH_VARARGS},
{"_PyTime_ObjectToTimespec", test_pytime_object_to_timespec, METH_VARARGS},
Expand Down
142 changes: 88 additions & 54 deletions Python/pytime.c
Original file line number Diff line number Diff line change
Expand Up @@ -116,18 +116,22 @@ pytime_overflow(void)
static inline int
pytime_add(PyTime_t *t1, PyTime_t t2)
{
if (t2 > 0 && *t1 > PyTime_MAX - t2) {
*t1 = PyTime_MAX;
return -1;
}
else if (t2 < 0 && *t1 < PyTime_MIN - t2) {
*t1 = PyTime_MIN;
return -1;
if (t2 >= 0) {
if (*t1 > PyTime_MAX - t2) {
*t1 = PyTime_MAX;
return -1;
}
}
else {
*t1 += t2;
return 0;
// t2 < 0
if (*t1 < PyTime_MIN - t2) {
*t1 = PyTime_MIN;
return -1;
}
}

*t1 += t2;
return 0;
}


Expand Down Expand Up @@ -402,10 +406,72 @@ pytime_round(double x, _PyTime_round_t round)
}


// Convert C double to C time_t.
// Set *sec and return 0 on success.
// Set OverflowError and return -1 on overflow.
//
// See also pytime_from_double_cast().
static inline int
pytime_double_to_time_t(time_t *sec, double value)
{
assert(!isnan(value));

/*
Conversion of an out-of-range value to time_t gives undefined behaviour
(C99 §6.3.1.4p1), so we must guard against it. However, checking that
`value` is in range is delicate: the obvious expression `value <=
PY_TIME_T_MAX` will first convert the value `PY_TIME_T_MAX` to a double,
potentially changing its value and leading to us failing to catch some
UB-inducing values. The code below works correctly under the mild
assumption that time_t is a two's complement integer type with no trap
representation, and that `PY_TIME_T_MIN` is within the representable
range of a C double.

Note: we want the `if` condition below to be true for NaNs; therefore,
resist any temptation to simplify by applying De Morgan's laws.
*/
const double time_t_min = (double)PY_TIME_T_MIN;
if (!(time_t_min <= value && value < -time_t_min)) {
pytime_time_t_overflow();
*sec = 0;
return -1;
}
*sec = (time_t)value;
return 0;
}


// Convert C double to C PyTime_t.
// Set *sec and return 0 on success.
// Set OverflowError and return -1 on overflow.
//
// See also pytime_double_to_time_t().
static inline int
pytime_from_double_cast(PyTime_t *tp, double value)
{
assert(!isnan(value));

/* See comments in pytime_double_to_time_t() */
const double time_min = (double)PyTime_MIN;
if (!(time_min <= value && value < -time_min)) {
pytime_overflow();
*tp = 0;
return -1;
}
*tp = (PyTime_t)value;
return 0;
}


static int
pytime_double_to_denominator(double d, time_t *sec, long *numerator,
long idenominator, _PyTime_round_t round)
{
if (isnan(d)) {
PyErr_SetString(PyExc_ValueError, "Invalid value NaN (not a number)");
return -1;
}

double denominator = (double)idenominator;
double intpart;
/* volatile avoids optimization changing how numbers are rounded */
Expand All @@ -425,25 +491,11 @@ pytime_double_to_denominator(double d, time_t *sec, long *numerator,
}
assert(0.0 <= floatpart && floatpart < denominator);

/*
Conversion of an out-of-range value to time_t gives undefined behaviour
(C99 §6.3.1.4p1), so we must guard against it. However, checking that
`intpart` is in range is delicate: the obvious expression `intpart <=
PY_TIME_T_MAX` will first convert the value `PY_TIME_T_MAX` to a double,
potentially changing its value and leading to us failing to catch some
UB-inducing values. The code below works correctly under the mild
assumption that time_t is a two's complement integer type with no trap
representation, and that `PY_TIME_T_MIN` is within the representable
range of a C double.

Note: we want the `if` condition below to be true for NaNs; therefore,
resist any temptation to simplify by applying De Morgan's laws.
*/
if (!((double)PY_TIME_T_MIN <= intpart && intpart < -(double)PY_TIME_T_MIN)) {
pytime_time_t_overflow();
if (pytime_double_to_time_t(sec, intpart) < 0) {
// overflow
return -1;
}
*sec = (time_t)intpart;

*numerator = (long)floatpart;
assert(0 <= *numerator && *numerator < idenominator);
return 0;
Expand Down Expand Up @@ -661,10 +713,6 @@ pytime_pyfloat_to_denominator(PyObject *obj, time_t *sec, long *numerator,
if (d == -1 && PyErr_Occurred()) {
return -1;
}
if (isnan(d)) {
PyErr_SetString(PyExc_ValueError, "Invalid value NaN (not a number)");
return -1;
}
return pytime_double_to_denominator(d, sec, numerator,
denominator, round);
}
Expand Down Expand Up @@ -726,13 +774,7 @@ pytime_pyfloat_to_time_t(PyObject *obj, time_t *sec, _PyTime_round_t round)
d = pytime_round(d, round);
(void)modf(d, &intpart);

/* See comments in pytime_double_to_denominator */
if (!((double)PY_TIME_T_MIN <= intpart && intpart < -(double)PY_TIME_T_MIN)) {
pytime_time_t_overflow();
return -1;
}
*sec = (time_t)intpart;
return 0;
return pytime_double_to_time_t(sec, intpart);
}


Expand Down Expand Up @@ -799,9 +841,9 @@ _PyTime_FromSeconds(int seconds)
static_assert(INT_MAX <= PyTime_MAX / SEC_TO_NS, "PyTime_t overflow");
static_assert(INT_MIN >= PyTime_MIN / SEC_TO_NS, "PyTime_t underflow");

Py_BUILD_ASSERT(sizeof(int) <= sizeof(PyTime_t));
PyTime_t t = (PyTime_t)seconds;
assert((t >= 0 && t <= PyTime_MAX / SEC_TO_NS)
|| (t < 0 && t >= PyTime_MIN / SEC_TO_NS));
assert(!pytime_mul_check_overflow(t, SEC_TO_NS));
t *= SEC_TO_NS;
return t;
}
Expand Down Expand Up @@ -882,6 +924,11 @@ static int
pytime_from_double(PyTime_t *tp, double value, _PyTime_round_t round,
long unit_to_ns)
{
if (isnan(value)) {
PyErr_SetString(PyExc_ValueError, "Invalid value NaN (not a number)");
return -1;
}

/* volatile avoids optimization changing how numbers are rounded */
volatile double d;

Expand All @@ -890,16 +937,7 @@ pytime_from_double(PyTime_t *tp, double value, _PyTime_round_t round,
d *= (double)unit_to_ns;
d = pytime_round(d, round);

/* See comments in pytime_double_to_denominator */
if (!((double)PyTime_MIN <= d && d < -(double)PyTime_MIN)) {
pytime_overflow();
*tp = 0;
return -1;
}
PyTime_t ns = (PyTime_t)d;

*tp = ns;
return 0;
return pytime_from_double_cast(tp, d);
}


Expand All @@ -911,10 +949,6 @@ pytime_from_pyfloat(PyTime_t *tp, PyObject *obj, _PyTime_round_t round,
if (d == -1 && PyErr_Occurred()) {
return -1;
}
if (isnan(d)) {
PyErr_SetString(PyExc_ValueError, "Invalid value NaN (not a number)");
return -1;
}
return pytime_from_double(tp, d, round, unit_to_ns);
}

Expand All @@ -933,7 +967,7 @@ pytime_from_object(PyTime_t *tp, PyObject *obj, _PyTime_round_t round,
}

static_assert(sizeof(long long) <= sizeof(PyTime_t),
"PyTime_t is smaller than long long");
"PyTime_t is smaller than long long");
PyTime_t ns = (PyTime_t)sec;
if (pytime_mul(&ns, unit_to_ns) < 0) {
pytime_overflow();
Expand Down
Loading