Balance the stack in luaA_loadrc()

In various conditions, luaA_loadrc() left luaA_dofunction_on_error and an error
message on the Lua stack. Also, it used LUA_MULTRET without looking at the
return values. Fix all of this and reorder the code a bit to make it easier to
follow.

Signed-off-by: Uli Schlachter <psychon@znc.in>
This commit is contained in:
Uli Schlachter 2016-02-17 19:03:48 +01:00
parent 9eab892124
commit 2209f58e5d
1 changed files with 29 additions and 28 deletions

57
luaa.c
View File

@ -610,40 +610,41 @@ static bool
luaA_loadrc(const char *confpath, bool run)
{
lua_State *L = globalconf_get_lua_State();
if(!luaL_loadfile(L, confpath))
{
if(run)
{
/* Set the conffile right now so it can be used inside the
* configuration file. */
conffile = a_strdup(confpath);
/* Move error handling function before function */
lua_pushcfunction(L, luaA_dofunction_on_error);
lua_insert(L, -2);
if(lua_pcall(L, 0, LUA_MULTRET, -2))
{
const char *err = lua_tostring(L, -1);
luaA_startup_error(err);
fprintf(stderr, "%s\n", err);
/* An error happened, so reset this. */
conffile = NULL;
}
else
return true;
}
else
{
lua_pop(L, 1);
return true;
}
}
else
if(luaL_loadfile(L, confpath))
{
const char *err = lua_tostring(L, -1);
luaA_startup_error(err);
fprintf(stderr, "%s\n", err);
return false;
}
if(!run)
{
lua_pop(L, 1);
return true;
}
/* Set the conffile right now so it can be used inside the
* configuration file. */
conffile = a_strdup(confpath);
/* Move error handling function before function */
lua_pushcfunction(L, luaA_dofunction_on_error);
lua_insert(L, -2);
if(!lua_pcall(L, 0, 0, -2))
{
/* Pop luaA_dofunction_on_error */
lua_pop(L, 1);
return true;
}
const char *err = lua_tostring(L, -1);
luaA_startup_error(err);
fprintf(stderr, "%s\n", err);
/* An error happened, so reset this. */
conffile = NULL;
/* Pop luaA_dofunction_on_error() and the error message */
lua_pop(L, 2);
return false;
}