From 96011bc71816b5e9eb2cc6c86f009198dfb1146c Mon Sep 17 00:00:00 2001 From: Anthony Zhang Date: Sat, 22 Jun 2013 15:43:58 -0400 Subject: [PATCH] Audit code for possible nil value indexing with unregistered nodes. --- mesecons/wires.lua | 3 +-- mesecons_extrawires/depends.txt | 1 + mesecons_extrawires/vertical.lua | 30 ++++++++-------------- mesecons_movestones/init.lua | 8 ++++-- mesecons_mvps/init.lua | 43 ++++++++++++++++++++++++-------- mesecons_pistons/init.lua | 31 ++++++++++++----------- 6 files changed, 67 insertions(+), 49 deletions(-) diff --git a/mesecons/wires.lua b/mesecons/wires.lua index 50052b5..88199e7 100644 --- a/mesecons/wires.lua +++ b/mesecons/wires.lua @@ -241,8 +241,7 @@ function mesecon:update_autoconnect(pos, secondcall, replace_old) end end -if minetest.registered_nodes["default:stone_with_mese"] == nil then - +if not minetest.registered_nodes["default:stone_with_mese"] then --before MESE update, use old recipes minetest.register_craft({ output = "mesecons:wire_00000000_off 18", recipe = { diff --git a/mesecons_extrawires/depends.txt b/mesecons_extrawires/depends.txt index acaa924..aca967d 100644 --- a/mesecons_extrawires/depends.txt +++ b/mesecons_extrawires/depends.txt @@ -1 +1,2 @@ +default mesecons diff --git a/mesecons_extrawires/vertical.lua b/mesecons_extrawires/vertical.lua index 39b5417..3274c1a 100644 --- a/mesecons_extrawires/vertical.lua +++ b/mesecons_extrawires/vertical.lua @@ -34,31 +34,23 @@ local brules = local vertical_updatepos = function (pos) local node = minetest.env:get_node(pos) - if minetest.registered_nodes[node.name].is_vertical_conductor then + if minetest.registered_nodes[node.name] + and minetest.registered_nodes[node.name].is_vertical_conductor then local node_above = minetest.env:get_node(mesecon:addPosRule(pos, vrules[1])) local node_below = minetest.env:get_node(mesecon:addPosRule(pos, vrules[2])) local namestate = minetest.registered_nodes[node.name].vertical_conductor_state - -- above and below: vertical mesecon - if minetest.registered_nodes[node_above.name].is_vertical_conductor - and minetest.registered_nodes[node_below.name].is_vertical_conductor then - minetest.env:add_node (pos, - {name = "mesecons_extrawires:vertical_"..namestate}) + local above = minetest.registered_nodes[node_above.name] and minetest.registered_nodes[node_above.name].is_vertical_conductor + local below = minetest.registered_nodes[node_below.name] and minetest.registered_nodes[node_below.name].is_vertical_conductor - -- above only: bottom - elseif minetest.registered_nodes[node_above.name].is_vertical_conductor - and not minetest.registered_nodes[node_below.name].is_vertical_conductor then - minetest.env:add_node (pos, - {name = "mesecons_extrawires:vertical_bottom_"..namestate}) - - -- below only: top - elseif not minetest.registered_nodes[node_above.name].is_vertical_conductor - and minetest.registered_nodes[node_below.name].is_vertical_conductor then - minetest.env:add_node (pos, - {name = "mesecons_extrawires:vertical_top_"..namestate}) + if above and below then -- above and below: vertical mesecon + minetest.env:add_node(pos, {name = "mesecons_extrawires:vertical_"..namestate}) + elseif above and not below then -- above only: bottom + minetest.env:add_node(pos, {name = "mesecons_extrawires:vertical_bottom_"..namestate}) + elseif not above and below then -- below only: top + minetest.env:add_node(pos, {name = "mesecons_extrawires:vertical_top_"..namestate}) else -- no vertical wire above, no vertical wire below: use default wire - minetest.env:add_node (pos, - {name = "mesecons_extrawires:vertical_"..namestate}) + minetest.env:add_node (pos, {name = "mesecons_extrawires:vertical_"..namestate}) end end end diff --git a/mesecons_movestones/init.lua b/mesecons_movestones/init.lua index 14d828b..4bf6dc5 100644 --- a/mesecons_movestones/init.lua +++ b/mesecons_movestones/init.lua @@ -92,9 +92,11 @@ minetest.register_entity("mesecons_movestones:movestone_entity", { local direction = mesecon:get_movestone_direction(pos) if not direction then -- no mesecon power + --push only solid nodes local name = minetest.env:get_node(pos).name if name ~= "air" and name ~= "ignore" - and minetest.registered_nodes[name].liquidtype == "none" then + and ((not minetest.registered_nodes[name]) + or minetest.registered_nodes[name].liquidtype == "none") then mesecon:mvps_push(pos, self.lastdir, MOVESTONE_MAXIMUM_PUSH) end minetest.env:add_node(pos, {name="mesecons_movestones:movestone"}) @@ -176,9 +178,11 @@ minetest.register_entity("mesecons_movestones:sticky_movestone_entity", { local direction = mesecon:get_movestone_direction(pos) if not direction then -- no mesecon power + --push only solid nodes local name = minetest.env:get_node(pos).name if name ~= "air" and name ~= "ignore" - and minetest.registered_nodes[name].liquidtype == "none" then + and ((not minetest.registered_nodes[name]) + or minetest.registered_nodes[name].liquidtype == "none") then mesecon:mvps_push(pos, self.lastdir, MOVESTONE_MAXIMUM_PUSH) --STICKY mesecon:mvps_pull_all(pos, self.lastdir) diff --git a/mesecons_mvps/init.lua b/mesecons_mvps/init.lua index 91fb345..c572cb3 100644 --- a/mesecons_mvps/init.lua +++ b/mesecons_mvps/init.lua @@ -48,7 +48,8 @@ function mesecon:mvps_get_stack(pos, dir, maximum) end if nn.name == "air" - or minetest.registered_nodes[nn.name].liquidtype ~= "none" then --is liquid + or (minetest.registered_nodes[nn.name] + and minetest.registered_nodes[nn.name].liquidtype ~= "none") then --is liquid break end @@ -106,8 +107,9 @@ function mesecon:mvps_pull_single(pos, dir) -- pos: pos of mvps; direction: dire np = mesecon:addPosRule(pos, dir) nn = minetest.env:get_node(np) - if minetest.registered_nodes[nn.name].liquidtype == "none" - and not mesecon:is_mvps_stopper(nn, {x = -dir.x, y = -dir.y, z = -dir.z}, {{pos = np, node = nn}}, 1) then + if ((not minetest.registered_nodes[nn.name]) --unregistered node + or minetest.registered_nodes[nn.name].liquidtype == "none") --non-liquid node + and not mesecon:is_mvps_stopper(nn, {x = -dir.x, y = -dir.y, z = -dir.z}, {{pos = np, node = nn}}, 1) then --non-stopper node local meta = minetest.env:get_meta(np):to_table() minetest.env:remove_node(np) minetest.env:add_node(pos, nn) @@ -129,10 +131,23 @@ function mesecon:mvps_pull_all(pos, direction) -- pos: pos of mvps; direction: d local lpos2 = {x=pos.x-direction.x*2, y=pos.y-direction.y*2, z=pos.z-direction.z*2} -- 2 away local lnode2 = minetest.env:get_node(lpos2) - if lnode.name ~= "ignore" and lnode.name ~= "air" and minetest.registered_nodes[lnode.name].liquidtype == "none" then return end - if lnode2.name == "ignore" or lnode2.name == "air" or not(minetest.registered_nodes[lnode2.name].liquidtype == "none") then return end + --avoid pulling solid nodes + if lnode.name ~= "ignore" + and lnode.name ~= "air" + and ((not minetest.registered_nodes[lnode.name]) + or minetest.registered_nodes[lnode.name].liquidtype == "none") then + return + end - local oldpos = {x=lpos2.x+direction.x, y=lpos2.y+direction.y, z=lpos2.z+direction.z} + --avoid pulling empty or liquid nodes + if lnode2.name == "ignore" + or lnode2.name == "air" + or (minetest.registered_nodes[lnode2.name] + and minetest.registered_nodes[lnode2.name].liquidtype ~= "none") then + return + end + + local oldpos = {x=lpos2.x + direction.x, y=lpos2.y + direction.y, z=lpos2.z + direction.z} repeat lnode2 = minetest.env:get_node(lpos2) minetest.env:add_node(oldpos, {name=lnode2.name}) @@ -142,7 +157,10 @@ function mesecon:mvps_pull_all(pos, direction) -- pos: pos of mvps; direction: d lpos2.y = lpos2.y-direction.y lpos2.z = lpos2.z-direction.z lnode = minetest.env:get_node(lpos2) - until lnode.name=="air" or lnode.name=="ignore" or not(minetest.registered_nodes[lnode2.name].liquidtype == "none") + until lnode.name == "air" + or lnode.name == "ignore" + or (minetest.registered_nodes[lnode2.name] + and minetest.registered_nodes[lnode2.name].liquidtype ~= "none") minetest.env:remove_node(oldpos) end @@ -151,9 +169,9 @@ function mesecon:mvps_move_objects(pos, dir, nodestack) -- Move object at tip of stack local pushpos = mesecon:addPosRule(pos, -- get pos at tip of stack - {x = dir.x * (#nodestack), - y = dir.y * (#nodestack), - z = dir.z * (#nodestack)}) + {x = dir.x * #nodestack, + y = dir.y * #nodestack, + z = dir.z * #nodestack}) local objects = minetest.env:get_objects_inside_radius(pushpos, 1) @@ -177,8 +195,11 @@ function mesecon:mvps_move_objects(pos, dir, nodestack) local entity = obj:get_luaentity() if not entity or not mesecon:is_mvps_unmov(entity.name) then local np = mesecon:addPosRule(obj:getpos(), dir) + + --move only if destination is not solid local nn = minetest.env:get_node(np) - if not minetest.registered_nodes[nn.name].walkable then + if not ((not minetest.registered_nodes[nn.name]) + or minetest.registered_nodes[nn.name].walkable) then obj:setpos(np) end end diff --git a/mesecons_pistons/init.lua b/mesecons_pistons/init.lua index f044d5a..1284eb7 100644 --- a/mesecons_pistons/init.lua +++ b/mesecons_pistons/init.lua @@ -46,7 +46,7 @@ piston_facedir_direction = function (node) return rules[1] end -piston_get_direction = function (dir, node) +piston_get_direction = function(dir, node) if type(dir) == "function" then return dir(node) else @@ -54,25 +54,26 @@ piston_get_direction = function (dir, node) end end -local piston_remove_pusher = function (pos, node) +local piston_remove_pusher = function(pos, node) pistonspec = minetest.registered_nodes[node.name].mesecons_piston + if pushername == pistonspec.pusher then --make sure there actually is a pusher (for compatibility reasons mainly) + return + end dir = piston_get_direction(pistonspec.dir, node) local pusherpos = mesecon:addPosRule(pos, dir) local pushername = minetest.env:get_node(pusherpos).name - if pushername == pistonspec.pusher then --make sure there actually is a pusher (for compatibility reasons mainly) - minetest.env:remove_node(pusherpos) - minetest.sound_play("piston_retract", { - pos = pos, - max_hear_distance = 20, - gain = 0.3, - }) - nodeupdate(pusherpos) - end + minetest.env:remove_node(pusherpos) + minetest.sound_play("piston_retract", { + pos = pos, + max_hear_distance = 20, + gain = 0.3, + }) + nodeupdate(pusherpos) end -local piston_on = function (pos, node) +local piston_on = function(pos, node) local pistonspec = minetest.registered_nodes[node.name].mesecons_piston local dir = piston_get_direction(pistonspec.dir, node) @@ -91,10 +92,10 @@ local piston_on = function (pos, node) end end -local piston_off = function (pos, node) +local piston_off = function(pos, node) local pistonspec = minetest.registered_nodes[node.name].mesecons_piston minetest.env:add_node(pos, {param2 = node.param2, name = pistonspec.offname}) - piston_remove_pusher (pos, node) + piston_remove_pusher(pos, node) if pistonspec.sticky then dir = piston_get_direction(pistonspec.dir, node) @@ -104,7 +105,7 @@ local piston_off = function (pos, node) end end -local piston_orientate = function (pos, placer) +local piston_orientate = function(pos, placer) -- not placed by player if not placer then return end