]> git.lizzy.rs Git - minetest.git/commitdiff
HUD: Reject and warn on invalid stat types (#11548)
authorSmallJoker <SmallJoker@users.noreply.github.com>
Sat, 21 Aug 2021 18:04:04 +0000 (20:04 +0200)
committerGitHub <noreply@github.com>
Sat, 21 Aug 2021 18:04:04 +0000 (20:04 +0200)
This comes into play on older servers which do not know the "stat" type.
Warnings are only logged once to avoid spam within globalstep callbacks

src/network/clientpackethandler.cpp
src/script/common/c_content.cpp
src/script/common/c_content.h
src/script/common/c_internal.cpp
src/script/common/c_internal.h
src/script/lua_api/l_localplayer.cpp
src/script/lua_api/l_object.cpp

index 50f4979592e85b4fa575a66da6ef8ee290d70fd4..a631a3178dce62ef90ade3e8d3dc87d410221ae7 100644 (file)
@@ -1119,17 +1119,29 @@ void Client::handleCommand_HudChange(NetworkPacket* pkt)
 
        *pkt >> server_id >> stat;
 
-       if (stat == HUD_STAT_POS || stat == HUD_STAT_SCALE ||
-               stat == HUD_STAT_ALIGN || stat == HUD_STAT_OFFSET)
-               *pkt >> v2fdata;
-       else if (stat == HUD_STAT_NAME || stat == HUD_STAT_TEXT || stat == HUD_STAT_TEXT2)
-               *pkt >> sdata;
-       else if (stat == HUD_STAT_WORLD_POS)
-               *pkt >> v3fdata;
-       else if (stat == HUD_STAT_SIZE)
-               *pkt >> v2s32data;
-       else
-               *pkt >> intdata;
+       // Keep in sync with:server.cpp -> SendHUDChange
+       switch ((HudElementStat)stat) {
+               case HUD_STAT_POS:
+               case HUD_STAT_SCALE:
+               case HUD_STAT_ALIGN:
+               case HUD_STAT_OFFSET:
+                       *pkt >> v2fdata;
+                       break;
+               case HUD_STAT_NAME:
+               case HUD_STAT_TEXT:
+               case HUD_STAT_TEXT2:
+                       *pkt >> sdata;
+                       break;
+               case HUD_STAT_WORLD_POS:
+                       *pkt >> v3fdata;
+                       break;
+               case HUD_STAT_SIZE:
+                       *pkt >> v2s32data;
+                       break;
+               default:
+                       *pkt >> intdata;
+                       break;
+       }
 
        ClientEvent *event = new ClientEvent();
        event->type                 = CE_HUDCHANGE;
index 235016be09abfe87435fe39fcc17ac7da2d3046e..f13287375a44e4590714a8b5af1e195b54318599 100644 (file)
@@ -1989,15 +1989,17 @@ void push_hud_element(lua_State *L, HudElement *elem)
        lua_setfield(L, -2, "style");
 }
 
