fix some lua crashes (use more argument checking)

This commit is contained in:
jacob1 2015-01-10 18:59:20 -05:00
parent 4f6094136e
commit e92bbac700
2 changed files with 23 additions and 23 deletions

View File

@ -278,7 +278,7 @@ void GameModel::BuildMenus()
tempTool = new ElementTool(i, sim->elements[i].Name, sim->elements[i].Description, PIXR(sim->elements[i].Colour), PIXG(sim->elements[i].Colour), PIXB(sim->elements[i].Colour), sim->elements[i].Identifier, sim->elements[i].IconGenerator); tempTool = new ElementTool(i, sim->elements[i].Name, sim->elements[i].Description, PIXR(sim->elements[i].Colour), PIXG(sim->elements[i].Colour), PIXB(sim->elements[i].Colour), sim->elements[i].Identifier, sim->elements[i].IconGenerator);
} }
if(sim->elements[i].MenuSection < SC_TOTAL && sim->elements[i].MenuVisible) if (sim->elements[i].MenuSection >= 0 && sim->elements[i].MenuSection < SC_TOTAL && sim->elements[i].MenuVisible)
{ {
menuList[sim->elements[i].MenuSection]->AddTool(tempTool); menuList[sim->elements[i].MenuSection]->AddTool(tempTool);
} }

View File

