Skip to content

Commit fad85aa

Browse files
Issue #25558: Use compile-time asserts.
1 parent 41a8763 commit fad85aa

File tree

7 files changed

+24
-20
lines changed

7 files changed

+24
-20
lines changed

Include/pymacro.h

+4
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,10 @@
3636
#define Py_BUILD_ASSERT_EXPR(cond) \
3737
(sizeof(char [1 - 2*!(cond)]) - 1)
3838

39+
#define Py_BUILD_ASSERT(cond) do { \
40+
(void)Py_BUILD_ASSERT_EXPR(cond); \
41+
} while(0)
42+
3943
/* Get the number of elements in a visible array
4044
4145
This does not work on pointers, or arrays declared as [], or function

Modules/_ctypes/_ctypes.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -2386,7 +2386,7 @@ unique_key(CDataObject *target, Py_ssize_t index)
23862386
char *cp = string;
23872387
size_t bytes_left;
23882388

2389-
assert(sizeof(string) - 1 > sizeof(Py_ssize_t) * 2);
2389+
Py_BUILD_ASSERT(sizeof(string) - 1 > sizeof(Py_ssize_t) * 2);
23902390
cp += sprintf(cp, "%x", Py_SAFE_DOWNCAST(index, Py_ssize_t, int));
23912391
while (target->b_base) {
23922392
bytes_left = sizeof(string) - (cp - string) - 1;

Modules/_datetimemodule.c

+3-3
Original file line numberDiff line numberDiff line change
@@ -5329,19 +5329,19 @@ PyInit__datetime(void)
53295329
/* A 4-year cycle has an extra leap day over what we'd get from
53305330
* pasting together 4 single years.
53315331
*/
5332-
assert(DI4Y == 4 * 365 + 1);
5332+
Py_BUILD_ASSERT(DI4Y == 4 * 365 + 1);
53335333
assert(DI4Y == days_before_year(4+1));
53345334

53355335
/* Similarly, a 400-year cycle has an extra leap day over what we'd
53365336
* get from pasting together 4 100-year cycles.
53375337
*/
5338-
assert(DI400Y == 4 * DI100Y + 1);
5338+
Py_BUILD_ASSERT(DI400Y == 4 * DI100Y + 1);
53395339
assert(DI400Y == days_before_year(400+1));
53405340

53415341
/* OTOH, a 100-year cycle has one fewer leap day than we'd get from
53425342
* pasting together 25 4-year cycles.
53435343
*/
5344-
assert(DI100Y == 25 * DI4Y - 1);
5344+
Py_BUILD_ASSERT(DI100Y == 25 * DI4Y - 1);
53455345
assert(DI100Y == days_before_year(100+1));
53465346

53475347
one = PyLong_FromLong(1);

