From 2ddde836011cf1620ea35b9ad8293b2ff44454e0 Mon Sep 17 00:00:00 2001 From: Monster Iestyn Date: Sun, 1 May 2016 22:14:42 +0100 Subject: [PATCH] General improvements to Lua error messages for out-of-bounds stuff. The idea is for the layman Lua user to understand better what range of values to use for mobj types, states, sfxs, player #s etc. Additionally, mobjinfo/states/sfxinfo/hudinfo tables all now have actual bound checks when accessing/editing them. Yikes, why didn't they have any before?! --- src/lua_baselib.c | 61 +++++++++++++++++++++++++++------------------ src/lua_hudlib.c | 2 ++ src/lua_infolib.c | 27 +++++++++++++++++--- src/lua_playerlib.c | 2 +- src/lua_skinlib.c | 2 +- 5 files changed, 65 insertions(+), 29 deletions(-) diff --git a/src/lua_baselib.c b/src/lua_baselib.c index 1488f4024..525f2fe68 100644 --- a/src/lua_baselib.c +++ b/src/lua_baselib.c @@ -295,8 +295,8 @@ static int lib_pSpawnMobj(lua_State *L) fixed_t z = luaL_checkfixed(L, 3); mobjtype_t type = luaL_checkinteger(L, 4); NOHUD - if (type > MT_LASTFREESLOT) - return luaL_error(L, "mobjtype_t out of bounds error!"); + if (type >= NUMMOBJTYPES) + return luaL_error(L, "mobj type %d out of range (0 - %d)", type, NUMMOBJTYPES-1); LUA_PushUserdata(L, P_SpawnMobj(x, y, z, type), META_MOBJ); return 1; } @@ -321,8 +321,8 @@ static int lib_pSpawnMissile(lua_State *L) NOHUD if (!source || !dest) return LUA_ErrInvalid(L, "mobj_t"); - if (type > MT_LASTFREESLOT) - return luaL_error(L, "mobjtype_t out of bounds error!"); + if (type >= NUMMOBJTYPES) + return luaL_error(L, "mobj type %d out of range (0 - %d)", type, NUMMOBJTYPES-1); LUA_PushUserdata(L, P_SpawnMissile(source, dest, type), META_MOBJ); return 1; } @@ -338,8 +338,8 @@ static int lib_pSpawnXYZMissile(lua_State *L) NOHUD if (!source || !dest) return LUA_ErrInvalid(L, "mobj_t"); - if (type > MT_LASTFREESLOT) - return luaL_error(L, "mobjtype_t out of bounds error!"); + if (type >= NUMMOBJTYPES) + return luaL_error(L, "mobj type %d out of range (0 - %d)", type, NUMMOBJTYPES-1); LUA_PushUserdata(L, P_SpawnXYZMissile(source, dest, type, x, y, z), META_MOBJ); return 1; } @@ -357,8 +357,8 @@ static int lib_pSpawnPointMissile(lua_State *L) NOHUD if (!source) return LUA_ErrInvalid(L, "mobj_t"); - if (type > MT_LASTFREESLOT) - return luaL_error(L, "mobjtype_t out of bounds error!"); + if (type >= NUMMOBJTYPES) + return luaL_error(L, "mobj type %d out of range (0 - %d)", type, NUMMOBJTYPES-1); LUA_PushUserdata(L, P_SpawnPointMissile(source, xa, ya, za, type, x, y, z), META_MOBJ); return 1; } @@ -374,8 +374,8 @@ static int lib_pSpawnAlteredDirectionMissile(lua_State *L) NOHUD if (!source) return LUA_ErrInvalid(L, "mobj_t"); - if (type > MT_LASTFREESLOT) - return luaL_error(L, "mobjtype_t out of bounds error!"); + if (type >= NUMMOBJTYPES) + return luaL_error(L, "mobj type %d out of range (0 - %d)", type, NUMMOBJTYPES-1); LUA_PushUserdata(L, P_SpawnAlteredDirectionMissile(source, type, x, y, z, shiftingAngle), META_MOBJ); return 1; } @@ -403,8 +403,8 @@ static int lib_pSPMAngle(lua_State *L) NOHUD if (!source) return LUA_ErrInvalid(L, "mobj_t"); - if (type > MT_LASTFREESLOT) - return luaL_error(L, "mobjtype_t out of bounds error!"); + if (type >= NUMMOBJTYPES) + return luaL_error(L, "mobj type %d out of range (0 - %d)", type, NUMMOBJTYPES-1); LUA_PushUserdata(L, P_SPMAngle(source, type, angle, allowaim, flags2), META_MOBJ); return 1; } @@ -417,8 +417,8 @@ static int lib_pSpawnPlayerMissile(lua_State *L) NOHUD if (!source) return LUA_ErrInvalid(L, "mobj_t"); - if (type > MT_LASTFREESLOT) - return luaL_error(L, "mobjtype_t out of bounds error!"); + if (type >= NUMMOBJTYPES) + return luaL_error(L, "mobj type %d out of range (0 - %d)", type, NUMMOBJTYPES-1); LUA_PushUserdata(L, P_SpawnPlayerMissile(source, type, flags2), META_MOBJ); return 1; } @@ -437,8 +437,8 @@ static int lib_pWeaponOrPanel(lua_State *L) { mobjtype_t type = luaL_checkinteger(L, 1); //HUDSAFE - if (type > MT_LASTFREESLOT) - return luaL_error(L, "mobjtype_t out of bounds error!"); + if (type >= NUMMOBJTYPES) + return luaL_error(L, "mobj type %d out of range (0 - %d)", type, NUMMOBJTYPES-1); lua_pushboolean(L, P_WeaponOrPanel(type)); return 1; } @@ -477,8 +477,10 @@ static int lib_pSpawnParaloop(lua_State *L) statenum_t nstate = luaL_optinteger(L, 8, S_NULL); boolean spawncenter = lua_optboolean(L, 9); NOHUD - if (type > MT_LASTFREESLOT) - return luaL_error(L, "mobjtype_t out of bounds error!"); + if (type >= NUMMOBJTYPES) + return luaL_error(L, "mobj type %d out of range (0 - %d)", type, NUMMOBJTYPES-1); + if (nstate >= NUMSTATES) + return luaL_error(L, "state %d out of range (0 - %d)", nstate, NUMSTATES-1); P_SpawnParaloop(x, y, z, radius, number, type, nstate, rotangle, spawncenter); return 0; } @@ -908,8 +910,8 @@ static int lib_pSpawnSpinMobj(lua_State *L) NOHUD if (!player) return LUA_ErrInvalid(L, "player_t"); - if (type > MT_LASTFREESLOT) - return luaL_error(L, "mobjtype_t out of bounds error!"); + if (type >= NUMMOBJTYPES) + return luaL_error(L, "mobj type %d out of range (0 - %d)", type, NUMMOBJTYPES-1); P_SpawnSpinMobj(player, type); return 0; } @@ -1274,6 +1276,8 @@ static int lib_pSetMobjStateNF(lua_State *L) NOHUD if (!mobj) return LUA_ErrInvalid(L, "mobj_t"); + if (state >= NUMSTATES) + return luaL_error(L, "state %d out of range (0 - %d)", state, NUMSTATES-1); if (mobj->player && state == S_NULL) return luaL_error(L, "Attempt to remove player mobj with S_NULL."); lua_pushboolean(L, P_SetMobjStateNF(mobj, state)); @@ -1385,8 +1389,8 @@ static int lib_pIsFlagAtBase(lua_State *L) { mobjtype_t flag = luaL_checkinteger(L, 1); NOHUD - if (flag > MT_LASTFREESLOT) - return luaL_error(L, "mobjtype_t out of bounds error!"); + if (flag >= NUMMOBJTYPES) + return luaL_error(L, "mobj type %d out of range (0 - %d)", flag, NUMMOBJTYPES-1); lua_pushboolean(L, P_IsFlagAtBase(flag)); return 1; } @@ -1619,7 +1623,7 @@ static int lib_rSetPlayerSkin(lua_State *L) { INT32 i = luaL_checkinteger(L, 2); if (i < 0 || i >= MAXSKINS) - return luaL_error(L, "argument #2 cannot exceed MAXSKINS"); + return luaL_error(L, "skin number (argument #2) %d out of range (0 - %d)", i, MAXSKINS-1); SetPlayerSkinByNum(player-players, i); } else // skin name @@ -1639,6 +1643,8 @@ static int lib_sStartSound(lua_State *L) sfxenum_t sound_id = luaL_checkinteger(L, 2); player_t *player = NULL; NOHUD + if (sound_id >= NUMSFX) + return luaL_error(L, "sfx %d out of range (0 - %d)", sound_id, NUMSFX-1); if (!lua_isnil(L, 1)) { origin = *((mobj_t **)luaL_checkudata(L, 1, META_MOBJ)); @@ -1663,12 +1669,15 @@ static int lib_sStartSoundAtVolume(lua_State *L) INT32 volume = (INT32)luaL_checkinteger(L, 3); player_t *player = NULL; NOHUD + if (!lua_isnil(L, 1)) { origin = *((mobj_t **)luaL_checkudata(L, 1, META_MOBJ)); if (!origin) return LUA_ErrInvalid(L, "mobj_t"); } + if (sound_id >= NUMSFX) + return luaL_error(L, "sfx %d out of range (0 - %d)", sound_id, NUMSFX-1); if (!lua_isnone(L, 4) && lua_isuserdata(L, 4)) { player = *((player_t **)luaL_checkudata(L, 4, META_PLAYER)); @@ -1800,6 +1809,8 @@ static int lib_sIdPlaying(lua_State *L) { sfxenum_t id = luaL_checkinteger(L, 1); NOHUD + if (id >= NUMSFX) + return luaL_error(L, "sfx %d out of range (0 - %d)", id, NUMSFX-1); lua_pushboolean(L, S_IdPlaying(id)); return 1; } @@ -1811,6 +1822,8 @@ static int lib_sSoundPlaying(lua_State *L) NOHUD if (!origin) return LUA_ErrInvalid(L, "mobj_t"); + if (id >= NUMSFX) + return luaL_error(L, "sfx %d out of range (0 - %d)", id, NUMSFX-1); lua_pushboolean(L, S_SoundPlaying(origin, id)); return 1; } @@ -1831,7 +1844,7 @@ static int lib_gDoReborn(lua_State *L) INT32 playernum = luaL_checkinteger(L, 1); NOHUD if (playernum >= MAXPLAYERS) - return luaL_error(L, "playernum out of bounds error!"); + return luaL_error(L, "playernum %d out of range (0 - %d)", playernum, MAXPLAYERS-1); G_DoReborn(playernum); return 0; } diff --git a/src/lua_hudlib.c b/src/lua_hudlib.c index 52770a88a..c4a72f074 100644 --- a/src/lua_hudlib.c +++ b/src/lua_hudlib.c @@ -166,6 +166,8 @@ static int lib_getHudInfo(lua_State *L) lua_remove(L, 1); i = luaL_checkinteger(L, 1); + if (i >= NUMHUDITEMS) + return luaL_error(L, "hudinfo[] index %d out of range (0 - %d)", i, NUMHUDITEMS-1); LUA_PushUserdata(L, &hudinfo[i], META_HUDINFO); return 1; } diff --git a/src/lua_infolib.c b/src/lua_infolib.c index 0fddebaba..6c99e4f6e 100644 --- a/src/lua_infolib.c +++ b/src/lua_infolib.c @@ -146,6 +146,8 @@ static int lib_getState(lua_State *L) lua_remove(L, 1); i = luaL_checkinteger(L, 1); + if (i >= NUMSTATES) + return luaL_error(L, "states[] index %d out of range (0 - %d)", i, NUMSTATES-1); LUA_PushUserdata(L, &states[i], META_STATE); return 1; } @@ -155,7 +157,12 @@ static int lib_setState(lua_State *L) { state_t *state; lua_remove(L, 1); // don't care about states[] userdata. - state = &states[luaL_checkinteger(L, 1)]; // get the state to assign to. + { + UINT32 i = luaL_checkinteger(L, 1); + if (i >= NUMSTATES) + return luaL_error(L, "states[] index %d out of range (0 - %d)", i, NUMSTATES-1); + state = &states[i]; // get the state to assign to. + } luaL_checktype(L, 2, LUA_TTABLE); // check that we've been passed a table. lua_remove(L, 1); // pop state num, don't need it any more. lua_settop(L, 1); // cut the stack here. the only thing left now is the table of data we're assigning to the state. @@ -436,6 +443,8 @@ static int lib_getMobjInfo(lua_State *L) lua_remove(L, 1); i = luaL_checkinteger(L, 1); + if (i >= NUMMOBJTYPES) + return luaL_error(L, "mobjinfo[] index %d out of range (0 - %d)", i, NUMMOBJTYPES-1); LUA_PushUserdata(L, &mobjinfo[i], META_MOBJINFO); return 1; } @@ -445,7 +454,12 @@ static int lib_setMobjInfo(lua_State *L) { mobjinfo_t *info; lua_remove(L, 1); // don't care about mobjinfo[] userdata. - info = &mobjinfo[luaL_checkinteger(L, 1)]; // get the mobjinfo to assign to. + { + UINT32 i = luaL_checkinteger(L, 1); + if (i >= NUMMOBJTYPES) + return luaL_error(L, "mobjinfo[] index %d out of range (0 - %d)", i, NUMMOBJTYPES-1); + info = &mobjinfo[i]; // get the mobjinfo to assign to. + } luaL_checktype(L, 2, LUA_TTABLE); // check that we've been passed a table. lua_remove(L, 1); // pop mobjtype num, don't need it any more. lua_settop(L, 1); // cut the stack here. the only thing left now is the table of data we're assigning to the mobjinfo. @@ -717,6 +731,8 @@ static int lib_getSfxInfo(lua_State *L) lua_remove(L, 1); i = luaL_checkinteger(L, 1); + if (i >= NUMSFX) + return luaL_error(L, "sfxinfo[] index %d out of range (0 - %d)", i, NUMSFX-1); LUA_PushUserdata(L, &S_sfx[i], META_SFXINFO); return 1; } @@ -727,7 +743,12 @@ static int lib_setSfxInfo(lua_State *L) sfxinfo_t *info; lua_remove(L, 1); - info = &S_sfx[luaL_checkinteger(L, 1)]; // get the mobjinfo to assign to. + { + UINT32 i = luaL_checkinteger(L, 1); + if (i >= NUMSFX) + return luaL_error(L, "sfxinfo[] index %d out of range (0 - %d)", i, NUMSFX-1); + info = &S_sfx[i]; // get the mobjinfo to assign to. + } luaL_checktype(L, 2, LUA_TTABLE); // check that we've been passed a table. lua_remove(L, 1); // pop mobjtype num, don't need it any more. lua_settop(L, 1); // cut the stack here. the only thing left now is the table of data we're assigning to the mobjinfo. diff --git a/src/lua_playerlib.c b/src/lua_playerlib.c index 53af2e3ac..388fe3175 100644 --- a/src/lua_playerlib.c +++ b/src/lua_playerlib.c @@ -55,7 +55,7 @@ static int lib_getPlayer(lua_State *L) { lua_Integer i = luaL_checkinteger(L, 2); if (i < 0 || i >= MAXPLAYERS) - return luaL_error(L, "players[] index cannot exceed MAXPLAYERS"); + return luaL_error(L, "players[] index %d out of range (0 - %d)", i, MAXPLAYERS-1); if (!playeringame[i]) return 0; if (!players[i].mo) diff --git a/src/lua_skinlib.c b/src/lua_skinlib.c index f07b4564c..545cf55b0 100644 --- a/src/lua_skinlib.c +++ b/src/lua_skinlib.c @@ -244,7 +244,7 @@ static int lib_getSkin(lua_State *L) { i = luaL_checkinteger(L, 2); if (i < 0 || i >= MAXSKINS) - return luaL_error(L, "skins[] index cannot exceed MAXSKINS"); + return luaL_error(L, "skins[] index %d out of range (0 - %d)", i, MAXSKINS-1); if (i >= numskins) return 0; LUA_PushUserdata(L, &skins[i], META_SKIN);