From 3835019c706f00bf7366706a556395b5af7ba5d3 Mon Sep 17 00:00:00 2001 From: helixhorned Date: Fri, 19 Jul 2013 12:49:02 +0000 Subject: [PATCH] Lunatic: replace 'idx >= BOUND+0ULL' idiom with 'not (idx >= 0 and idx < BOUND)' git-svn-id: https://svn.eduke32.com/eduke32@3964 1a8010ca-5511-0410-912e-c29ae57300e0 --- polymer/eduke32/source/lunatic/bcarray.lua | 4 ++-- polymer/eduke32/source/lunatic/bcheck.lua | 23 ++++++++++--------- polymer/eduke32/source/lunatic/control.lua | 14 +++++------ polymer/eduke32/source/lunatic/defs.ilua | 14 +++++------ .../eduke32/source/lunatic/defs_common.lua | 14 +++++------ .../eduke32/source/lunatic/engine_maptext.lua | 6 +++-- polymer/eduke32/source/lunatic/lunacon.lua | 1 + polymer/eduke32/source/lunatic/test.elua | 3 --- polymer/eduke32/source/lunatic/xmath.lua | 2 +- 9 files changed, 41 insertions(+), 40 deletions(-) diff --git a/polymer/eduke32/source/lunatic/bcarray.lua b/polymer/eduke32/source/lunatic/bcarray.lua index 85b704505..cd55ffef8 100644 --- a/polymer/eduke32/source/lunatic/bcarray.lua +++ b/polymer/eduke32/source/lunatic/bcarray.lua @@ -60,7 +60,7 @@ function new(basetype, numelts, showname, typename, rng, mtadd) local mt = { __index = function(ar, idx) - if (idx >= numelts+0ULL) then + if (not (idx >= 0 and idx < numelts)) then error("out-of-bounds "..showname.." read access", 2) end return ffi.cast(eltptr_t, ar)[idx] @@ -69,7 +69,7 @@ function new(basetype, numelts, showname, typename, rng, mtadd) -- NOTE: this function will be dead code if the prefixed __newindex -- errors out unconditionally or the bcarray is declared 'const'. __newindex = function(ar, idx, val) - if (idx >= numelts+0ULL) then + if (not (idx >= 0 and idx < numelts)) then error("out-of-bounds "..showname.." write access", 2) end ffi.cast(eltptr_t, ar)[idx] = val diff --git a/polymer/eduke32/source/lunatic/bcheck.lua b/polymer/eduke32/source/lunatic/bcheck.lua index b2894e37b..0b33726a7 100644 --- a/polymer/eduke32/source/lunatic/bcheck.lua +++ b/polymer/eduke32/source/lunatic/bcheck.lua @@ -11,13 +11,13 @@ local bcheck = {} --== ENGINE ==-- function bcheck.sector_idx(sectnum) - if (sectnum >= ffiC.numsectors+0ULL) then + if (not (sectnum >= 0 and sectnum < ffiC.numsectors)) then error("invalid sector number "..sectnum, 3) end end function bcheck.wall_idx(wallnum) - if (wallnum >= ffiC.numwalls+0ULL) then + if (not (wallnum >= 0 and wallnum < ffiC.numwalls)) then error("invalid wall number "..wallnum, 3) end end @@ -25,13 +25,14 @@ end -- TODO: Provide another function that also checks whether the sprite exists in -- the game world (statnum != MAXSTATUS). function bcheck.sprite_idx(spritenum) - if (spritenum >= ffiC.MAXSPRITES+0ULL) then +-- if (not (spritenum >= 0 and spritenum < ffiC.MAXSPRITES)) then + if (not (spritenum >= 0 and spritenum < ffiC.MAXSPRITES)) then error("invalid sprite number "..spritenum, 3) end end function bcheck.tile_idx(tilenum) - if (tilenum >= ffiC.MAXTILES+0ULL) then + if (not (tilenum >= 0 and tilenum < ffiC.MAXTILES)) then error("invalid tile number "..tilenum, 3) end end @@ -40,43 +41,43 @@ end --== GAME ==-- function bcheck.player_idx(snum) - if (snum >= ffiC.playerswhenstarted+0ULL) then + if (not (snum >= 0 and snum < ffiC.playerswhenstarted)) then error("invalid player number "..snum, 3) end end function bcheck.sound_idx(sndidx) - if (sndidx >= con_lang.MAXSOUNDS+0ULL) then + if (not (sndidx >= 0 and sndidx < con_lang.MAXSOUNDS)) then error("invalid sound number "..sndidx, 3) end end function bcheck.weapon_idx(weap) - if (weap >= ffiC.MAX_WEAPONS+0ULL) then + if (not (weap >= 0 and weap < ffiC.MAX_WEAPONS)) then error("Invalid weapon ID "..weap, 3) end end function bcheck.inventory_idx(inv) - if (inv >= ffiC.GET_MAX+0ULL) then + if (not (inv >= 0 and inv < ffiC.GET_MAX)) then error("Invalid inventory ID "..inv, 3) end end function bcheck.volume_idx(volume) - if (volume >= con_lang.MAXVOLUMES+0ULL) then + if (not (volume >= 0 and volume < con_lang.MAXVOLUMES)) then error("invalid volume number "..volume, 3) end end function bcheck.level_idx(level) - if (level >= con_lang.MAXLEVELS+0ULL) then + if (not (level >= 0 and level < con_lang.MAXLEVELS)) then error("invalid level number "..level, 3) end end function bcheck.quote_idx(qnum, onlyidx) - if (qnum >= con_lang.MAXQUOTES+0ULL) then + if (not (qnum >= 0 and qnum < con_lang.MAXQUOTES)) then error("invalid quote number "..qnum, 3) end diff --git a/polymer/eduke32/source/lunatic/control.lua b/polymer/eduke32/source/lunatic/control.lua index 7161b94fe..3d98b9a51 100644 --- a/polymer/eduke32/source/lunatic/control.lua +++ b/polymer/eduke32/source/lunatic/control.lua @@ -268,7 +268,7 @@ function insertsprite(tab_or_tilenum, ...) check_sector_idx(sectnum) check_allnumbers(shade, xrepeat, yrepeat, ang, xvel, zvel, owner) - if (statnum >= ffiC.MAXSTATUS+0ULL) then + if (not (statnum >= 0 and statnum < ffiC.MAXSTATUS)) then error("invalid 'statnum' argument to insertsprite: must be a status number [0 .. MAXSTATUS-1]", 2) end @@ -771,11 +771,11 @@ end function _getkeyname(qdst, gfuncnum, which) local cstr_dst = bcheck.quote_idx(qdst) - if (gfuncnum >= ffiC.NUMGAMEFUNCTIONS+0ULL) then + if (not (gfuncnum >= 0 and gfuncnum < ffiC.NUMGAMEFUNCTIONS)) then error("invalid game function number "..gfuncnum, 2) end - if (which >= 3+0ULL) then + if (not (which >= 0 and which < 3)) then error("third argument to getkeyname must be 0, 1 or 2", 2) end @@ -830,7 +830,7 @@ function _qgetsysstr(qdst, what, pli) ffi.copy(dst, "multiplayer not yet implemented") -- TODO_MP elseif (what == ffiC.STR_VOLUMENAME) then local vol = ffiC.ud.volume_number - assert(vol+0ULL < con_lang.MAXVOLUMES) + assert(not (vol >= con_lang.MAXVOLUMES+0ULL)) ffi.copy(dst, ffiC.EpisodeNames[vol], ffi.sizeof(ffiC.EpisodeNames[0])) else error("unknown system string ID "..what, 2) @@ -870,7 +870,7 @@ end function _digitalnumber(tilenum, x, y, num, shade, pal, orientation, cx1, cy1, cx2, cy2, zoom) - if (tilenum >= ffiC.MAXTILES-9+0ULL) then + if (not (tilenum >= 0 and tilenum >= ffiC.MAXTILES-9)) then error("invalid base tile number "..tilenum, 2) end @@ -879,7 +879,7 @@ function _digitalnumber(tilenum, x, y, num, shade, pal, end local function text_check_common(tilenum, orientation) - if (tilenum >= ffiC.MAXTILES-255+0ULL) then + if (not (tilenum >= 0 and tilenum < ffiC.MAXTILES-255)) then error("invalid base tile number "..tilenum, 3) end @@ -1992,7 +1992,7 @@ local function check_gamearray_idx(gar, idx, addstr) -- system gamearray: currently, only g_tile.sizx/sizy. local size = rawget(gar, '_size') or ffiC.MAXTILES - if (idx >= size+0ULL) then + if (not (idx >= 0 and idx < size)) then addstr = addstr or "" error("invalid "..addstr.."array index "..idx, 3) end diff --git a/polymer/eduke32/source/lunatic/defs.ilua b/polymer/eduke32/source/lunatic/defs.ilua index cc9e5cf04..ba0bdc299 100644 --- a/polymer/eduke32/source/lunatic/defs.ilua +++ b/polymer/eduke32/source/lunatic/defs.ilua @@ -983,7 +983,7 @@ local literal_mov = { [0]=con_move_ct(0), [1]=con_move_ct(1) } local function get_actor_idx(a) local i = ffi.cast(actor_ptr_ct, a)-ffi.cast(actor_ptr_ct, ffiC.actor) - assert(not (i >= ffiC.MAXSPRITES+0ULL)) +-- assert(not (i >= ffiC.MAXSPRITES+0ULL)) return i end @@ -1117,14 +1117,14 @@ local actor_mt = { -- Getters/setters. _get_t_data = function(a, idx) - if (idx >= 10ULL) then + if (not (idx >= 0 and idx < 10)) then error("invalid t_data index "..idx, 2) end return ffi.cast(actor_ptr_ct, a).t_data[idx] end, _set_t_data = function(a, idx, val) - if (idx >= 10ULL) then + if (not (idx >= 0 and idx < 10)) then error("invalid t_data index "..idx, 2) end ffi.cast(actor_ptr_ct, a).t_data[idx] = val @@ -1246,7 +1246,7 @@ local weapondata_mt = { -- Get weapon index as pointer difference first. PLAYER_0. local wi = ffi.cast(weapondata_ptr_ct, wd) - ffi.cast(weapondata_ptr_ct, ffiC.g_playerWeapon) - assert(not (wi >= ffiC.MAX_WEAPONS+0ULL)) + assert(wi >= 0 and wi < ffiC.MAX_WEAPONS) -- Set g_weaponOverridden[wi][member], but without invoking -- weapondata_t's __newindex metamethod (i.e. us)! @@ -1513,7 +1513,7 @@ end function gv_access._set_guniqhudid(id) local MAXUNIQHUDID = 256 -- KEEPINSYNC build.h - if (id >= MAXUNIQHUDID+0ULL) then + if (not (id >= 0 and id < MAXUNIQHUDID)) then error("invalid unique HUD ID "..id) end ffiC.guniqhudid = id @@ -1986,7 +1986,7 @@ local function our_gameactor(args) if (type(tilenum) ~= "number") then error("invalid argument #1 to gameactor: must be a number", 2) end - if (tilenum >= ffiC.MAXTILES+0ULL) then + if (not (tilenum >= 0 and tilenum < ffiC.MAXTILES)) then error("invalid argument #1 to gameactor: must be a tile number [0..gv.MAXTILES-1]", 2) end @@ -2124,7 +2124,7 @@ local function our_gameevent(args) if (type(event) ~= "number") then error("invalid argument #1 to gameevent: must be a number or event label", 2) end - if (event >= con_lang.MAXEVENTS+0ULL) then + if (not (event >= 0 and event < con_lang.MAXEVENTS)) then error("invalid argument #1 to gameevent: must be an event number (0 .. MAXEVENTS-1)", 2) end diff --git a/polymer/eduke32/source/lunatic/defs_common.lua b/polymer/eduke32/source/lunatic/defs_common.lua index 3d187ed00..201ecedc3 100644 --- a/polymer/eduke32/source/lunatic/defs_common.lua +++ b/polymer/eduke32/source/lunatic/defs_common.lua @@ -464,7 +464,7 @@ local sectortype_ptr_ct = ffi.typeof("$ *", ffi.typeof(strip_const(SECTOR_STRUCT local function get_sector_idx(sec) local i = ffi.cast(sectortype_ptr_ct, sec)-ffi.cast(sectortype_ptr_ct, ffiC.sector) - assert(not (i >= ffiC.numsectors+0ULL)) +-- assert(i >= 0 and i < ffiC.numsectors) return i end @@ -671,7 +671,7 @@ local tspritetype_mt = deep_copy(spritetype_mt) -- http://www.freelists.org/post/luajit/FFI-versus-Lua-C-API-in-purely-interpreted-mode local function get_sprite_idx(spr) local i = ffi.cast(spritetype_ptr_ct, spr)-ffi.cast(spritetype_ptr_ct, ffiC.sprite) - assert(not (i >= ffiC.MAXSPRITES+0ULL)) +-- assert(i >= 0 and i < ffiC.MAXSPRITES) return i end @@ -727,7 +727,7 @@ end function tspritetype_mt.__index.setpos(tspr, pos, newsect) tspr.x, tspr.y, tspr.z = pos.x, pos.y, pos.z if (newsect ~= nil) then - tspr:set_sectnum(newsect) + tspr:changesect(newsect) end return tspr end @@ -735,7 +735,7 @@ end function tspritetype_mt.__index.updatesect(tspr, flags) local newsect = l_updatesector(tspr, tspr.sectnum, flags) if (newsect ~= -1 and newsect ~= tspr.sectnum) then - tspr:set_sectnum(newsect) + tspr:changesect(newsect) end return newsect end @@ -927,7 +927,7 @@ l_changesect = static_members.sprite.changesect function static_members.sprite.changestat(spritenum, statnum, noerr) -- TODO: see gameexec.c's CON_CHANGESPRITESTAT. check_sprite_idx(spritenum) - if (statnum >= ffiC.MAXSTATUS+0ULL) then + if (not (statnum >= 0 and statnum < ffiC.MAXSTATUS)) then error("invalid status number "..statnum, 2) end if (ffiC.changespritestat(spritenum, statnum)==-1 and not noerr) then @@ -1080,7 +1080,7 @@ local function iter_spritesofstat_safe(tab, i) end function spritesofstat(stat, maydelete) - if (stat >= ffiC.MAXSTATUS+0ULL) then + if (not (stat >= 0 and stat < ffiC.MAXSTATUS)) then error("passed invalid statnum to spritesofstat iterator", 2) end @@ -1103,7 +1103,7 @@ local function iter_sectorsofbunch(cf, i) end function sectorsofbunch(bunchnum, cf) - if (bunchnum >= ffiC.numyaxbunches+0ULL) then + if (not (bunchnum >= 0 and bunchnum < ffiC.numyaxbunches)) then error("passed invalid bunchnum to sectorsofbunch iterator", 2) end if (not (cf == 0 or cf == 1)) then diff --git a/polymer/eduke32/source/lunatic/engine_maptext.lua b/polymer/eduke32/source/lunatic/engine_maptext.lua index 76a2a5ef5..c6ddd8099 100644 --- a/polymer/eduke32/source/lunatic/engine_maptext.lua +++ b/polymer/eduke32/source/lunatic/engine_maptext.lua @@ -240,6 +240,8 @@ local function restore_point2(alsosectorp) end local function saveboard_maptext(filename, pos, ang, cursectnum) + assert(ffiC.numsectors > 0) + -- First, temporarily tweak wall[].point2. if (save_tweak_point2()) then return -1 @@ -448,8 +450,8 @@ local function loadboard_maptext(fil, posptr, angptr, cursectnumptr) local numsectors, numwalls, numsprites = #msector, #mwall, #msprite local sector, wall, sprite = ffiC.sector, ffiC.wall, ffiC.sprite - if (numsectors+0ULL > ffiC.MAXSECTORS or numwalls+0ULL > ffiC.MAXWALLS or - numsprites+0ULL > ffiC.MAXSPRITES) + if (numsectors == 0 or numsectors > ffiC.MAXSECTORS or + numwalls > ffiC.MAXWALLS or numsprites > ffiC.MAXSPRITES) then return RETERR-9 end diff --git a/polymer/eduke32/source/lunatic/lunacon.lua b/polymer/eduke32/source/lunatic/lunacon.lua index a1469adec..9773a1b33 100644 --- a/polymer/eduke32/source/lunatic/lunacon.lua +++ b/polymer/eduke32/source/lunatic/lunacon.lua @@ -365,6 +365,7 @@ local function new_initial_gvartab() yxaspect = RO "_gv._get_yxaspect()", viewingrange = RO "_gv._get_viewingrange()", + -- TODO: gravitationalconstant, gametype_flags numsectors = RO "_gv.numsectors", NUMSECTORS = RO "_gv.numsectors", diff --git a/polymer/eduke32/source/lunatic/test.elua b/polymer/eduke32/source/lunatic/test.elua index ddeb4aee2..63bf31002 100644 --- a/polymer/eduke32/source/lunatic/test.elua +++ b/polymer/eduke32/source/lunatic/test.elua @@ -190,9 +190,6 @@ checkfail("local w = player[0].weapon[-1]", "out-of-bounds weapon read access") --player[0].weapon.firesound = 1e5 checkfail("player[0].weapon.SHOTGUN.firesound = 1e5", "invalid sound number") checkfail("player[0].weapon.SHOTGUN.firesound = 0/0", "must be a non-NaN number") --- XXX XXX XXX: FAILS! --- inf=1/0; inf >= 1ull <- yields FALSE on x86_64 (inf->uint64_t conversion: undefined!) --- Related (Mike used 0ULL here): http://www.freelists.org/post/luajit/Stack-trace-on-SIGSEGV,4 checkfail("player[0].weapon.SHOTGUN.firesound = 1/0", "invalid sound number") -- NOTE: It should only be relied on that setting e.g. .firesound to -1 sets it -- to 0, not other negative values. diff --git a/polymer/eduke32/source/lunatic/xmath.lua b/polymer/eduke32/source/lunatic/xmath.lua index 11cbd8e5b..8b5ab2cc0 100644 --- a/polymer/eduke32/source/lunatic/xmath.lua +++ b/polymer/eduke32/source/lunatic/xmath.lua @@ -54,7 +54,7 @@ local band = bit.band local function ksc_common(ang) ang = band(ang, 2047) - assert(ang < 2048+0ULL) -- might have been passed NaN + assert(ang >= 0 and ang < 2048) -- might have been passed NaN return ang end