Skip to content

Commit 6eb6e75

Browse files
Foghrye4nerzhul
authored andcommittedOct 25, 2016
Adding LuaError on attempt to assign vectors with values out of range
1 parent 3db2f08 commit 6eb6e75

File tree

2 files changed

+14
-2
lines changed

2 files changed

+14
-2
lines changed
 

‎src/script/common/c_converter.cpp

+12
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ extern "C" {
2323
}
2424

2525
#include "util/numeric.h"
26+
#include "util/serialize.h"
2627
#include "util/string.h"
2728
#include "common/c_converter.h"
2829
#include "constants.h"
@@ -37,6 +38,14 @@ extern "C" {
3738
} \
3839
} while(0)
3940
#define CHECK_POS_COORD(name) CHECK_TYPE(-1, "position coordinate '" name "'", LUA_TNUMBER)
41+
#define CHECK_FLOAT_RANGE(value, name) \
42+
if (value < F1000_MIN || value > F1000_MAX) { \
43+
std::ostringstream error_text; \
44+
error_text << "Invalid float vector dimension range '" name "' " << \
45+
"(expected " << F1000_MIN << " < " name " < " << F1000_MAX << \
46+
" got " << value << ")." << std::endl; \
47+
throw LuaError(error_text.str()); \
48+
}
4049
#define CHECK_POS_TAB(index) CHECK_TYPE(index, "position", LUA_TTABLE)
4150

4251

@@ -170,14 +179,17 @@ v3f check_v3f(lua_State *L, int index)
170179
lua_getfield(L, index, "x");
171180
CHECK_POS_COORD("x");
172181
pos.X = lua_tonumber(L, -1);
182+
CHECK_FLOAT_RANGE(pos.X, "x")
173183
lua_pop(L, 1);
174184
lua_getfield(L, index, "y");
175185
CHECK_POS_COORD("y");
176186
pos.Y = lua_tonumber(L, -1);
187+
CHECK_FLOAT_RANGE(pos.Y, "y")
177188
lua_pop(L, 1);
178189
lua_getfield(L, index, "z");
179190
CHECK_POS_COORD("z");
180191
pos.Z = lua_tonumber(L, -1);
192+
CHECK_FLOAT_RANGE(pos.Z, "z")
181193
lua_pop(L, 1);
182194
return pos;
183195
}

‎src/script/lua_api/l_object.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -606,10 +606,10 @@ int ObjectRef::l_set_bone_position(lua_State *L)
606606
bone = lua_tostring(L, 2);
607607
v3f position = v3f(0, 0, 0);
608608
if (!lua_isnil(L, 3))
609-
position = read_v3f(L, 3);
609+
position = check_v3f(L, 3);
610610
v3f rotation = v3f(0, 0, 0);
611611
if (!lua_isnil(L, 4))
612-
rotation = read_v3f(L, 4);
612+
rotation = check_v3f(L, 4);
613613
co->setBonePosition(bone, position, rotation);
614614
return 0;
615615
}

0 commit comments

Comments
 (0)
Please sign in to comment.