TNT: Fix up nil derefs
authorAuke Kok <sofar@foo-projects.org>
Tue, 29 Mar 2016 01:20:14 +0000 (18:20 -0700)
committerparamat <mat.gregory@virginmedia.com>
Sat, 16 Apr 2016 18:27:16 +0000 (19:27 +0100)
I spotted two places where under stress (many explosions) luajit would
end up passing nil to these functions. I'm not entirely sure how,
but it seems good form to guard against it, which does make it
more robust. After this patch, I'm not able to crash the server. With
many explosions, it may still lag significantly, but always returns
in the end.

mods/tnt/init.lua

index a3c2bf99e0ac4e064851f85384f695af04a6db19..c447c715151ee745e03c50ff4b88406e46e8760f 100644 (file)
@@ -51,9 +51,9 @@ local function eject_drops(drops, pos, radius)
                local count = item:get_count()
                local take_est = math.log(count * count) + math.random(0,4) - 2
                while count > 0 do
-                       local take = math.min(take_est,
+                       local take = math.max(1,math.min(take_est,
                                        item:get_count(),
-                                       item:get_stack_max())
+                                       item:get_stack_max()))
                        rand_pos(pos, drop_pos, radius)
                        local obj = minetest.add_item(drop_pos, item:get_name() .. " " .. take)
                        if obj then
@@ -165,7 +165,10 @@ local function add_effects(pos, radius, drops)
                local count = stack:get_count()
                if count > most then
                        most = count
-                       texture = minetest.registered_nodes[name].tiles[1]
+                       local def = minetest.registered_nodes[name]
+                       if def and def.tiles and def.tiles[1] then
+                               texture = def.tiles[1]
+                       end
                end
        end