-HudElementStat read_hud_change(lua_State *L, HudElement *elem, void **value)
+bool read_hud_change(lua_State *L, HudElementStat &stat, HudElement *elem, void **value)
 {
-       HudElementStat stat = HUD_STAT_NUMBER;
-       std::string statstr;
-       if (lua_isstring(L, 3)) {
+       std::string statstr = lua_tostring(L, 3);
+       {
                int statint;
-               statstr = lua_tostring(L, 3);
-               stat = string_to_enum(es_HudElementStat, statint, statstr) ?
-                               (HudElementStat)statint : stat;
+               if (!string_to_enum(es_HudElementStat, statint, statstr)) {
+                       script_log_unique(L, "Unknown HUD stat type: " + statstr, warningstream);
+                       return false;
+               }
+
+               stat = (HudElementStat)statint;
        }
 
        switch (stat) {
@@ -2060,7 +2062,8 @@ HudElementStat read_hud_change(lua_State *L, HudElement *elem, void **value)
                        *value = &elem->style;
                        break;
        }
-       return stat;
+
+       return true;
 }
 
 /******************************************************************************/
index 4dc614706b02eb9624945f975b763b2621325704..e762604a4e0131de61f51653648b6706d44dabf1 100644 (file)
@@ -193,12 +193,12 @@ void               read_json_value           (lua_State *L, Json::Value &root,
 void push_pointed_thing(lua_State *L, const PointedThing &pointed, bool csm =
        false, bool hitpoint = false);
 
-void               push_objectRef            (lua_State *L, const u16 id);
+void push_objectRef            (lua_State *L, const u16 id);
 
-void               read_hud_element          (lua_State *L, HudElement *elem);
+void read_hud_element          (lua_State *L, HudElement *elem);
 
-void               push_hud_element          (lua_State *L, HudElement *elem);
+void push_hud_element          (lua_State *L, HudElement *elem);
 
-HudElementStat     read_hud_change           (lua_State *L, HudElement *elem, void **value);
+bool read_hud_change           (lua_State *L, HudElementStat &stat, HudElement *elem, void **value);
 
-void               push_collision_move_result(lua_State *L, const collisionMoveResult &res);
+void push_collision_move_result(lua_State *L, const collisionMoveResult &res);
index ad5f836c503fe77b4f84164f10fab181b88a5cbf..66f6a9b9865bccfed13035bdf3d6b0fcf17358f5 100644 (file)
@@ -18,10 +18,12 @@ with this program; if not, write to the Free Software Foundation, Inc.,
 */
 
 #include "common/c_internal.h"
+#include "util/numeric.h"
 #include "debug.h"
 #include "log.h"
 #include "porting.h"
 #include "settings.h"
+#include <algorithm> // std::find
 
 std::string script_get_backtrace(lua_State *L)
 {
@@ -135,24 +137,35 @@ void script_run_callbacks_f(lua_State *L, int nargs,
        lua_remove(L, error_handler);
 }
 
-static void script_log(lua_State *L, const std::string &message,
-       std::ostream &log_to, bool do_error, int stack_depth)
+static void script_log_add_source(lua_State *L, std::string &message, int stack_depth)
 {
        lua_Debug ar;
 
-       log_to << message << " ";
        if (lua_getstack(L, stack_depth, &ar)) {
                FATAL_ERROR_IF(!lua_getinfo(L, "Sl", &ar), "lua_getinfo() failed");
-               log_to << "(at " << ar.short_src << ":" << ar.currentline << ")";
+               message.append(" (at " + std::string(ar.short_src) + ":"
+                       + std::to_string(ar.currentline) + ")");
        } else {
-               log_to << "(at ?:?)";
+               message.append(" (at ?:?)");
        }
-       log_to << std::endl;
+}
 
-       if (do_error)
-               script_error(L, LUA_ERRRUN, NULL, NULL);
-       else
-               infostream << script_get_backtrace(L) << std::endl;
+bool script_log_unique(lua_State *L, std::string message, std::ostream &log_to,
+       int stack_depth)
+{
+       thread_local std::vector<u64> logged_messages;
+
+       script_log_add_source(L, message, stack_depth);
+       u64 hash = murmur_hash_64_ua(message.data(), message.length(), 0xBADBABE);
+
+       if (std::find(logged_messages.begin(), logged_messages.end(), hash)
+                       == logged_messages.end()) {
+
+               logged_messages.emplace_back(hash);
+               log_to << message << std::endl;
+               return true;
+       }
+       return false;
 }
 
 DeprecatedHandlingMode get_deprecated_handling_mode()
@@ -174,9 +187,18 @@ DeprecatedHandlingMode get_deprecated_handling_mode()
        return ret;
 }
 
-void log_deprecated(lua_State *L, const std::string &message, int stack_depth)
+void log_deprecated(lua_State *L, std::string message, int stack_depth)
 {
        DeprecatedHandlingMode mode = get_deprecated_handling_mode();
-       if (mode != DeprecatedHandlingMode::Ignore)
-               script_log(L, message, warningstream, mode == DeprecatedHandlingMode::Error, stack_depth);
+       if (mode == DeprecatedHandlingMode::Ignore)
+               return;
+
+       script_log_add_source(L, message, stack_depth);
+       warningstream << message << std::endl;
+
+       if (mode == DeprecatedHandlingMode::Error)
+               script_error(L, LUA_ERRRUN, NULL, NULL);
+       else
+               infostream << script_get_backtrace(L) << std::endl;
 }
+
index 452c2dd5e098e74198008619fe29c0c90bc99f86..4ddbed23289248020306734f9888499126ad25dc 100644 (file)
@@ -114,6 +114,9 @@ void script_error(lua_State *L, int pcall_result, const char *mod, const char *f
 void script_run_callbacks_f(lua_State *L, int nargs,
        RunCallbacksMode mode, const char *fxn);
 
+bool script_log_unique(lua_State *L, std::string message, std::ostream &log_to,
+       int stack_depth = 1);
+
 enum class DeprecatedHandlingMode {
        Ignore,
        Log,
@@ -134,5 +137,4 @@ DeprecatedHandlingMode get_deprecated_handling_mode();
  * @param message The deprecation method
  * @param stack_depth How far on the stack to the first user function (ie: not builtin or core)
  */
-void log_deprecated(lua_State *L, const std::string &message,
-       int stack_depth=1);
+void log_deprecated(lua_State *L, std::string message, int stack_depth = 1);
index 59d9ea5f8ee17f8d08fedad315fd4fb549eae60c..77a692f081f4755df60fdbc98c50c35121e5c81b 100644 (file)
@@ -369,10 +369,11 @@ int LuaLocalPlayer::l_hud_change(lua_State *L)
        if (!element)
                return 0;
 
+       HudElementStat stat;
        void *unused;
-       read_hud_change(L, element, &unused);
+       bool ok = read_hud_change(L, stat, element, &unused);
 
-       lua_pushboolean(L, true);
+       lua_pushboolean(L, ok);
        return 1;
 }
 
index c404cb63cf067d1ce39ea0d3b2c86254ca5ab10e..c915fa9e186cdff5440257b14f978adf6f550a28 100644 (file)
@@ -1555,12 +1555,14 @@ int ObjectRef::l_hud_change(lua_State *L)
        if (elem == nullptr)
                return 0;
 
+       HudElementStat stat;
        void *value = nullptr;
-       HudElementStat stat = read_hud_change(L, elem, &value);
+       bool ok = read_hud_change(L, stat, elem, &value);
 
-       getServer(L)->hudChange(player, id, stat, value);
+       if (ok)
+               getServer(L)->hudChange(player, id, stat, value);
 
-       lua_pushboolean(L, true);
+       lua_pushboolean(L, ok);
        return 1;
 }