From 08b416c94c8f598f3482313433b8d1f4591815f9 Mon Sep 17 00:00:00 2001 From: Ilari Liusvaara Date: Sat, 17 Sep 2011 12:17:14 +0300 Subject: [PATCH] Fix crash on invalid Lua function arguments --- lua-int.hpp | 10 +++++++--- lua.cpp | 12 +++++++++--- 2 files changed, 16 insertions(+), 6 deletions(-) diff --git a/lua-int.hpp b/lua-int.hpp index afb27ed5..ac200c42 100644 --- a/lua-int.hpp +++ b/lua-int.hpp @@ -17,7 +17,9 @@ template T get_numeric_argument(lua_State* LS, unsigned argindex, const char* fname) { if(lua_isnone(LS, argindex) || !lua_isnumber(LS, argindex)) { - lua_pushfstring(LS, "argument #%i to %s must be numeric", argindex, fname); + char buffer[1024]; + sprintf(buffer, "argument #%i to %s must be numeric", argindex, fname); + lua_pushstring(LS, buffer); lua_error(LS); } return static_cast(lua_tonumber(LS, argindex)); @@ -29,10 +31,12 @@ void get_numeric_argument(lua_State* LS, unsigned argindex, T& value, const char if(lua_isnoneornil(LS, argindex)) return; if(lua_isnone(LS, argindex) || !lua_isnumber(LS, argindex)) { - lua_pushfstring(LS, "argument #%i to %s must be numeric if present", argindex, fname); + char buffer[1024]; + sprintf(buffer, "argument #%i to %s must be numeric if present", argindex, fname); + lua_pushstring(LS, buffer); lua_error(LS); } value = static_cast(lua_tonumber(LS, argindex)); } -#endif \ No newline at end of file +#endif diff --git a/lua.cpp b/lua.cpp index 46851150..766d7d65 100644 --- a/lua.cpp +++ b/lua.cpp @@ -99,13 +99,17 @@ lua_function::~lua_function() throw() std::string get_string_argument(lua_State* LS, unsigned argindex, const char* fname) { if(lua_isnone(LS, argindex)) { - lua_pushfstring(LS, "argument #%i to %s must be string", argindex, fname); + char buffer[1024]; + sprintf(buffer, "argument #%i to %s must be string", argindex, fname); + lua_pushstring(LS, buffer); lua_error(LS); } size_t len; const char* f = lua_tolstring(LS, argindex, &len); if(!f) { - lua_pushfstring(LS, "argument #%i to %s must be string", argindex, fname); + char buffer[1024]; + sprintf(buffer, "argument #%i to %s must be string", argindex, fname); + lua_pushstring(LS, buffer); lua_error(LS); } return std::string(f, f + len); @@ -114,7 +118,9 @@ std::string get_string_argument(lua_State* LS, unsigned argindex, const char* fn bool get_boolean_argument(lua_State* LS, unsigned argindex, const char* fname) { if(lua_isnone(LS, argindex) || !lua_isboolean(LS, argindex)) { - lua_pushfstring(LS, "argument #%i to %s must be boolean", argindex, fname); + char buffer[1024]; + sprintf(buffer, "argument #%i to %s must be boolean", argindex, fname); + lua_pushstring(LS, buffer); lua_error(LS); } return (lua_toboolean(LS, argindex) != 0); -- 2.11.4.GIT