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 a181bee465
commit 333cfaf0a5
1 changed files with 29 additions and 28 deletions

39
luaa.c
View File

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