diff options
Diffstat (limited to 'Python')
-rw-r--r-- | Python/pylifecycle.c | 6 | ||||
-rw-r--r-- | Python/pytime.c | 17 |
2 files changed, 20 insertions, 3 deletions
diff --git a/Python/pylifecycle.c b/Python/pylifecycle.c index 38543e05ef..bab3a2f38a 100644 --- a/Python/pylifecycle.c +++ b/Python/pylifecycle.c @@ -405,15 +405,15 @@ _Py_InitializeEx_Private(int install_sigs, int install_importlib) if (!install_importlib) return; + if (_PyTime_Init() < 0) + Py_FatalError("Py_Initialize: can't initialize time"); + import_init(interp, sysmod); /* initialize the faulthandler module */ if (_PyFaulthandler_Init()) Py_FatalError("Py_Initialize: can't initialize faulthandler"); - if (_PyTime_Init() < 0) - Py_FatalError("Py_Initialize: can't initialize time"); - if (initfsencoding(interp) < 0) Py_FatalError("Py_Initialize: unable to load the file system codec"); diff --git a/Python/pytime.c b/Python/pytime.c index 491bbea611..02a93749c7 100644 --- a/Python/pytime.c +++ b/Python/pytime.c @@ -159,6 +159,19 @@ _PyTime_overflow(void) } _PyTime_t +_PyTime_FromSeconds(int seconds) +{ + _PyTime_t t; + /* ensure that integer overflow cannot happen, int type should have 32 + bits, whereas _PyTime_t type has at least 64 bits (SEC_TO_MS takes 30 + bits). */ + assert((seconds >= 0 && seconds <= _PyTime_MAX / SEC_TO_NS) + || (seconds < 0 && seconds >= _PyTime_MIN / SEC_TO_NS)); + t = (_PyTime_t)seconds * SEC_TO_NS; + return t; +} + +_PyTime_t _PyTime_FromNanoseconds(PY_LONG_LONG ns) { _PyTime_t t; @@ -657,5 +670,9 @@ _PyTime_Init(void) /* ensure that the operating system provides a monotonic clock */ if (_PyTime_GetMonotonicClockWithInfo(&t, NULL) < 0) return -1; + + /* check that _PyTime_FromSeconds() cannot overflow */ + assert(INT_MAX <= _PyTime_MAX / SEC_TO_NS); + assert(INT_MIN >= _PyTime_MIN / SEC_TO_NS); return 0; } |