From 871a57ecd7081073e1d2b8f4750e1560eb7d94fb Mon Sep 17 00:00:00 2001 From: Allanis Date: Mon, 26 Aug 2013 16:38:37 +0100 Subject: [PATCH] [Add] More faction validity checking. --- src/faction.c | 4 ++++ src/llua_space.c | 8 +++++--- 2 files changed, 9 insertions(+), 3 deletions(-) diff --git a/src/faction.c b/src/faction.c index d93219a..86d90ec 100644 --- a/src/faction.c +++ b/src/faction.c @@ -58,6 +58,10 @@ int faction_get(const char* name) { /* Return the faction's name. */ char* faction_name(int f) { + if((f < 0) || (f >= faction_nstack)) { + WARN("Faction id '%d' is invalid.", f); + return NULL; + } return faction_stack[f].name; } diff --git a/src/llua_space.c b/src/llua_space.c index 2188f73..803b863 100644 --- a/src/llua_space.c +++ b/src/llua_space.c @@ -347,9 +347,11 @@ static int systemL_faction(lua_State* L) { /* Return result in table. */ lua_newtable(L); for(i = 0; i < sys->s->nplanets; i++) { - lua_pushboolean(L, 1); /* Value. */ - lua_setfield(L, -2, faction_name(sys->s->planets[i].faction)); /* Key. */ - /* Allows syntax foo = space.faction("foo"); if foo["bar"] then ... end */ + if(sys->s->planets[i].faction > 0) { /* Faction must be valid. */ + lua_pushboolean(L, 1); /* Value. */ + lua_setfield(L, -2, faction_name(sys->s->planets[i].faction)); /* Key. */ + /* Allows syntax foo = space.faction("foo"); if foo["bar"] then ... end */ + } } return 1;