]> git.ipfire.org Git - thirdparty/asterisk.git/commitdiff
Check for errors from fseek() when loading config file, properly abort on errors...
authorMatthew Nicholson <mnicholson@digium.com>
Fri, 4 Mar 2011 18:59:20 +0000 (18:59 +0000)
committerMatthew Nicholson <mnicholson@digium.com>
Fri, 4 Mar 2011 18:59:20 +0000 (18:59 +0000)
Also, prepend a newline to traceback output so that the main error message is on it's own line.

git-svn-id: https://origsvn.digium.com/svn/asterisk/branches/1.6.2@309541 65c4cc65-6c06-0410-ace0-fbb531ad65f3

pbx/pbx_lua.c

index 4056298a27481dc2e14c11e6bd6822e839790379..2f2fd9f7db0711c4a4e4b1f43307fd9b89e0c128 100644 (file)
@@ -736,6 +736,9 @@ static int lua_error_function(lua_State *L)
         * backtrace to it */
        message_index = lua_gettop(L);
 
+       /* prepare to prepend a new line to the traceback */
+       lua_pushliteral(L, "\n");
+
        lua_getglobal(L, "debug");
        lua_getfield(L, -1, "traceback");
        lua_remove(L, -2); /* remove the 'debug' table */
@@ -747,6 +750,9 @@ static int lua_error_function(lua_State *L)
 
        lua_call(L, 2, 1);
 
+       /* prepend the new line we prepared above */
+       lua_concat(L, 2);
+
        return 1;
 }
 
@@ -920,6 +926,7 @@ static int lua_extension_cmp(lua_State *L)
 static char *lua_read_extensions_file(lua_State *L, long *size)
 {
        FILE *f;
+       int error_func;
        char *data;
        char *path = alloca(strlen(config) + strlen(ast_config_AST_CONFIG_DIR) + 2);
        sprintf(path, "%s/%s", ast_config_AST_CONFIG_DIR, config);
@@ -934,10 +941,20 @@ static char *lua_read_extensions_file(lua_State *L, long *size)
                return NULL;
        }
 
-       fseek(f, 0l, SEEK_END);
+       if (fseek(f, 0l, SEEK_END)) {
+               fclose(f);
+               lua_pushliteral(L, "error determining the size of the config file");
+               return NULL;
+       }
+
        *size = ftell(f);
 
-       fseek(f, 0l, SEEK_SET);
+       if (fseek(f, 0l, SEEK_SET)) {
+               *size = 0;
+               fclose(f);
+               lua_pushliteral(L, "error reading config file");
+               return NULL;
+       }
 
        if (!(data = ast_malloc(*size))) {
                *size = 0;
@@ -947,18 +964,26 @@ static char *lua_read_extensions_file(lua_State *L, long *size)
        }
 
        if (fread(data, sizeof(char), *size, f) != *size) {
-               ast_log(LOG_WARNING, "fread() failed: %s\n", strerror(errno));
+               *size = 0;
+               fclose(f);
+               lua_pushliteral(L, "problem reading configuration file");
+               return NULL;
        }
        fclose(f);
 
+       lua_pushcfunction(L, &lua_error_function);
+       error_func = lua_gettop(L);
+
        if (luaL_loadbuffer(L, data, *size, "extensions.lua")
-                       || lua_pcall(L, 0, LUA_MULTRET, 0)
+                       || lua_pcall(L, 0, LUA_MULTRET, error_func)
                        || lua_sort_extensions(L)
                        || lua_register_switches(L)) {
                ast_free(data);
                data = NULL;
                *size = 0;
        }
+
+       lua_remove(L, error_func);
        return data;
 }