Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit cb0c602

Browse files
committed
Issue #22117: Fix _PyTime_GetMonotonicClock() and
_PyTime_GetSystemClockWithInfo() to not raise an exception and return 0 on error (it should never occur)
1 parent 02937aa commit cb0c602

1 file changed

Lines changed: 20 additions & 12 deletions

File tree

Python/pytime.c

Lines changed: 20 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -279,36 +279,41 @@ _PyTime_FromNanoseconds(PY_LONG_LONG ns)
279279

280280
#ifdef HAVE_CLOCK_GETTIME
281281
static int
282-
_PyTime_FromTimespec(_PyTime_t *tp, struct timespec *ts)
282+
_PyTime_FromTimespec(_PyTime_t *tp, struct timespec *ts, int raise)
283283
{
284284
_PyTime_t t;
285+
int res = 0;
286+
285287
t = (_PyTime_t)ts->tv_sec * SEC_TO_NS;
286288
if (t / SEC_TO_NS != ts->tv_sec) {
287-
_PyTime_overflow();
288-
return -1;
289+
if (raise)
290+
_PyTime_overflow();
291+
res = -1;
289292
}
290293

291294
t += ts->tv_nsec;
292295

293296
*tp = t;
294-
return 0;
297+
return res;
295298
}
296299
#else
297300
static int
298-
_PyTime_FromTimeval(_PyTime_t *tp, struct timeval *tv)
301+
_PyTime_FromTimeval(_PyTime_t *tp, struct timeval *tv, int raise)
299302
{
300303
_PyTime_t t;
304+
int res = 0;
301305

302306
t = (_PyTime_t)tv->tv_sec * SEC_TO_NS;
303307
if (t / SEC_TO_NS != tv->tv_sec) {
304-
_PyTime_overflow();
305-
return -1;
308+
if (raise)
309+
_PyTime_overflow();
310+
res = -1;
306311
}
307312

308313
t += (_PyTime_t)tv->tv_usec * US_TO_NS;
309314

310315
*tp = t;
311-
return 0;
316+
return res;
312317
}
313318
#endif
314319

@@ -532,7 +537,7 @@ pygettimeofday_new(_PyTime_t *tp, _Py_clock_info_t *info, int raise)
532537
PyErr_SetFromErrno(PyExc_OSError);
533538
return -1;
534539
}
535-
if (_PyTime_FromTimespec(tp, &ts) < 0)
540+
if (_PyTime_FromTimespec(tp, &ts, raise) < 0)
536541
return -1;
537542

538543
if (info) {
@@ -558,7 +563,7 @@ pygettimeofday_new(_PyTime_t *tp, _Py_clock_info_t *info, int raise)
558563
PyErr_SetFromErrno(PyExc_OSError);
559564
return -1;
560565
}
561-
if (_PyTime_FromTimeval(tp, &tv) < 0)
566+
if (_PyTime_FromTimeval(tp, &tv, raise) < 0)
562567
return -1;
563568

564569
if (info) {
@@ -674,7 +679,7 @@ pymonotonic_new(_PyTime_t *tp, _Py_clock_info_t *info, int raise)
674679
}
675680
info->resolution = res.tv_sec + res.tv_nsec * 1e-9;
676681
}
677-
if (_PyTime_FromTimespec(tp, &ts) < 0)
682+
if (_PyTime_FromTimespec(tp, &ts, raise) < 0)
678683
return -1;
679684
#endif
680685
#ifdef Py_DEBUG
@@ -691,8 +696,11 @@ _PyTime_GetMonotonicClock(void)
691696
{
692697
_PyTime_t t;
693698
if (pymonotonic_new(&t, NULL, 0) < 0) {
694-
/* cannot happen, _PyTime_Init() checks that pymonotonic_new() works */
699+
/* should not happen, _PyTime_Init() checked that monotonic clock at
700+
startup */
695701
assert(0);
702+
703+
/* use a fixed value instead of a random value from the stack */
696704
t = 0;
697705
}
698706
return t;

0 commit comments

Comments
 (0)