mesh/mesh-config-json: Fix errors found by static analysis
authorInga Stotland <inga.stotland@intel.com>
Fri, 6 Nov 2020 22:22:45 +0000 (14:22 -0800)
committerAbhay Agarwal <ay.agarwal@samsung.com>
Mon, 28 Dec 2020 06:20:04 +0000 (11:50 +0530)
This fixes miscellaneous issues related to unchecked return status
and (temporary) allocated memory leaks due to daemon's exit on startup
failure.

Change-Id: Icf621e459ed4f045a740a78f3252dcdde8621350
Signed-off-by: anuj.bhumiya <anuj.bhumiya@samsung.com>
mesh/mesh-config-json.c

index c58348d..f3ba1a0 100644 (file)
@@ -476,6 +476,7 @@ static bool read_app_keys(json_object *jobj, struct mesh_config_node *node)
                struct mesh_config_appkey *appkey;
 
                appkey = l_new(struct mesh_config_appkey, 1);
+               l_queue_push_tail(node->appkeys, appkey);
 
                jtemp = json_object_array_get_idx(jarray, i);
 
@@ -497,8 +498,6 @@ static bool read_app_keys(json_object *jobj, struct mesh_config_node *node)
 
                if (!str2hex(str, strlen(str), appkey->key, 16))
                        goto fail;
-
-               l_queue_push_tail(node->appkeys, appkey);
        }
 
        return true;
@@ -533,6 +532,8 @@ static bool read_net_keys(json_object *jobj, struct mesh_config_node *node)
 
                netkey = l_new(struct mesh_config_netkey, 1);
 
+               l_queue_push_tail(node->netkeys, netkey);
+
                jtemp = json_object_array_get_idx(jarray, i);
 
                if (!get_key_index(jtemp, "index", &netkey->idx))
@@ -562,8 +563,6 @@ static bool read_net_keys(json_object *jobj, struct mesh_config_node *node)
 
                if (!str2hex(str, strlen(str), netkey->key, 16))
                        goto fail;
-
-               l_queue_push_tail(node->netkeys, netkey);
        }
 
        return true;
@@ -1037,7 +1036,7 @@ static bool parse_model_subscriptions(json_object *jsubs,
 
                jvalue = json_object_array_get_idx(jsubs, i);
                if (!jvalue)
-                       return false;
+                       goto fail;
 
                str = (char *)json_object_get_string(jvalue);
                len = strlen(str);
@@ -1046,7 +1045,7 @@ static bool parse_model_subscriptions(json_object *jsubs,
                case 4:
                        if (sscanf(str, "%04hx", &subs[i].addr.grp) != 1)
                                goto fail;
-               break;
+                       break;
                case 32:
                        if (!str2hex(str, len, subs[i].addr.label, 16))
                                goto fail;
@@ -1087,6 +1086,8 @@ static bool parse_models(json_object *jmodels, struct mesh_config_element *ele)
 
                mod = l_new(struct mesh_config_model, 1);
 
+               l_queue_push_tail(ele->models, mod);
+
                if (!json_object_object_get_ex(jmodel, "modelId", &jvalue))
                        goto fail;
 
@@ -1094,13 +1095,9 @@ static bool parse_models(json_object *jmodels, struct mesh_config_element *ele)
 
                len = strlen(str);
 
-               if (len != 4 && len != 8)
-                       goto fail;
-
                if (len == 4) {
                        if (sscanf(str, "%04x", &id) != 1)
                                goto fail;
-
                } else if (len == 8) {
                        if (sscanf(str, "%08x", &id) != 1)
                                goto fail;
@@ -1136,8 +1133,6 @@ static bool parse_models(json_object *jmodels, struct mesh_config_element *ele)
                        if (!parse_model_subscriptions(jarray, mod))
                                goto fail;
                }
-
-               l_queue_push_tail(ele->models, mod);
        }
 
        return true;
@@ -1585,16 +1580,19 @@ static void add_model(void *a, void *b)
 {
        struct mesh_config_model *mod = a;
        json_object *jmodels = b, *jmodel, *jval;
+       bool result;
 
        jmodel = json_object_new_object();
        if (!jmodel)
                return;
 
-       if (!mod->vendor)
-               write_uint16_hex(jmodel, "modelId",
-                                               (uint16_t) mod->id);
-       else
-               write_uint32_hex(jmodel, "modelId", mod->id);
+       result = (mod->vendor) ? write_uint32_hex(jmodel, "modelId", mod->id) :
+                       write_uint16_hex(jmodel, "modelId", (uint16_t) mod->id);
+
+       if (!result) {
+               json_object_put(jmodel);
+               return;
+       }
 
        jval = json_object_new_boolean(mod->sub_enabled);
        json_object_object_add(jmodel, "subEnabled", jval);
@@ -1672,24 +1670,24 @@ static struct mesh_config *create_config(const char *cfg_path,
 
                jelement = json_object_new_object();
 
-               if (!jelement) {
-                       json_object_put(jelems);
-                       return NULL;
-               }
+               if (!jelement)
+                       goto fail;
 
-               write_int(jelement, "elementIndex", ele->index);
-               write_uint16_hex(jelement, "location", ele->location);
                json_object_array_add(jelems, jelement);
 
+               if (!write_int(jelement, "elementIndex", ele->index))
+                       goto fail;
+
+               if (!write_uint16_hex(jelement, "location", ele->location))
+                       goto fail;
+
                /* Models */
                if (l_queue_isempty(ele->models))
                        continue;
 
                jmodels = json_object_new_array();
-               if (!jmodels) {
-                       json_object_put(jelems);
-                       return NULL;
-               }
+               if (!jmodels)
+                       goto fail;
 
                json_object_object_add(jelement, "models", jmodels);
                l_queue_foreach(ele->models, add_model, jmodels);
@@ -1707,6 +1705,10 @@ static struct mesh_config *create_config(const char *cfg_path,
        gettimeofday(&cfg->write_time, NULL);
 
        return cfg;
+
+fail:
+               json_object_put(jelems);
+               return NULL;
 }
 
 struct mesh_config *mesh_config_create(const char *cfgdir_name,
@@ -2405,8 +2407,10 @@ static void idle_save_config(struct l_idle *idle, void *user_data)
 
        if (result) {
                remove(fname_bak);
-               rename(fname_cfg, fname_bak);
-               rename(fname_tmp, fname_cfg);
+
+               if (rename(fname_cfg, fname_bak) < 0 ||
+                                       rename(fname_tmp, fname_cfg) < 0)
+                       result = false;
        }
 
        remove(fname_tmp);