From 16883b08e7c4fdc373aced7061d0488da787f201 Mon Sep 17 00:00:00 2001 From: Thorbjørn Lindeijer Date: Fri, 20 Aug 2010 00:08:31 +0200 Subject: Perform more detailed argument checking in Lua functions Based on helper functions in the auxiliary library. Reviewed-by: Yohann Ferreira --- src/scripting/lua.cpp | 255 ++++++++++++++++++++------------------------------ 1 file changed, 101 insertions(+), 154 deletions(-) (limited to 'src') diff --git a/src/scripting/lua.cpp b/src/scripting/lua.cpp index 2d9562ed..c4c215d3 100644 --- a/src/scripting/lua.cpp +++ b/src/scripting/lua.cpp @@ -68,8 +68,8 @@ static int npc_message(lua_State *s) NPC *p = getNPC(s, 1); Character *q = getCharacter(s, 2); size_t l; - const char *m = lua_tolstring(s, 3, &l); - if (!p || !q || !m) + const char *m = luaL_checklstring(s, 3, &l); + if (!p || !q) { raiseScriptError(s, "npc_message called with incorrect parameters."); return 0; @@ -91,7 +91,7 @@ static int npc_choice(lua_State *s) Character *q = getCharacter(s, 2); if (!p || !q) { - raiseScriptError(s, "npc_Choice called with incorrect parameters."); + raiseScriptError(s, "npc_choice called with incorrect parameters."); return 0; } MessageOut msg(GPMSG_NPC_CHOICE); @@ -112,7 +112,7 @@ static int npc_choice(lua_State *s) } else { - raiseScriptError(s, "npc_Choice called with incorrect parameters."); + raiseScriptError(s, "npc_choice called with incorrect parameters."); return 0; } lua_pop(s, 1); @@ -120,7 +120,7 @@ static int npc_choice(lua_State *s) } else { - raiseScriptError(s, "npc_Choice called with incorrect parameters."); + raiseScriptError(s, "npc_choice called with incorrect parameters."); return 0; } } @@ -184,15 +184,15 @@ static int npc_ask_string(lua_State *s) */ static int npc_create(lua_State *s) { - if (!lua_isstring(s, 1) || !lua_isnumber(s, 2) || !lua_isnumber(s, 3) || !lua_isnumber(s, 4)) - { - raiseScriptError(s, "npc_create called with incorrect parameters."); - return 0; - } + const char *name = luaL_checkstring(s, 1); + const int id = luaL_checkint(s, 2); + const int x = luaL_checkint(s, 3); + const int y = luaL_checkint(s, 4); + lua_pushlightuserdata(s, (void *)®istryKey); lua_gettable(s, LUA_REGISTRYINDEX); Script *t = static_cast