@ -2330,29 +2330,29 @@ int LuaScriptInterface::elements_property(lua_State * l)
{ {
case StructProperty::ParticleType: case StructProperty::ParticleType:
case StructProperty::Integer: case StructProperty::Integer:
*((int*)(((unsigned char*)&luacon_sim->elements[id])+offset)) = lua_tointeger(l, 3); *((int*)(((unsigned char*)&luacon_sim->elements[id])+offset)) = luaL_checkinteger(l, 3);
break; break;
case StructProperty::UInteger: case StructProperty::UInteger:
*((unsigned int*)(((unsigned char*)&luacon_sim->elements[id])+offset)) = lua_tointeger(l, 3); *((unsigned int*)(((unsigned char*)&luacon_sim->elements[id])+offset)) = luaL_checkinteger(l, 3);
break; break;
case StructProperty::Float: case StructProperty::Float:
*((float*)(((unsigned char*)&luacon_sim->elements[id])+offset)) = lua_tonumber(l, 3); *((float*)(((unsigned char*)&luacon_sim->elements[id])+offset)) = luaL_checkinteger(l, 3);
break; break;
case StructProperty::Char: case StructProperty::Char:
*((char*)(((unsigned char*)&luacon_sim->elements[id])+offset)) = lua_tointeger(l, 3); *((char*)(((unsigned char*)&luacon_sim->elements[id])+offset)) = luaL_checkinteger(l, 3);
break; break;
case StructProperty::UChar: case StructProperty::UChar:
*((unsigned char*)(((unsigned char*)&luacon_sim->elements[id])+offset)) = lua_tointeger(l, 3); *((unsigned char*)(((unsigned char*)&luacon_sim->elements[id])+offset)) = luaL_checkinteger(l, 3);
break; break;
case StructProperty::String: case StructProperty::String:
*((char**)(((unsigned char*)&luacon_sim->elements[id])+offset)) = strdup(lua_tostring(l, 3)); *((char**)(((unsigned char*)&luacon_sim->elements[id])+offset)) = strdup(luaL_checkstring(l, 3));
break; break;
case StructProperty::Colour: case StructProperty::Colour:
#if PIXELSIZE == 4 #if PIXELSIZE == 4
*((unsigned int*)(((unsigned char*)&luacon_sim->elements[id])+offset)) = lua_tointeger(l, 3); *((unsigned int*)(((unsigned char*)&luacon_sim->elements[id])+offset)) = luaL_checkinteger(l, 3);
#else #else
*((unsigned short*)(((unsigned char*)&luacon_sim->elements[id])+offset)) = lua_tointeger(l, 3); *((unsigned short*)(((unsigned char*)&luacon_sim->elements[id])+offset)) = luaL_checkinteger(l, 3);
#endif #endif
break; break;
} }
} }
@ -2724,7 +2724,7 @@ void LuaScriptInterface::initFileSystemAPI()
int LuaScriptInterface::fileSystem_list(lua_State * l) int LuaScriptInterface::fileSystem_list(lua_State * l)
{ {
const char * directoryName = lua_tostring(l, 1); const char * directoryName = luaL_checkstring(l, 1);
int index = 1; int index = 1;
lua_newtable(l); lua_newtable(l);
@ -2755,7 +2755,7 @@ int LuaScriptInterface::fileSystem_list(lua_State * l)
int LuaScriptInterface::fileSystem_exists(lua_State * l) int LuaScriptInterface::fileSystem_exists(lua_State * l)
{ {
const char * filename = lua_tostring(l, 1); const char * filename = luaL_checkstring(l, 1);
bool exists = false; bool exists = false;
#ifdef WIN #ifdef WIN
@ -2779,7 +2779,7 @@ int LuaScriptInterface::fileSystem_exists(lua_State * l)
int LuaScriptInterface::fileSystem_isFile(lua_State * l) int LuaScriptInterface::fileSystem_isFile(lua_State * l)
{ {
const char * filename = lua_tostring(l, 1); const char * filename = luaL_checkstring(l, 1);
bool isFile = false; bool isFile = false;
#ifdef WIN #ifdef WIN
@ -2810,7 +2810,7 @@ int LuaScriptInterface::fileSystem_isFile(lua_State * l)
int LuaScriptInterface::fileSystem_isDirectory(lua_State * l) int LuaScriptInterface::fileSystem_isDirectory(lua_State * l)
{ {
const char * filename = lua_tostring(l, 1); const char * filename = luaL_checkstring(l, 1);
bool isDir = false; bool isDir = false;
#ifdef WIN #ifdef WIN
@ -2841,7 +2841,7 @@ int LuaScriptInterface::fileSystem_isDirectory(lua_State * l)
int LuaScriptInterface::fileSystem_makeDirectory(lua_State * l) int LuaScriptInterface::fileSystem_makeDirectory(lua_State * l)
{ {
const char * dirname = lua_tostring(l, 1); const char * dirname = luaL_checkstring(l, 1);
int ret = 0; int ret = 0;
ret = Client::Ref().MakeDirectory(dirname); ret = Client::Ref().MakeDirectory(dirname);
@ -2851,7 +2851,7 @@ int LuaScriptInterface::fileSystem_makeDirectory(lua_State * l)
int LuaScriptInterface::fileSystem_removeDirectory(lua_State * l) int LuaScriptInterface::fileSystem_removeDirectory(lua_State * l)
{ {
const char * filename = lua_tostring(l, 1); const char * filename = luaL_checkstring(l, 1);
int ret = 0; int ret = 0;
#ifdef WIN #ifdef WIN
@ -2865,7 +2865,7 @@ int LuaScriptInterface::fileSystem_removeDirectory(lua_State * l)
int LuaScriptInterface::fileSystem_removeFile(lua_State * l) int LuaScriptInterface::fileSystem_removeFile(lua_State * l)
{ {
const char * filename = lua_tostring(l, 1); const char * filename = luaL_checkstring(l, 1);
int ret = 0; int ret = 0;
#ifdef WIN #ifdef WIN
@ -2879,8 +2879,8 @@ int LuaScriptInterface::fileSystem_removeFile(lua_State * l)
int LuaScriptInterface::fileSystem_move(lua_State * l) int LuaScriptInterface::fileSystem_move(lua_State * l)
{ {
const char * filename = lua_tostring(l, 1); const char * filename = luaL_checkstring(l, 1);
const char * newFilename = lua_tostring(l, 2); const char * newFilename = luaL_checkstring(l, 2);
int ret = 0; int ret = 0;
ret = rename(filename, newFilename); ret = rename(filename, newFilename);
@ -2891,8 +2891,8 @@ int LuaScriptInterface::fileSystem_move(lua_State * l)
int LuaScriptInterface::fileSystem_copy(lua_State * l) int LuaScriptInterface::fileSystem_copy(lua_State * l)
{ {
const char * filename = lua_tostring(l, 1); const char * filename = luaL_checkstring(l, 1);
const char * newFilename = lua_tostring(l, 2); const char * newFilename = luaL_checkstring(l, 2);
int ret = 0; int ret = 0;
try try