From f9e3c4fd6dd0c7f922eae47bd1e1b2bfdfbb09c8 Mon Sep 17 00:00:00 2001 From: cora Date: Sun, 5 Dec 2021 03:06:33 +0100 Subject: [PATCH 1/2] Fix crash when skeleton kills player using bow Commit 5252952555d204e4b5c1a2a4367b36665c476451 used string.gsub() to strip newlines from tools in death messages. The second return value of string.gsub() (the number of substitutions) was erroneously returned too by get_tool_name(). This bug caused a crash whenever a skeleton killed a player using its bow. --- mods/HUD/mcl_death_messages/init.lua | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/mods/HUD/mcl_death_messages/init.lua b/mods/HUD/mcl_death_messages/init.lua index aed8c770..837238b5 100644 --- a/mods/HUD/mcl_death_messages/init.lua +++ b/mods/HUD/mcl_death_messages/init.lua @@ -7,7 +7,8 @@ local function get_tool_name(item) local def = item:get_definition() name=def._tt_original_description or def.description end - return name:gsub("[\r\n]"," ") + local sanitized_name, substitution_count = name:gsub("[\r\n]"," ") + return sanitized_name end mcl_death_messages = {} From 609105e091b0853b68e24770674993dc18acc111 Mon Sep 17 00:00:00 2001 From: Nils Dagsson Moskopp Date: Mon, 6 Dec 2021 03:34:44 +0100 Subject: [PATCH 2/2] Add test cases for get_tool_name() --- mods/HUD/mcl_death_messages/init.lua | 92 ++++++++++++++++++++++++++++ 1 file changed, 92 insertions(+) diff --git a/mods/HUD/mcl_death_messages/init.lua b/mods/HUD/mcl_death_messages/init.lua index 837238b5..2f115823 100644 --- a/mods/HUD/mcl_death_messages/init.lua +++ b/mods/HUD/mcl_death_messages/init.lua @@ -11,6 +11,98 @@ local function get_tool_name(item) return sanitized_name end +local test_tool_1a = { + get_meta = function() + return { + get_string = function() + return "foo 1a" + end + } + end +} + +assert( get_tool_name(test_tool_1a) == "foo 1a" ) + +local test_tool_1b = { + get_meta = function() + return { + get_string = function() + return "bar\rbaz\n1b" + end + } + end +} + +assert( get_tool_name(test_tool_1b) == "bar baz 1b" ) + +local test_tool_2a = { + get_definition = function() + return { + _tt_original_description = "foo 2a" + } + end, + get_meta = function() + return { + get_string = function() + return "" + end + } + end +} + +assert( get_tool_name(test_tool_2a) == "foo 2a" ) + +local test_tool_2b = { + get_definition = function() + return { + _tt_original_description = "bar\rbaz\n2b" + } + end, + get_meta = function() + return { + get_string = function() + return "" + end + } + end +} + +assert( get_tool_name(test_tool_2b) == "bar baz 2b" ) + +local test_tool_3a = { + get_definition = function() + return { + description = "foo 3a" + } + end, + get_meta = function() + return { + get_string = function() + return "" + end + } + end +} + +assert( get_tool_name(test_tool_3a) == "foo 3a" ) + +local test_tool_3b = { + get_definition = function() + return { + description = "bar\rbaz\n3b" + } + end, + get_meta = function() + return { + get_string = function() + return "" + end + } + end +} + +assert( get_tool_name(test_tool_3b) == "bar baz 3b" ) + mcl_death_messages = {} -- Death messages