From 221ee0fcf1da898fffbfaeda1fddb1c22491a3da Mon Sep 17 00:00:00 2001 From: iliekprogrammar Date: Sun, 7 Aug 2022 00:13:03 +0800 Subject: [PATCH 1/7] [5.6] mark places with unsafe set_hp entity handling There might be other places i might be missing, but these are the obvious ones. leftover entity handling i did not mark: * everything that involves a apply()-like algorithm (e.g. callbacks) over the same list of objects. * in mcl_damage, it's not known whether mcl_damage.from_mt() would involve entity handling or just player handling. --- mods/CORE/mcl_explosions/init.lua | 3 ++- mods/ENTITIES/mcl_boats/init.lua | 1 + mods/ENTITIES/mcl_mobs/api.lua | 1 + mods/ENVIRONMENT/lightning/init.lua | 3 +++ mods/ITEMS/mcl_armor/damage.lua | 1 + mods/ITEMS/mcl_bows/arrow.lua | 1 + mods/ITEMS/mcl_bows/rocket.lua | 1 + 7 files changed, 10 insertions(+), 1 deletion(-) diff --git a/mods/CORE/mcl_explosions/init.lua b/mods/CORE/mcl_explosions/init.lua index 0132d1669..92453a9d5 100644 --- a/mods/CORE/mcl_explosions/init.lua +++ b/mods/CORE/mcl_explosions/init.lua @@ -337,12 +337,13 @@ local function trace_explode(pos, strength, raydirs, radius, info, direct, sourc if not obj:is_player() then return end - + -- ERROR: [#2523] THIS WILL CAUSE INVALID OBJECTREFS mcl_util.deal_damage(obj, damage, {type = "explosion", direct = direct, source = source}) obj:add_velocity(vector.multiply(punch_dir, impact * 20)) end) else + -- ERROR: [#2523] THIS WILL CAUSE INVALID OBJECTREFS mcl_util.deal_damage(obj, damage, {type = "explosion", direct = direct, source = source}) if obj:is_player() or ent.tnt_knockback then diff --git a/mods/ENTITIES/mcl_boats/init.lua b/mods/ENTITIES/mcl_boats/init.lua index 087cd7eae..cd8002b22 100644 --- a/mods/ENTITIES/mcl_boats/init.lua +++ b/mods/ENTITIES/mcl_boats/init.lua @@ -201,6 +201,7 @@ function boat.on_punch(self, puncher, time_from_last_punch, tool_capabilities, d end function boat.on_step(self, dtime, moveresult) + -- ERROR: [#2523] THIS WILL CAUSE INVALID OBJECTREFS mcl_burning.tick(self.object, dtime, self) self._v = get_v(self.object:get_velocity()) * get_sign(self._v) diff --git a/mods/ENTITIES/mcl_mobs/api.lua b/mods/ENTITIES/mcl_mobs/api.lua index 8945c6e20..d183d55c7 100644 --- a/mods/ENTITIES/mcl_mobs/api.lua +++ b/mods/ENTITIES/mcl_mobs/api.lua @@ -3617,6 +3617,7 @@ local mob_step = function(self, dtime) check_item_pickup(self) check_aggro(self,dtime) if not self.fire_resistant then + -- ERROR: [#2523] THIS WILL CAUSE INVALID OBJECTREFS mcl_burning.tick(self.object, dtime, self) end diff --git a/mods/ENVIRONMENT/lightning/init.lua b/mods/ENVIRONMENT/lightning/init.lua index 14d8f5176..48eefce4e 100644 --- a/mods/ENVIRONMENT/lightning/init.lua +++ b/mods/ENVIRONMENT/lightning/init.lua @@ -120,6 +120,8 @@ function lightning.strike(pos) if not pos then return false end + -- ERROR: [#2523] THIS WILL CAUSE INVALID OBJECTREFS + -- mcl_util.deal_damage inside one of on_strike_functions local objects = get_objects_inside_radius(pos2, 3.5) if lightning.on_strike_functions then for _, func in pairs(lightning.on_strike_functions) do @@ -174,6 +176,7 @@ lightning.register_on_strike(function(pos, pos2, objects) elseif lua and lua.name == "mobs_mc:creeper" then mcl_util.replace_mob(obj, "mobs_mc:creeper_charged") else + -- ERROR: [#2523] THIS WILL CAUSE INVALID OBJECTREFS mcl_util.deal_damage(obj, 5, { type = "lightning_bolt" }) end end diff --git a/mods/ITEMS/mcl_armor/damage.lua b/mods/ITEMS/mcl_armor/damage.lua index ed616397d..e76f8034b 100644 --- a/mods/ITEMS/mcl_armor/damage.lua +++ b/mods/ITEMS/mcl_armor/damage.lua @@ -89,6 +89,7 @@ mcl_damage.register_modifier(function(obj, damage, reason) local thorns_damage = thorns_damage_regular + thorns_damage_irregular if thorns_damage > 0 and reason.type ~= "thorns" and reason.source ~= obj then + -- ERROR: [#2523] THIS WILL CAUSE INVALID OBJECTREFS mcl_util.deal_damage(reason.source, thorns_damage, {type = "thorns", direct = obj}) local thorns_item = thorns_pieces[math.random(#thorns_pieces)] diff --git a/mods/ITEMS/mcl_bows/arrow.lua b/mods/ITEMS/mcl_bows/arrow.lua index a03b875cb..10b7f6421 100644 --- a/mods/ITEMS/mcl_bows/arrow.lua +++ b/mods/ITEMS/mcl_bows/arrow.lua @@ -114,6 +114,7 @@ local function damage_particles(pos, is_critical) end function ARROW_ENTITY.on_step(self, dtime) + -- ERROR: [#2523] THIS WILL CAUSE INVALID OBJECTREFS mcl_burning.tick(self.object, dtime, self) self._time_in_air = self._time_in_air + .001 diff --git a/mods/ITEMS/mcl_bows/rocket.lua b/mods/ITEMS/mcl_bows/rocket.lua index d25c52647..e08db5323 100644 --- a/mods/ITEMS/mcl_bows/rocket.lua +++ b/mods/ITEMS/mcl_bows/rocket.lua @@ -312,6 +312,7 @@ local function damage_particles(pos, is_critical) end function ARROW_ENTITY.on_step(self, dtime) + -- ERROR: [#2523] THIS WILL CAUSE INVALID OBJECTREFS mcl_burning.tick(self.object, dtime, self) self._time_in_air = self._time_in_air + .001 From bce4d29737c46d5ef37a217a0ddf271e0edd1e95 Mon Sep 17 00:00:00 2001 From: iliekprogrammar Date: Sun, 7 Aug 2022 10:44:13 +0800 Subject: [PATCH 2/7] [5.6][lightning] fix unsafe entitiy handling this does not excuse all on_strike callbacks to allow oversights. it must also track entity removal. --- mods/ENVIRONMENT/lightning/init.lua | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/mods/ENVIRONMENT/lightning/init.lua b/mods/ENVIRONMENT/lightning/init.lua index 48eefce4e..bd8bc265b 100644 --- a/mods/ENVIRONMENT/lightning/init.lua +++ b/mods/ENVIRONMENT/lightning/init.lua @@ -120,11 +120,10 @@ function lightning.strike(pos) if not pos then return false end - -- ERROR: [#2523] THIS WILL CAUSE INVALID OBJECTREFS - -- mcl_util.deal_damage inside one of on_strike_functions - local objects = get_objects_inside_radius(pos2, 3.5) if lightning.on_strike_functions then for _, func in pairs(lightning.on_strike_functions) do + -- allow on_strike callbacks to destroy entities by re-obtaining objects for each callback + local objects = get_objects_inside_radius(pos2, 3.5) func(pos, pos2, objects) end end @@ -176,7 +175,7 @@ lightning.register_on_strike(function(pos, pos2, objects) elseif lua and lua.name == "mobs_mc:creeper" then mcl_util.replace_mob(obj, "mobs_mc:creeper_charged") else - -- ERROR: [#2523] THIS WILL CAUSE INVALID OBJECTREFS + -- WARNING: unsafe entity handling. object may be removed immediately mcl_util.deal_damage(obj, 5, { type = "lightning_bolt" }) end end From 4b316923194506b4e897029900a259463f5cf0fa Mon Sep 17 00:00:00 2001 From: iliekprogrammar Date: Sun, 7 Aug 2022 12:09:12 +0800 Subject: [PATCH 3/7] [5.6][boats] fix unsafe entity handling this probably does not affect normal gameplay, unless you can set boats on fire somehow --- mods/ENTITIES/mcl_boats/init.lua | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/mods/ENTITIES/mcl_boats/init.lua b/mods/ENTITIES/mcl_boats/init.lua index cd8002b22..9775b8597 100644 --- a/mods/ENTITIES/mcl_boats/init.lua +++ b/mods/ENTITIES/mcl_boats/init.lua @@ -201,8 +201,9 @@ function boat.on_punch(self, puncher, time_from_last_punch, tool_capabilities, d end function boat.on_step(self, dtime, moveresult) - -- ERROR: [#2523] THIS WILL CAUSE INVALID OBJECTREFS mcl_burning.tick(self.object, dtime, self) + -- mcl_burning.tick may remove object immediately + if not self.object:get_pos() then return end self._v = get_v(self.object:get_velocity()) * get_sign(self._v) local v_factor = 1 From bf80074d98d5399663ffa1034c301f0adf36f583 Mon Sep 17 00:00:00 2001 From: iliekprogrammar Date: Sun, 7 Aug 2022 12:36:53 +0800 Subject: [PATCH 4/7] [5.6][mobs] fix unsafe entity handling (unsure) i'm not sure about this one, can't find a suitable test for it. --- mods/ENTITIES/mcl_mobs/api.lua | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/mods/ENTITIES/mcl_mobs/api.lua b/mods/ENTITIES/mcl_mobs/api.lua index d183d55c7..8cbe9b8ca 100644 --- a/mods/ENTITIES/mcl_mobs/api.lua +++ b/mods/ENTITIES/mcl_mobs/api.lua @@ -3617,8 +3617,9 @@ local mob_step = function(self, dtime) check_item_pickup(self) check_aggro(self,dtime) if not self.fire_resistant then - -- ERROR: [#2523] THIS WILL CAUSE INVALID OBJECTREFS mcl_burning.tick(self.object, dtime, self) + -- mcl_burning.tick may remove object immediately + if not self.object:get_pos() then return end end local pos = self.object:get_pos() From 4f2789c498e35067633a5f12e41dcaef303615f8 Mon Sep 17 00:00:00 2001 From: iliekprogrammar Date: Sun, 7 Aug 2022 12:43:24 +0800 Subject: [PATCH 5/7] [5.6][armor] fix unsafe entity handling (unsure) i'm not sure about this one, can't fnd a suitable test for it. --- mods/ITEMS/mcl_armor/damage.lua | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/mods/ITEMS/mcl_armor/damage.lua b/mods/ITEMS/mcl_armor/damage.lua index e76f8034b..012c32307 100644 --- a/mods/ITEMS/mcl_armor/damage.lua +++ b/mods/ITEMS/mcl_armor/damage.lua @@ -89,8 +89,9 @@ mcl_damage.register_modifier(function(obj, damage, reason) local thorns_damage = thorns_damage_regular + thorns_damage_irregular if thorns_damage > 0 and reason.type ~= "thorns" and reason.source ~= obj then - -- ERROR: [#2523] THIS WILL CAUSE INVALID OBJECTREFS mcl_util.deal_damage(reason.source, thorns_damage, {type = "thorns", direct = obj}) + -- mcl_util.deal_damage may remove object immediately + if not reason.source:get_pos() then return end local thorns_item = thorns_pieces[math.random(#thorns_pieces)] From 156aff21a1aa4ade6ec2164f3a2b1e88c7f86377 Mon Sep 17 00:00:00 2001 From: iliekprogrammar Date: Sun, 7 Aug 2022 12:48:09 +0800 Subject: [PATCH 6/7] [5.6][bows] fix unsafe entity handling for arrows --- mods/ITEMS/mcl_bows/arrow.lua | 3 ++- mods/ITEMS/mcl_bows/rocket.lua | 3 ++- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/mods/ITEMS/mcl_bows/arrow.lua b/mods/ITEMS/mcl_bows/arrow.lua index 10b7f6421..ad4a3f30e 100644 --- a/mods/ITEMS/mcl_bows/arrow.lua +++ b/mods/ITEMS/mcl_bows/arrow.lua @@ -114,8 +114,9 @@ local function damage_particles(pos, is_critical) end function ARROW_ENTITY.on_step(self, dtime) - -- ERROR: [#2523] THIS WILL CAUSE INVALID OBJECTREFS mcl_burning.tick(self.object, dtime, self) + -- mcl_burning.tick may remove object immediately + if not self.object:get_pos() then return end self._time_in_air = self._time_in_air + .001 diff --git a/mods/ITEMS/mcl_bows/rocket.lua b/mods/ITEMS/mcl_bows/rocket.lua index e08db5323..e846937d0 100644 --- a/mods/ITEMS/mcl_bows/rocket.lua +++ b/mods/ITEMS/mcl_bows/rocket.lua @@ -312,8 +312,9 @@ local function damage_particles(pos, is_critical) end function ARROW_ENTITY.on_step(self, dtime) - -- ERROR: [#2523] THIS WILL CAUSE INVALID OBJECTREFS mcl_burning.tick(self.object, dtime, self) + -- mcl_burning.tick may remove object immediately + if not self.object:get_pos() then return end self._time_in_air = self._time_in_air + .001 From 0e999d8bb9d0c8f12f4761739b67f9ddd0df7223 Mon Sep 17 00:00:00 2001 From: iliekprogrammar Date: Sun, 7 Aug 2022 13:00:43 +0800 Subject: [PATCH 7/7] [5.6] remove incorrect marks. wrong, bad. these are fine. --- mods/CORE/mcl_explosions/init.lua | 2 -- 1 file changed, 2 deletions(-) diff --git a/mods/CORE/mcl_explosions/init.lua b/mods/CORE/mcl_explosions/init.lua index 92453a9d5..cb9af5c79 100644 --- a/mods/CORE/mcl_explosions/init.lua +++ b/mods/CORE/mcl_explosions/init.lua @@ -337,13 +337,11 @@ local function trace_explode(pos, strength, raydirs, radius, info, direct, sourc if not obj:is_player() then return end - -- ERROR: [#2523] THIS WILL CAUSE INVALID OBJECTREFS mcl_util.deal_damage(obj, damage, {type = "explosion", direct = direct, source = source}) obj:add_velocity(vector.multiply(punch_dir, impact * 20)) end) else - -- ERROR: [#2523] THIS WILL CAUSE INVALID OBJECTREFS mcl_util.deal_damage(obj, damage, {type = "explosion", direct = direct, source = source}) if obj:is_player() or ent.tnt_knockback then