Fix crash on invalid Lua function arguments
This commit is contained in:
parent
000d45b9eb
commit
08b416c94c
2 changed files with 16 additions and 6 deletions
|
@ -17,7 +17,9 @@ template<typename T>
|
|||
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<T>(lua_tonumber(LS, argindex));
|
||||
|
@ -29,7 +31,9 @@ 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<T>(lua_tonumber(LS, argindex));
|
||||
|
|
12
lua.cpp
12
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);
|
||||
|
|
Loading…
Add table
Reference in a new issue