[Fix] Sanitized lua bindings a little.

This commit is contained in:
Allanis 2013-07-26 18:39:21 +01:00
parent fb7c73aae4
commit 4f6c5fb93f

View File

@ -84,7 +84,7 @@ lua_State* llua_newState(void) {
int llua_load(lua_State* L, lua_CFunction f) { int llua_load(lua_State* L, lua_CFunction f) {
lua_pushcfunction(L, f); lua_pushcfunction(L, f);
if(lua_pcall(L, 0, 0, 0)) if(lua_pcall(L, 0, 0, 0))
WARN("llua include error: %s", lua_tostring(L, -1)); WARN("llua include error: %s", lua_tostring(L, 1));
return 0; return 0;
} }
@ -129,12 +129,12 @@ static int llua_packfileLoader(lua_State* L) {
LLUA_MIN_ARGS(1); LLUA_MIN_ARGS(1);
if(!lua_isstring(L, -1)) { if(!lua_isstring(L, 1)) {
LLUA_INVALID_PARAMETER(); LLUA_INVALID_PARAMETER();
return 0; return 0;
} }
filename = (char*) lua_tostring(L, -1); filename = (char*) lua_tostring(L, 1);
/* Try to locate the data. */ /* Try to locate the data. */
buf = pack_readfile(DATA, filename, &bufsize); buf = pack_readfile(DATA, filename, &bufsize);
@ -203,15 +203,15 @@ static int space_getPlanet(lua_State* L) {
lua_pushstring(L, space_getRndPlanet()); lua_pushstring(L, space_getRndPlanet());
return 1; return 1;
} }
else if(lua_isnumber(L, -1)) { else if(lua_isnumber(L, 1)) {
i = lua_tonumber(L, -1); i = lua_tonumber(L, 1);
planets = space_getFactionPlanet(&nplanets, &i, 1); planets = space_getFactionPlanet(&nplanets, &i, 1);
} }
else if(lua_isstring(L, -1)) { else if(lua_isstring(L, 1)) {
i = faction_get((char*) lua_tostring(L, -1)); i = faction_get((char*) lua_tostring(L, 1));
planets = space_getFactionPlanet(&nplanets, &i, 1); planets = space_getFactionPlanet(&nplanets, &i, 1);
} }
else if(lua_istable(L, -1)) { else if(lua_istable(L, 1)) {
/* Load up the table. */ /* Load up the table. */
lua_pushnil(L); lua_pushnil(L);
nfactions = (int) lua_gettop(L); nfactions = (int) lua_gettop(L);
@ -245,7 +245,7 @@ static int space_getSystem(lua_State* L) {
LLUA_MIN_ARGS(1); LLUA_MIN_ARGS(1);
char* planetname, *sysname; char* planetname, *sysname;
if(lua_isstring(L, -1)) planetname = (char*) lua_tostring(L, -1); if(lua_isstring(L, 1)) planetname = (char*) lua_tostring(L, 1);
else return 0; else return 0;
sysname = planet_getSystem(planetname); sysname = planet_getSystem(planetname);
@ -272,12 +272,12 @@ static int space_jumpDist(lua_State* L) {
int jumps; int jumps;
char* start, *goal; char* start, *goal;
if(lua_isstring(L, -1)) if(lua_isstring(L, 1))
start = (char*)lua_tostring(L, -1); start = (char*)lua_tostring(L, 1);
else LLUA_INVALID_PARAMETER(); else LLUA_INVALID_PARAMETER();
if((lua_gettop(L) > 1) && lua_isstring(L, -2)) if((lua_gettop(L) > 1) && lua_isstring(L, 2))
goal = (char*) lua_tostring(L, -2); goal = (char*) lua_tostring(L, 2);
else else
goal = cur_system->name; goal = cur_system->name;
@ -296,8 +296,8 @@ static int time_get(lua_State* L) {
static int time_str(lua_State* L) { static int time_str(lua_State* L) {
char* lt; char* lt;
if((lua_gettop(L) > 0) && (lua_isnumber(L, -1))) if((lua_gettop(L) > 0) && (lua_isnumber(L, 1)))
lt = ltime_pretty((unsigned int) lua_tonumber(L, -1)); lt = ltime_pretty((unsigned int) lua_tonumber(L, 1));
else else
lt = ltime_pretty(ltime_get()); lt = ltime_pretty(ltime_get());
lua_pushstring(L, lt); lua_pushstring(L, lt);
@ -306,8 +306,8 @@ static int time_str(lua_State* L) {
} }
static int time_units(lua_State* L) { static int time_units(lua_State* L) {
if((lua_gettop(L) > 0) && (lua_isnumber(L, -1))) if((lua_gettop(L) > 0) && (lua_isnumber(L, 1)))
lua_pushnumber(L, (unsigned int) lua_tonumber(L, -1) * LTIME_UNIT_LENGTH); lua_pushnumber(L, (unsigned int) lua_tonumber(L, 1) * LTIME_UNIT_LENGTH);
else else
lua_pushnumber(L, LTIME_UNIT_LENGTH); lua_pushnumber(L, LTIME_UNIT_LENGTH);
return 1; return 1;
@ -322,17 +322,17 @@ static int rnd_int(lua_State* L) {
if(o == 0) lua_pushnumber(L, RNGF()); /* Random double o <= x <= 1. */ if(o == 0) lua_pushnumber(L, RNGF()); /* Random double o <= x <= 1. */
else if(o == 1) { else if(o == 1) {
/* Random int 0 <= x <= param. */ /* Random int 0 <= x <= param. */
if(lua_isnumber(L, -1)) if(lua_isnumber(L, 1))
lua_pushnumber(L, RNG(0, (int)lua_tonumber(L, -1))); lua_pushnumber(L, RNG(0, (int)lua_tonumber(L, 1)));
else return 0; else return 0;
} }
else if(o >= 2) { else if(o >= 2) {
/* Random int param 1 <= x <= param 2. */ /* Random int param 1 <= x <= param 2. */
if(lua_isnumber(L, -1) && lua_isnumber(L, -2)) if(lua_isnumber(L, 1) && lua_isnumber(L, 2))
lua_pushnumber(L, RNG((int)lua_tonumber(L, -2), (int)lua_tonumber(L, -1))); lua_pushnumber(L, RNG((int)lua_tonumber(L, 1), (int)lua_tonumber(L, 2)));
else return 0; else return 0;
} }
else return 0; else LLUA_INVALID_PARAMETER();
/* Unless it's returned 0 already it'll always return param. */ /* Unless it's returned 0 already it'll always return param. */
return 1; return 1;
@ -344,10 +344,10 @@ static int tk_msg(lua_State* L) {
char* title, *str; char* title, *str;
LLUA_MIN_ARGS(2); LLUA_MIN_ARGS(2);
if(lua_isstring(L, -2)) title = (char*) lua_tostring(L, -2); if(lua_isstring(L, 1)) title = (char*) lua_tostring(L, 1);
else return 0; else LLUA_INVALID_PARAMETER();
if(lua_isstring(L, -1)) str = (char*) lua_tostring(L, -1); if(lua_isstring(L, 2)) str = (char*) lua_tostring(L, 2);
else return 0; else LLUA_INVALID_PARAMETER();
dialogue_msg(title, str); dialogue_msg(title, str);
return 0; return 0;
@ -358,10 +358,10 @@ static int tk_yesno(lua_State* L) {
char* title, *str; char* title, *str;
LLUA_MIN_ARGS(2); LLUA_MIN_ARGS(2);
if(lua_isstring(L, -2)) title = (char*) lua_tostring(L, -2); if(lua_isstring(L, 1)) title = (char*) lua_tostring(L, 1);
else return 0; else LLUA_INVALID_PARAMETER();
if(lua_isstring(L, -1)) str = (char*) lua_tostring(L, -1); if(lua_isstring(L, 2)) str = (char*) lua_tostring(L, 2);
else return 0; else LLUA_INVALID_PARAMETER();
ret = dialogue_YesNo(title, str); ret = dialogue_YesNo(title, str);
lua_pushboolean(L, ret); lua_pushboolean(L, ret);
@ -373,14 +373,14 @@ static int tk_input(lua_State* L) {
int min, max; int min, max;
LLUA_MIN_ARGS(4); LLUA_MIN_ARGS(4);
if(lua_isstring(L, -4)) title = (char*) lua_tostring(L, -4); if(lua_isstring(L, 1)) title = (char*) lua_tostring(L, 1);
else return 0; else LLUA_INVALID_PARAMETER();
if(lua_isnumber(L, -3)) min = (int) lua_tonumber(L, -3); if(lua_isnumber(L, 2)) min = (int) lua_tonumber(L, 2);
else return 0; else LLUA_INVALID_PARAMETER();
if(lua_isnumber(L, -2)) max = (int) lua_tonumber(L, -2); if(lua_isnumber(L, 3)) max = (int) lua_tonumber(L, 3);
else return 0; else LLUA_INVALID_PARAMETER();
if(lua_isstring(L, -1)) str = (char*) lua_tostring(L, -1); if(lua_isstring(L, 4)) str = (char*) lua_tostring(L, 4);
else return 0; else LLUA_INVALID_PARAMETER();
dialogue_input(title, min, max, str); dialogue_input(title, min, max, str);
return 0; return 0;