@@ -162,12 +162,11 @@ time_t
162
162
_PyLong_AsTime_t (PyObject * obj )
163
163
{
164
164
#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 );
167
168
#else
168
- long val ;
169
- Py_BUILD_ASSERT (sizeof (time_t ) <= sizeof (long ));
170
- val = PyLong_AsLong (obj );
169
+ # error "unsupported time_t size"
171
170
#endif
172
171
if (val == -1 && PyErr_Occurred ()) {
173
172
if (PyErr_ExceptionMatches (PyExc_OverflowError )) {
@@ -184,9 +183,10 @@ _PyLong_FromTime_t(time_t t)
184
183
{
185
184
#if SIZEOF_TIME_T == SIZEOF_LONG_LONG
186
185
return PyLong_FromLongLong ((long long )t );
187
- #else
188
- Py_BUILD_ASSERT (sizeof (time_t ) <= sizeof (long ));
186
+ #elif SIZEOF_TIME_T <= SIZEOF_LONG
189
187
return PyLong_FromLong ((long )t );
188
+ #else
189
+ # error "unsupported time_t size"
190
190
#endif
191
191
}
192
192
@@ -386,10 +386,10 @@ _PyTime_t
386
386
_PyTime_FromSeconds (int seconds )
387
387
{
388
388
/* 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
390
390
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" );
393
393
394
394
_PyTime_t t = (_PyTime_t )seconds ;
395
395
assert ((t >= 0 && t <= _PyTime_MAX / SEC_TO_NS )
@@ -416,7 +416,8 @@ _PyTime_FromNanosecondsObject(_PyTime_t *tp, PyObject *obj)
416
416
return -1 ;
417
417
}
418
418
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" );
420
421
long long nsec = PyLong_AsLongLong (obj );
421
422
if (nsec == -1 && PyErr_Occurred ()) {
422
423
if (PyErr_ExceptionMatches (PyExc_OverflowError )) {
@@ -437,7 +438,8 @@ pytime_fromtimespec(_PyTime_t *tp, struct timespec *ts, int raise_exc)
437
438
{
438
439
_PyTime_t t , tv_nsec ;
439
440
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" );
441
443
t = (_PyTime_t )ts -> tv_sec ;
442
444
443
445
int res1 = pytime_mul (& t , SEC_TO_NS );
@@ -466,7 +468,8 @@ _PyTime_FromTimespec(_PyTime_t *tp, struct timespec *ts)
466
468
static int
467
469
pytime_fromtimeval (_PyTime_t * tp , struct timeval * tv , int raise_exc )
468
470
{
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" );
470
473
_PyTime_t t = (_PyTime_t )tv -> tv_sec ;
471
474
472
475
int res1 = pytime_mul (& t , SEC_TO_NS );
@@ -537,7 +540,8 @@ pytime_from_object(_PyTime_t *tp, PyObject *obj, _PyTime_round_t round,
537
540
return -1 ;
538
541
}
539
542
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" );
541
545
_PyTime_t ns = (_PyTime_t )sec ;
542
546
if (pytime_mul (& ns , unit_to_ns ) < 0 ) {
543
547
pytime_overflow ();
@@ -589,7 +593,8 @@ PyObject *
589
593
_PyTime_AsNanosecondsObject (_PyTime_t t )
590
594
{
591
595
_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" );
593
598
return PyLong_FromLongLong ((long long )ns );
594
599
}
595
600
@@ -984,15 +989,17 @@ py_mach_timebase_info(_PyTime_t *pnumer, _PyTime_t *pdenom, int raise)
984
989
_PyTime_t. In practice, timebase uses uint32_t, so casting cannot
985
990
overflow. At the end, only make sure that the type is uint32_t
986
991
(_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" );
989
996
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 .
992
999
993
1000
Known time bases:
994
1001
995
- * always (1, 1) on Intel
1002
+ * (1, 1) on Intel
996
1003
* (1000000000, 33333335) or (1000000000, 25000000) on PowerPC
997
1004
998
1005
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)
1019
1026
1020
1027
#if defined(MS_WINDOWS )
1021
1028
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
+ }
1024
1040
1025
1041
int res = pytime_mul (& t , MS_TO_NS );
1026
1042
* tp = t ;
@@ -1211,7 +1227,8 @@ py_get_win_perf_counter(_PyTime_t *tp, _Py_clock_info_t *info, int raise_exc)
1211
1227
/* Make sure that casting LONGLONG to _PyTime_t cannot overflow,
1212
1228
both types are signed */
1213
1229
_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" );
1215
1232
ticks = (_PyTime_t )ticksll ;
1216
1233
1217
1234
_PyTime_t ns = _PyTime_MulDiv (ticks , SEC_TO_NS , (_PyTime_t )frequency );
0 commit comments