From af19faae21bf209515dcb220a102b4adf7f5b7d5 Mon Sep 17 00:00:00 2001
From: Romain Forlot <romain.forlot@iot.bzh>
Date: Fri, 3 Nov 2017 15:42:26 +0100
Subject: Fix: strncpy does't add null char at the end

Fix: handle null response from CtlScanConfig()

Change-Id: Icbe2a649886998078adda35b0d0dfd1c46e8fe31
Signed-off-by: Romain Forlot <romain.forlot@iot.bzh>
---
 ctl-lib/ctl-config.c | 36 +++++++++++++++++++-----------------
 ctl-lib/ctl-lua.c    | 10 +++++-----
 ctl-lib/ctl-plugin.c |  4 ++--
 3 files changed, 26 insertions(+), 24 deletions(-)

(limited to 'ctl-lib')

diff --git a/ctl-lib/ctl-config.c b/ctl-lib/ctl-config.c
index c315f9c..314aead 100644
--- a/ctl-lib/ctl-config.c
+++ b/ctl-lib/ctl-config.c
@@ -45,7 +45,7 @@ PUBLIC int CtlConfigMagicNew() {
 
 PUBLIC  json_object* CtlConfigScan(const char *dirList, const char *prefix) {
     char controlFile [CONTROL_MAXPATH_LEN];
-    strncpy(controlFile, prefix, strlen(prefix));
+    strncpy(controlFile, prefix, strlen(prefix)+1);
     strncat(controlFile, GetBinderName(), strlen(GetBinderName()));
 
     // search for default dispatch config file
@@ -60,24 +60,26 @@ PUBLIC char* CtlConfigSearch(AFB_ApiT apiHandle, const char *dirList, const char
     // search for default dispatch config file
     json_object* responseJ = CtlConfigScan (dirList, prefix);
 
-    // We load 1st file others are just warnings
-    for (index = 0; index < json_object_array_length(responseJ); index++) {
-        json_object *entryJ = json_object_array_get_idx(responseJ, index);
+    if(responseJ) {
+        // We load 1st file others are just warnings
+        for (index = 0; index < json_object_array_length(responseJ); index++) {
+            json_object *entryJ = json_object_array_get_idx(responseJ, index);
 
-        char *filename;
-        char*fullpath;
-        int err = wrap_json_unpack(entryJ, "{s:s, s:s !}", "fullpath", &fullpath, "filename", &filename);
-        if (err) {
-            AFB_ApiError(apiHandle, "CTL-INIT HOOPs invalid JSON entry= %s", json_object_get_string(entryJ));
-            return NULL;
-        }
+            char *filename;
+            char*fullpath;
+            int err = wrap_json_unpack(entryJ, "{s:s, s:s !}", "fullpath", &fullpath, "filename", &filename);
+            if (err) {
+                AFB_ApiError(apiHandle, "CTL-INIT HOOPs invalid JSON entry= %s", json_object_get_string(entryJ));
+                return NULL;
+            }
 
-        if (index == 0) {
-            char filepath[CONTROL_MAXPATH_LEN];
-            strncpy(filepath, fullpath, strlen(fullpath));
-            strncat(filepath, "/", strlen("/"));
-            strncat(filepath, filename, strlen(filename));
-            return (strdup(filepath));
+            if (index == 0) {
+                char filepath[CONTROL_MAXPATH_LEN];
+                strncpy(filepath, fullpath, strlen(fullpath)+1);
+                strncat(filepath, "/", strlen("/"));
+                strncat(filepath, filename, strlen(filename));
+                return (strdup(filepath));
+            }
         }
     }
 
diff --git a/ctl-lib/ctl-lua.c b/ctl-lib/ctl-lua.c
index 25a6af9..07b26bf 100644
--- a/ctl-lib/ctl-lua.c
+++ b/ctl-lib/ctl-lua.c
@@ -804,7 +804,7 @@ STATIC void LuaDoAction (LuaDoActionT action, AFB_ReqT request) {
 
             // search for filename=script in CONTROL_LUA_PATH
             if (!luaScriptPathJ)  {
-                strncpy(luaScriptPath,CONTROL_DOSCRIPT_PRE, strlen(CONTROL_DOSCRIPT_PRE));
+                strncpy(luaScriptPath,CONTROL_DOSCRIPT_PRE, strlen(CONTROL_DOSCRIPT_PRE)+1);
                 strncat(luaScriptPath,"-", strlen("-"));
                 strncat(luaScriptPath,target, strlen(target));
                 luaScriptPathJ= ScanForConfig(CONTROL_LUA_PATH , CTL_SCAN_RECURSIVE,luaScriptPath,".lua");
@@ -820,7 +820,7 @@ STATIC void LuaDoAction (LuaDoActionT action, AFB_ReqT request) {
 
                 if (index > 0) AFB_ApiWarning(source->api, "LUA-DOSCRIPT-SCAN:Ignore second script=%s path=%s", filename, fullpath);
                 else {
-                    strncpy (luaScriptPath, fullpath, strlen(fullpath));
+                    strncpy (luaScriptPath, fullpath, strlen(fullpath)+1);
                     strncat (luaScriptPath, "/", strlen("/"));
                     strncat (luaScriptPath, filename, strlen(filename));
                 }
@@ -841,7 +841,7 @@ STATIC void LuaDoAction (LuaDoActionT action, AFB_ReqT request) {
 
             // if no func name given try to deduct from filename
             if (!func && (func=(char*)GetMidleName(filename))!=NULL) {
-                strncpy(luaScriptPath,"_", strlen("_"));
+                strncpy(luaScriptPath,"_", strlen("_")+1);
                 strncat(luaScriptPath,func, strlen(func));
                 func=luaScriptPath;
             }
@@ -1269,7 +1269,7 @@ PUBLIC int LuaConfigExec (AFB_ApiT apiHandle, const char* prefix) {
 
     // search for default policy config files
     char fullprefix[CONTROL_MAXPATH_LEN];
-    strncpy (fullprefix, prefix, strlen(prefix));
+    strncpy (fullprefix, prefix, strlen(prefix)+1);
     strncat (fullprefix, "-", strlen("-"));
     strncat (fullprefix, GetBinderName(), strlen(GetBinderName()));
     strncat (fullprefix, "-", strlen("-"));
@@ -1296,7 +1296,7 @@ PUBLIC int LuaConfigExec (AFB_ApiT apiHandle, const char* prefix) {
         }
 
         char filepath[CONTROL_MAXPATH_LEN];
-        strncpy(filepath, fullpath, strlen(fullpath));
+        strncpy(filepath, fullpath, strlen(fullpath)+1);
         strncat(filepath, "/", strlen("/"));
         strncat(filepath, filename, strlen(filename));
         err= luaL_loadfile(luaState, filepath);
diff --git a/ctl-lib/ctl-plugin.c b/ctl-lib/ctl-plugin.c
index 0df21ae..11df1e3 100644
--- a/ctl-lib/ctl-plugin.c
+++ b/ctl-lib/ctl-plugin.c
@@ -119,7 +119,7 @@ STATIC int PluginLoadOne (AFB_ApiT apiHandle, CtlPluginT *ctlPlugin, json_object
     }
 
     char pluginpath[CONTROL_MAXPATH_LEN];
-    strncpy(pluginpath, fullpath, strlen (fullpath));
+    strncpy(pluginpath, fullpath, strlen (fullpath)+1);
     strncat(pluginpath, "/", strlen ("/"));
     strncat(pluginpath, filename, strlen (filename));
     dlHandle = dlopen(pluginpath, RTLD_NOW);
@@ -156,7 +156,7 @@ STATIC int PluginLoadOne (AFB_ApiT apiHandle, CtlPluginT *ctlPlugin, json_object
 
         int Lua2cAddOne(luaL_Reg *l2cFunc, const char* l2cName, int index) {
             char funcName[CONTROL_MAXPATH_LEN];
-            strncpy(funcName, "lua2c_", strlen ("lua2c_"));
+            strncpy(funcName, "lua2c_", strlen ("lua2c_")+1);
             strncat(funcName, l2cName, strlen (l2cName));
 
             Lua2cFunctionT l2cFunction = (Lua2cFunctionT) dlsym(dlHandle, funcName);
-- 
cgit