diff --git a/polymer/eduke32/build/include/compat.h b/polymer/eduke32/build/include/compat.h index 7728860f0..add4e02fa 100644 --- a/polymer/eduke32/build/include/compat.h +++ b/polymer/eduke32/build/include/compat.h @@ -728,5 +728,16 @@ static inline char *Bstrncpyz(char *dst, const char *src, bsize_t n) #define initprintf_nowarn(fmt, ...) NOWARN(initprintf, fmt, ## __VA_ARGS__) #define message_nowarn(fmt, ...) NOWARN(message, fmt, ## __VA_ARGS__) +/* Static assertions, based on source found in LuaJIT's src/lj_def.h. */ +#define EDUKE32_ASSERT_NAME2(name, line) name ## line +#define EDUKE32_ASSERT_NAME(line) EDUKE32_ASSERT_NAME2(eduke32_assert_, line) +#ifdef __COUNTER__ +# define EDUKE32_STATIC_ASSERT(cond) \ + extern void EDUKE32_ASSERT_NAME(__COUNTER__)(int STATIC_ASSERTION_FAILED[(cond)?1:-1]) +#else +# define EDUKE32_STATIC_ASSERT(cond) \ + extern void EDUKE32_ASSERT_NAME(__LINE__)(int STATIC_ASSERTION_FAILED[(cond)?1:-1]) +#endif + #endif // __compat_h__ diff --git a/polymer/eduke32/source/animvpx.c b/polymer/eduke32/source/animvpx.c index 6f6396e09..8a19df028 100644 --- a/polymer/eduke32/source/animvpx.c +++ b/polymer/eduke32/source/animvpx.c @@ -28,12 +28,12 @@ const char *animvpx_read_ivf_header_errmsg[] = { "invalid framerate numerator or denominator after correction, must not be 0", }; +EDUKE32_STATIC_ASSERT(sizeof(animvpx_ivf_header_t) == 32); + int32_t animvpx_read_ivf_header(int32_t inhandle, animvpx_ivf_header_t *hdr) { int32_t err; - Bassert(sizeof(animvpx_ivf_header_t) == 32); - if (kread(inhandle, hdr, sizeof(animvpx_ivf_header_t)) != sizeof(animvpx_ivf_header_t)) return 1; // "couldn't read header" diff --git a/polymer/eduke32/source/game.c b/polymer/eduke32/source/game.c index 24ad0215b..75d234fcc 100644 --- a/polymer/eduke32/source/game.c +++ b/polymer/eduke32/source/game.c @@ -10113,6 +10113,9 @@ void G_MaybeAllocPlayer(int32_t pnum) #endif } +EDUKE32_STATIC_ASSERT(sizeof(actor_t)==128); +EDUKE32_STATIC_ASSERT(sizeof(DukePlayer_t)%4 == 0); + int32_t app_main(int32_t argc, const char **argv) { int32_t i = 0, j; @@ -10124,9 +10127,6 @@ int32_t app_main(int32_t argc, const char **argv) ENetCallbacks callbacks = { NULL, NULL, NULL }; #endif - Bassert(sizeof(actor_t)==128); - Bassert(sizeof(DukePlayer_t)%4 == 0); - G_ExtPreInit(); #ifdef _WIN32 diff --git a/polymer/eduke32/source/gamedef.c b/polymer/eduke32/source/gamedef.c index e69b544bb..341e7efa2 100644 --- a/polymer/eduke32/source/gamedef.c +++ b/polymer/eduke32/source/gamedef.c @@ -6119,9 +6119,7 @@ void C_InitProjectiles(void) 0, 1, 32, {0,0,0,0,0,0}, }; - // this will only happen if I forget to update this function... - if (sizeof(projectile_t) != sizeof(DefaultProjectile)) - G_GameExit("ERROR: C_InitProjectiles(): projectile_t mismatch!"); + EDUKE32_STATIC_ASSERT(sizeof(projectile_t) == sizeof(DefaultProjectile)); for (i=MAXTILES-1; i>=0; i--) { diff --git a/polymer/eduke32/source/savegame.c b/polymer/eduke32/source/savegame.c index d1fa081be..f75cd0639 100644 --- a/polymer/eduke32/source/savegame.c +++ b/polymer/eduke32/source/savegame.c @@ -1126,13 +1126,13 @@ static int32_t doallocsnap(int32_t allocinit) } +EDUKE32_STATIC_ASSERT(sizeof(savehead_t) == 310); + // make snapshot only if spot < 0 (demo) int32_t sv_saveandmakesnapshot(FILE *fil, int8_t spot, int8_t recdiffsp, int8_t diffcompress, int8_t synccompress) { savehead_t h; - Bassert(sizeof(savehead_t) == 310); - // set a few savegame system globals savegame_comprthres = SV_DEFAULTCOMPRTHRES; savegame_diffcompress = diffcompress; @@ -1237,13 +1237,14 @@ int32_t sv_saveandmakesnapshot(FILE *fil, int8_t spot, int8_t recdiffsp, int8_t return 0; } + +EDUKE32_STATIC_ASSERT(sizeof(savehead_t) == 310); + // if file is not an EDuke32 savegame/demo, h->headerstr will be all zeros int32_t sv_loadheader(int32_t fil, int32_t spot, savehead_t *h) { int32_t havedemo = (spot < 0); - Bassert(sizeof(savehead_t) == 310); - if (kread(fil, h, sizeof(savehead_t)) != sizeof(savehead_t)) { OSD_Printf("%s %d header corrupt.\n", havedemo ? "Demo":"Savegame", havedemo ? -spot : spot);