Modules/_pickle.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -874,7 +874,7 @@ _write_size64(char *out, size_t value)
874874
{
875875
size_t i;
876876

877-
assert(sizeof(size_t) <= 8);
877+
Py_BUILD_ASSERT(sizeof(size_t) <= 8);
878878

879879
for (i = 0; i < sizeof(size_t); i++) {
880880
out[i] = (unsigned char)((value >> (8 * i)) & 0xff);

Modules/pyexpat.c

+2-1
Original file line numberDiff line numberDiff line change
@@ -747,7 +747,8 @@ pyexpat_xmlparser_Parse_impl(xmlparseobject *self, PyObject *data,
747747
s += MAX_CHUNK_SIZE;
748748
slen -= MAX_CHUNK_SIZE;
749749
}
750-
assert(MAX_CHUNK_SIZE < INT_MAX && slen < INT_MAX);
750+
Py_BUILD_ASSERT(MAX_CHUNK_SIZE <= INT_MAX);
751+
assert(slen <= INT_MAX);
751752
rc = XML_Parse(self->itself, s, (int)slen, isfinal);
752753

753754
done:

Python/pytime.c

+12-13
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ _PyLong_AsTime_t(PyObject *obj)
4343
val = PyLong_AsLongLong(obj);
4444
#else
4545
long val;
46-
assert(sizeof(time_t) <= sizeof(long));
46+
Py_BUILD_ASSERT(sizeof(time_t) <= sizeof(long));
4747
val = PyLong_AsLong(obj);
4848
#endif
4949
if (val == -1 && PyErr_Occurred()) {
@@ -60,7 +60,7 @@ _PyLong_FromTime_t(time_t t)
6060
#if defined(HAVE_LONG_LONG) && SIZEOF_TIME_T == SIZEOF_LONG_LONG
6161
return PyLong_FromLongLong((PY_LONG_LONG)t);
6262
#else
63-
assert(sizeof(time_t) <= sizeof(long));
63+
Py_BUILD_ASSERT(sizeof(time_t) <= sizeof(long));
6464
return PyLong_FromLong((long)t);
6565
#endif
6666
}
@@ -209,6 +209,8 @@ _PyTime_FromSeconds(int seconds)
209209
/* ensure that integer overflow cannot happen, int type should have 32
210210
bits, whereas _PyTime_t type has at least 64 bits (SEC_TO_MS takes 30
211211
bits). */
212+
Py_BUILD_ASSERT(INT_MAX <= _PyTime_MAX / SEC_TO_NS);
213+
Py_BUILD_ASSERT(INT_MIN >= _PyTime_MIN / SEC_TO_NS);
212214
assert((t >= 0 && t <= _PyTime_MAX / SEC_TO_NS)
213215
|| (t < 0 && t >= _PyTime_MIN / SEC_TO_NS));
214216
t *= SEC_TO_NS;
@@ -219,7 +221,7 @@ _PyTime_t
219221
_PyTime_FromNanoseconds(PY_LONG_LONG ns)
220222
{
221223
_PyTime_t t;
222-
assert(sizeof(PY_LONG_LONG) <= sizeof(_PyTime_t));
224+
Py_BUILD_ASSERT(sizeof(PY_LONG_LONG) <= sizeof(_PyTime_t));
223225
t = Py_SAFE_DOWNCAST(ns, PY_LONG_LONG, _PyTime_t);
224226
return t;
225227
}
@@ -231,7 +233,7 @@ _PyTime_FromTimespec(_PyTime_t *tp, struct timespec *ts, int raise)
231233
_PyTime_t t;
232234
int res = 0;
233235

234-
assert(sizeof(ts->tv_sec) <= sizeof(_PyTime_t));
236+
Py_BUILD_ASSERT(sizeof(ts->tv_sec) <= sizeof(_PyTime_t));
235237
t = (_PyTime_t)ts->tv_sec;
236238

237239
if (_PyTime_check_mul_overflow(t, SEC_TO_NS)) {
@@ -253,7 +255,7 @@ _PyTime_FromTimeval(_PyTime_t *tp, struct timeval *tv, int raise)
253255
_PyTime_t t;
254256
int res = 0;
255257

256-
assert(sizeof(tv->tv_sec) <= sizeof(_PyTime_t));
258+
Py_BUILD_ASSERT(sizeof(tv->tv_sec) <= sizeof(_PyTime_t));
257259
t = (_PyTime_t)tv->tv_sec;
258260

259261
if (_PyTime_check_mul_overflow(t, SEC_TO_NS)) {
@@ -304,12 +306,12 @@ _PyTime_FromObject(_PyTime_t *t, PyObject *obj, _PyTime_round_t round,
304306
else {
305307
#ifdef HAVE_LONG_LONG
306308
PY_LONG_LONG sec;
307-
assert(sizeof(PY_LONG_LONG) <= sizeof(_PyTime_t));
309+
Py_BUILD_ASSERT(sizeof(PY_LONG_LONG) <= sizeof(_PyTime_t));
308310

309311
sec = PyLong_AsLongLong(obj);
310312
#else
311313
long sec;
312-
assert(sizeof(PY_LONG_LONG) <= sizeof(_PyTime_t));
314+
Py_BUILD_ASSERT(sizeof(PY_LONG_LONG) <= sizeof(_PyTime_t));
313315

314316
sec = PyLong_AsLong(obj);
315317
#endif
@@ -364,10 +366,10 @@ PyObject *
364366
_PyTime_AsNanosecondsObject(_PyTime_t t)
365367
{
366368
#ifdef HAVE_LONG_LONG
367-
assert(sizeof(PY_LONG_LONG) >= sizeof(_PyTime_t));
369+
Py_BUILD_ASSERT(sizeof(PY_LONG_LONG) >= sizeof(_PyTime_t));
368370
return PyLong_FromLongLong((PY_LONG_LONG)t);
369371
#else
370-
assert(sizeof(long) >= sizeof(_PyTime_t));
372+
Py_BUILD_ASSERT(sizeof(long) >= sizeof(_PyTime_t));
371373
return PyLong_FromLong((long)t);
372374
#endif
373375
}
@@ -650,7 +652,7 @@ pymonotonic(_PyTime_t *tp, _Py_clock_info_t *info, int raise)
650652
assert(info == NULL || raise);
651653

652654
ticks = GetTickCount64();
653-
assert(sizeof(ticks) <= sizeof(_PyTime_t));
655+
Py_BUILD_ASSERT(sizeof(ticks) <= sizeof(_PyTime_t));
654656
t = (_PyTime_t)ticks;
655657

656658
if (_PyTime_check_mul_overflow(t, MS_TO_NS)) {
@@ -774,8 +776,5 @@ _PyTime_Init(void)
774776
if (_PyTime_GetMonotonicClockWithInfo(&t, NULL) < 0)
775777
return -1;
776778

777-
/* check that _PyTime_FromSeconds() cannot overflow */
778-
assert(INT_MAX <= _PyTime_MAX / SEC_TO_NS);
779-
assert(INT_MIN >= _PyTime_MIN / SEC_TO_NS);
780779
return 0;
781780
}

Python/random.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -379,7 +379,7 @@ _PyRandom_Init(void)
379379
char *env;
380380
unsigned char *secret = (unsigned char *)&_Py_HashSecret.uc;
381381
Py_ssize_t secret_size = sizeof(_Py_HashSecret_t);
382-
assert(secret_size == sizeof(_Py_HashSecret.uc));
382+
Py_BUILD_ASSERT(sizeof(_Py_HashSecret_t) == sizeof(_Py_HashSecret.uc));
383383

384384
if (_Py_HashSecret_Initialized)
385385
return;

0 commit comments

Comments
 (0)