Adding LuaError on attempt to assign vectors with values out of range
authorFoghrye4 <foghrye4@gmail.com>
Sun, 23 Oct 2016 17:45:25 +0000 (20:45 +0300)
committerNer'zhul <nerzhul@users.noreply.github.com>
Tue, 25 Oct 2016 19:10:51 +0000 (21:10 +0200)
src/script/common/c_converter.cpp
src/script/lua_api/l_object.cpp

index 857300fa5fcf3ea77034dc5f52f22778fef23b79..f36298915154b152975faca8416aa688533afccd 100644 (file)
@@ -23,6 +23,7 @@ extern "C" {
 }
 
 #include "util/numeric.h"
+#include "util/serialize.h"
 #include "util/string.h"
 #include "common/c_converter.h"
 #include "constants.h"
@@ -37,6 +38,14 @@ extern "C" {
                } \
        } while(0)
 #define CHECK_POS_COORD(name) CHECK_TYPE(-1, "position coordinate '" name "'", LUA_TNUMBER)
+#define CHECK_FLOAT_RANGE(value, name) \
+if (value < F1000_MIN || value > F1000_MAX) { \
+       std::ostringstream error_text; \
+       error_text << "Invalid float vector dimension range '" name "' " << \
+       "(expected " << F1000_MIN << " < " name " < " << F1000_MAX << \
+       " got " << value << ")." << std::endl; \
+       throw LuaError(error_text.str()); \
+}
 #define CHECK_POS_TAB(index) CHECK_TYPE(index, "position", LUA_TTABLE)
 
 
@@ -170,14 +179,17 @@ v3f check_v3f(lua_State *L, int index)
        lua_getfield(L, index, "x");
        CHECK_POS_COORD("x");
        pos.X = lua_tonumber(L, -1);
+       CHECK_FLOAT_RANGE(pos.X, "x")
        lua_pop(L, 1);
        lua_getfield(L, index, "y");
        CHECK_POS_COORD("y");
        pos.Y = lua_tonumber(L, -1);
+       CHECK_FLOAT_RANGE(pos.Y, "y")
        lua_pop(L, 1);
        lua_getfield(L, index, "z");
        CHECK_POS_COORD("z");
        pos.Z = lua_tonumber(L, -1);
+       CHECK_FLOAT_RANGE(pos.Z, "z")
        lua_pop(L, 1);
        return pos;
 }
index bb352e429cd853530b2aa9a06169ef86042546ba..23994181c9fb84c2947ba65441d8054ab7f885a2 100644 (file)
@@ -606,10 +606,10 @@ int ObjectRef::l_set_bone_position(lua_State *L)
                bone = lua_tostring(L, 2);
        v3f position = v3f(0, 0, 0);
        if (!lua_isnil(L, 3))
-               position = read_v3f(L, 3);
+               position = check_v3f(L, 3);
        v3f rotation = v3f(0, 0, 0);
        if (!lua_isnil(L, 4))
-               rotation = read_v3f(L, 4);
+               rotation = check_v3f(L, 4);
        co->setBonePosition(bone, position, rotation);
        return 0;
 }