Commit 11f61537 authored by Victor Stinner's avatar Victor Stinner

Issue #22117: Fix _PyTime_GetMonotonicClock() and

_PyTime_GetSystemClockWithInfo() to not raise an exception and return 0 on
error (it should never occur)
parent f284a0f0
...@@ -279,36 +279,41 @@ _PyTime_FromNanoseconds(PY_LONG_LONG ns) ...@@ -279,36 +279,41 @@ _PyTime_FromNanoseconds(PY_LONG_LONG ns)
#ifdef HAVE_CLOCK_GETTIME #ifdef HAVE_CLOCK_GETTIME
static int static int
_PyTime_FromTimespec(_PyTime_t *tp, struct timespec *ts) _PyTime_FromTimespec(_PyTime_t *tp, struct timespec *ts, int raise)
{ {
_PyTime_t t; _PyTime_t t;
int res = 0;
t = (_PyTime_t)ts->tv_sec * SEC_TO_NS; t = (_PyTime_t)ts->tv_sec * SEC_TO_NS;
if (t / SEC_TO_NS != ts->tv_sec) { if (t / SEC_TO_NS != ts->tv_sec) {
_PyTime_overflow(); if (raise)
return -1; _PyTime_overflow();
res = -1;
} }
t += ts->tv_nsec; t += ts->tv_nsec;
*tp = t; *tp = t;
return 0; return res;
} }
#else #else
static int static int
_PyTime_FromTimeval(_PyTime_t *tp, struct timeval *tv) _PyTime_FromTimeval(_PyTime_t *tp, struct timeval *tv, int raise)
{ {
_PyTime_t t; _PyTime_t t;
int res = 0;
t = (_PyTime_t)tv->tv_sec * SEC_TO_NS; t = (_PyTime_t)tv->tv_sec * SEC_TO_NS;
if (t / SEC_TO_NS != tv->tv_sec) { if (t / SEC_TO_NS != tv->tv_sec) {
_PyTime_overflow(); if (raise)
return -1; _PyTime_overflow();
res = -1;
} }
t += (_PyTime_t)tv->tv_usec * US_TO_NS; t += (_PyTime_t)tv->tv_usec * US_TO_NS;
*tp = t; *tp = t;
return 0; return res;
} }
#endif #endif
...@@ -532,7 +537,7 @@ pygettimeofday_new(_PyTime_t *tp, _Py_clock_info_t *info, int raise) ...@@ -532,7 +537,7 @@ pygettimeofday_new(_PyTime_t *tp, _Py_clock_info_t *info, int raise)
PyErr_SetFromErrno(PyExc_OSError); PyErr_SetFromErrno(PyExc_OSError);
return -1; return -1;
} }
if (_PyTime_FromTimespec(tp, &ts) < 0) if (_PyTime_FromTimespec(tp, &ts, raise) < 0)
return -1; return -1;
if (info) { if (info) {
...@@ -558,7 +563,7 @@ pygettimeofday_new(_PyTime_t *tp, _Py_clock_info_t *info, int raise) ...@@ -558,7 +563,7 @@ pygettimeofday_new(_PyTime_t *tp, _Py_clock_info_t *info, int raise)
PyErr_SetFromErrno(PyExc_OSError); PyErr_SetFromErrno(PyExc_OSError);
return -1; return -1;
} }
if (_PyTime_FromTimeval(tp, &tv) < 0) if (_PyTime_FromTimeval(tp, &tv, raise) < 0)
return -1; return -1;
if (info) { if (info) {
...@@ -674,7 +679,7 @@ pymonotonic_new(_PyTime_t *tp, _Py_clock_info_t *info, int raise) ...@@ -674,7 +679,7 @@ pymonotonic_new(_PyTime_t *tp, _Py_clock_info_t *info, int raise)
} }
info->resolution = res.tv_sec + res.tv_nsec * 1e-9; info->resolution = res.tv_sec + res.tv_nsec * 1e-9;
} }
if (_PyTime_FromTimespec(tp, &ts) < 0) if (_PyTime_FromTimespec(tp, &ts, raise) < 0)
return -1; return -1;
#endif #endif
#ifdef Py_DEBUG #ifdef Py_DEBUG
...@@ -691,8 +696,11 @@ _PyTime_GetMonotonicClock(void) ...@@ -691,8 +696,11 @@ _PyTime_GetMonotonicClock(void)
{ {
_PyTime_t t; _PyTime_t t;
if (pymonotonic_new(&t, NULL, 0) < 0) { if (pymonotonic_new(&t, NULL, 0) < 0) {
/* cannot happen, _PyTime_Init() checks that pymonotonic_new() works */ /* should not happen, _PyTime_Init() checked that monotonic clock at
startup */
assert(0); assert(0);
/* use a fixed value instead of a random value from the stack */
t = 0; t = 0;
} }
return t; return t;
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment