@@ -162,12 +162,11 @@ time_t
162162_PyLong_AsTime_t (PyObject * obj )
163163{
164164#if SIZEOF_TIME_T == SIZEOF_LONG_LONG
165- long long val ;
166- val = PyLong_AsLongLong (obj );
165+ long long val = PyLong_AsLongLong (obj );
166+ #elif SIZEOF_TIME_T <= SIZEOF_LONG
167+ long val = PyLong_AsLong (obj );
167168#else
168- long val ;
169- Py_BUILD_ASSERT (sizeof (time_t ) <= sizeof (long ));
170- val = PyLong_AsLong (obj );
169+ # error "unsupported time_t size"
171170#endif
172171 if (val == -1 && PyErr_Occurred ()) {
173172 if (PyErr_ExceptionMatches (PyExc_OverflowError )) {
@@ -184,9 +183,10 @@ _PyLong_FromTime_t(time_t t)
184183{
185184#if SIZEOF_TIME_T == SIZEOF_LONG_LONG
186185 return PyLong_FromLongLong ((long long )t );
187- #else
188- Py_BUILD_ASSERT (sizeof (time_t ) <= sizeof (long ));
186+ #elif SIZEOF_TIME_T <= SIZEOF_LONG
189187 return PyLong_FromLong ((long )t );
188+ #else
189+ # error "unsupported time_t size"
190190#endif
191191}
192192
@@ -386,10 +386,10 @@ _PyTime_t
386386_PyTime_FromSeconds (int seconds )
387387{
388388 /* ensure that integer overflow cannot happen, int type should have 32
389- bits, whereas _PyTime_t type has at least 64 bits (SEC_TO_MS takes 30
389+ bits, whereas _PyTime_t type has at least 64 bits (SEC_TO_NS takes 30
390390 bits). */
391- Py_BUILD_ASSERT (INT_MAX <= _PyTime_MAX / SEC_TO_NS );
392- Py_BUILD_ASSERT (INT_MIN >= _PyTime_MIN / SEC_TO_NS );
391+ static_assert (INT_MAX <= _PyTime_MAX / SEC_TO_NS , "_PyTime_t overflow" );
392+ static_assert (INT_MIN >= _PyTime_MIN / SEC_TO_NS , "_PyTime_t underflow" );
393393
394394 _PyTime_t t = (_PyTime_t )seconds ;
395395 assert ((t >= 0 && t <= _PyTime_MAX / SEC_TO_NS )
@@ -416,7 +416,8 @@ _PyTime_FromNanosecondsObject(_PyTime_t *tp, PyObject *obj)
416416 return -1 ;
417417 }
418418
419- Py_BUILD_ASSERT (sizeof (long long ) == sizeof (_PyTime_t ));
419+ static_assert (sizeof (long long ) == sizeof (_PyTime_t ),
420+ "_PyTime_t is not long long" );
420421 long long nsec = PyLong_AsLongLong (obj );
421422 if (nsec == -1 && PyErr_Occurred ()) {
422423 if (PyErr_ExceptionMatches (PyExc_OverflowError )) {
@@ -437,7 +438,8 @@ pytime_fromtimespec(_PyTime_t *tp, struct timespec *ts, int raise_exc)
437438{
438439 _PyTime_t t , tv_nsec ;
439440
440- Py_BUILD_ASSERT (sizeof (ts -> tv_sec ) <= sizeof (_PyTime_t ));
441+ static_assert (sizeof (ts -> tv_sec ) <= sizeof (_PyTime_t ),
442+ "timespec.tv_sec is larger than _PyTime_t" );
441443 t = (_PyTime_t )ts -> tv_sec ;
442444
443445 int res1 = pytime_mul (& t , SEC_TO_NS );
@@ -466,7 +468,8 @@ _PyTime_FromTimespec(_PyTime_t *tp, struct timespec *ts)
466468static int
467469pytime_fromtimeval (_PyTime_t * tp , struct timeval * tv , int raise_exc )
468470{
469- Py_BUILD_ASSERT (sizeof (tv -> tv_sec ) <= sizeof (_PyTime_t ));
471+ static_assert (sizeof (tv -> tv_sec ) <= sizeof (_PyTime_t ),
472+ "timeval.tv_sec is larger than _PyTime_t" );
470473 _PyTime_t t = (_PyTime_t )tv -> tv_sec ;
471474
472475 int res1 = pytime_mul (& t , SEC_TO_NS );
@@ -537,7 +540,8 @@ pytime_from_object(_PyTime_t *tp, PyObject *obj, _PyTime_round_t round,
537540 return -1 ;
538541 }
539542
540- Py_BUILD_ASSERT (sizeof (long long ) <= sizeof (_PyTime_t ));
543+ static_assert (sizeof (long long ) <= sizeof (_PyTime_t ),
544+ "_PyTime_t is smaller than long long" );
541545 _PyTime_t ns = (_PyTime_t )sec ;
542546 if (pytime_mul (& ns , unit_to_ns ) < 0 ) {
543547 pytime_overflow ();
@@ -589,7 +593,8 @@ PyObject *
589593_PyTime_AsNanosecondsObject (_PyTime_t t )
590594{
591595 _PyTime_t ns = pytime_as_nanoseconds (t );
592- Py_BUILD_ASSERT (sizeof (long long ) >= sizeof (_PyTime_t ));
596+ static_assert (sizeof (long long ) >= sizeof (_PyTime_t ),
597+ "_PyTime_t is larger than long long" );
593598 return PyLong_FromLongLong ((long long )ns );
594599}
595600
@@ -984,15 +989,17 @@ py_mach_timebase_info(_PyTime_t *pnumer, _PyTime_t *pdenom, int raise)
984989 _PyTime_t. In practice, timebase uses uint32_t, so casting cannot
985990 overflow. At the end, only make sure that the type is uint32_t
986991 (_PyTime_t is 64-bit long). */
987- Py_BUILD_ASSERT (sizeof (timebase .numer ) < sizeof (_PyTime_t ));
988- Py_BUILD_ASSERT (sizeof (timebase .denom ) < sizeof (_PyTime_t ));
992+ static_assert (sizeof (timebase .numer ) <= sizeof (_PyTime_t ),
993+ "timebase.numer is larger than _PyTime_t" );
994+ static_assert (sizeof (timebase .denom ) <= sizeof (_PyTime_t ),
995+ "timebase.denom is larger than _PyTime_t" );
989996
990- /* Make sure that (ticks * timebase.numer) cannot overflow in
991- _PyTime_MulDiv(), with ticks < timebase.denom .
997+ /* Make sure that _PyTime_MulDiv (ticks, timebase_numer, timebase_denom)
998+ cannot overflow .
992999
9931000 Known time bases:
9941001
995- * always (1, 1) on Intel
1002+ * (1, 1) on Intel
9961003 * (1000000000, 33333335) or (1000000000, 25000000) on PowerPC
9971004
9981005 None of these time bases can overflow with 64-bit _PyTime_t, but
@@ -1019,8 +1026,17 @@ py_get_monotonic_clock(_PyTime_t *tp, _Py_clock_info_t *info, int raise_exc)
10191026
10201027#if defined(MS_WINDOWS )
10211028 ULONGLONG ticks = GetTickCount64 ();
1022- Py_BUILD_ASSERT (sizeof (ticks ) <= sizeof (_PyTime_t ));
1023- _PyTime_t t = (_PyTime_t )ticks ;
1029+ static_assert (sizeof (ticks ) <= sizeof (_PyTime_t ),
1030+ "ULONGLONG is larger than _PyTime_t" );
1031+ _PyTime_t t ;
1032+ if (ticks <= (ULONGLONG )_PyTime_MAX ) {
1033+ t = (_PyTime_t )ticks ;
1034+ }
1035+ else {
1036+ // GetTickCount64() maximum is larger than _PyTime_t maximum:
1037+ // ULONGLONG is unsigned, whereas _PyTime_t is signed.
1038+ t = _PyTime_MAX ;
1039+ }
10241040
10251041 int res = pytime_mul (& t , MS_TO_NS );
10261042 * tp = t ;
@@ -1211,7 +1227,8 @@ py_get_win_perf_counter(_PyTime_t *tp, _Py_clock_info_t *info, int raise_exc)
12111227 /* Make sure that casting LONGLONG to _PyTime_t cannot overflow,
12121228 both types are signed */
12131229 _PyTime_t ticks ;
1214- Py_BUILD_ASSERT (sizeof (ticksll ) <= sizeof (ticks ));
1230+ static_assert (sizeof (ticksll ) <= sizeof (ticks ),
1231+ "LONGLONG is larger than _PyTime_t" );
12151232 ticks = (_PyTime_t )ticksll ;
12161233
12171234 _PyTime_t ns = _PyTime_MulDiv (ticks , SEC_TO_NS , (_PyTime_t )frequency );
0 commit comments