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;