From a3af52514681cbe17819f0638911bd60c1aeb6e4 Mon Sep 17 00:00:00 2001 From: Romain Forlot Date: Wed, 29 Nov 2017 19:24:37 +0100 Subject: Protect exec when no files found. ScanForConfig return null when no files found which isn't handled until now. Change-Id: I70d6b4a45be9d434aca049e3cd3260dfc38a7ff7 Signed-off-by: Romain Forlot --- ctl-lib/ctl-lua.c | 59 +++++++++++++++++++++++++++++-------------------------- 1 file changed, 31 insertions(+), 28 deletions(-) (limited to 'ctl-lib/ctl-lua.c') diff --git a/ctl-lib/ctl-lua.c b/ctl-lib/ctl-lua.c index add4c0d..f80668e 100644 --- a/ctl-lib/ctl-lua.c +++ b/ctl-lib/ctl-lua.c @@ -1293,40 +1293,43 @@ PUBLIC int LuaConfigExec (AFB_ApiT apiHandle, const char* prefix) { json_object *luaScriptPathJ = ScanForConfig(dirList , CTL_SCAN_RECURSIVE, fullprefix, "lua"); // load+exec any file found in LUA search path - for (index=0; index < json_object_array_length(luaScriptPathJ); index++) { - json_object *entryJ=json_object_array_get_idx(luaScriptPathJ, index); + if(luaScriptPathJ) { + for (index=0; index < json_object_array_length(luaScriptPathJ); index++) { + json_object *entryJ=json_object_array_get_idx(luaScriptPathJ, index); - char *filename; char*fullpath; - err= wrap_json_unpack (entryJ, "{s:s, s:s !}", "fullpath", &fullpath,"filename", &filename); - if (err) { - AFB_ApiError(apiHandle, "LUA-INIT HOOPs invalid config file path = %s", json_object_get_string(entryJ)); - goto OnErrorExit; - } + char *filename; char*fullpath; + err= wrap_json_unpack (entryJ, "{s:s, s:s !}", "fullpath", &fullpath,"filename", &filename); + if (err) { + AFB_ApiError(apiHandle, "LUA-INIT HOOPs invalid config file path = %s", json_object_get_string(entryJ)); + goto OnErrorExit; + } - char filepath[CONTROL_MAXPATH_LEN]; - strncpy(filepath, fullpath, strlen(fullpath)+1); - strncat(filepath, "/", strlen("/")); - strncat(filepath, filename, strlen(filename)); - err= luaL_loadfile(luaState, filepath); - if (err) { - AFB_ApiError(apiHandle, "LUA-LOAD HOOPs Error in LUA loading scripts=%s err=%s", filepath, lua_tostring(luaState,-1)); - goto OnErrorExit; - } + char filepath[CONTROL_MAXPATH_LEN]; + strncpy(filepath, fullpath, strlen(fullpath)+1); + strncat(filepath, "/", strlen("/")); + strncat(filepath, filename, strlen(filename)); + err= luaL_loadfile(luaState, filepath); + if (err) { + AFB_ApiError(apiHandle, "LUA-LOAD HOOPs Error in LUA loading scripts=%s err=%s", filepath, lua_tostring(luaState,-1)); + goto OnErrorExit; + } - // exec/compil script - err = lua_pcall(luaState, 0, 0, 0); - if (err) { - AFB_ApiError(apiHandle, "LUA-LOAD HOOPs Error in LUA exec scripts=%s err=%s", filepath, lua_tostring(luaState,-1)); - goto OnErrorExit; - } else { - AFB_ApiNotice(apiHandle, "LUA-LOAD '%s'", filepath); + // exec/compil script + err = lua_pcall(luaState, 0, 0, 0); + if (err) { + AFB_ApiError(apiHandle, "LUA-LOAD HOOPs Error in LUA exec scripts=%s err=%s", filepath, lua_tostring(luaState,-1)); + goto OnErrorExit; + } else { + AFB_ApiNotice(apiHandle, "LUA-LOAD '%s'", filepath); + } } - } - // no policy config found remove control API from binder - if (index == 0) { - AFB_ApiWarning (apiHandle, "POLICY-INIT:WARNING (setenv CONTROL_LUA_PATH) No LUA '%s*.lua' in '%s'", fullprefix, dirList); + // no policy config found remove control API from binder + if (index == 0) { + AFB_ApiWarning (apiHandle, "POLICY-INIT:WARNING (setenv CONTROL_LUA_PATH) No LUA '%s*.lua' in '%s'", fullprefix, dirList); + } } + else AFB_ApiWarning (apiHandle, "POLICY-INIT:WARNING (setenv CONTROL_LUA_PATH) No LUA '%s*.lua' in '%s'", fullprefix, dirList); AFB_ApiDebug (apiHandle, "Audio control-LUA Init Done"); return 0; -- cgit 1.2.3-korg