From 333cfaf0a580c78ad92c7215dc46517b4c349e62 Mon Sep 17 00:00:00 2001 From: Uli Schlachter Date: Wed, 17 Feb 2016 19:03:48 +0100 Subject: [PATCH] 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 --- luaa.c | 57 +++++++++++++++++++++++++++++---------------------------- 1 file changed, 29 insertions(+), 28 deletions(-) diff --git a/luaa.c b/luaa.c index 6cc7d6432..48f010432 100644 --- a/luaa.c +++ b/luaa.c @@ -498,40 +498,41 @@ luaA_startup_error(const char *err) static bool luaA_loadrc(const char *confpath, bool run) { - if(!luaL_loadfile(globalconf.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(globalconf.L, luaA_dofunction_on_error); - lua_insert(globalconf.L, -2); - if(lua_pcall(globalconf.L, 0, LUA_MULTRET, -2)) - { - const char *err = lua_tostring(globalconf.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(globalconf.L, 1); - return true; - } - } - else + if(luaL_loadfile(globalconf.L, confpath)) { 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 + * configuration file. */ + conffile = a_strdup(confpath); + /* Move error handling function before function */ + lua_pushcfunction(globalconf.L, luaA_dofunction_on_error); + lua_insert(globalconf.L, -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); + 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(globalconf.L, 2); + return false; }