agm: initialize stream module list properly

gkv of stream module is not initialized, which can
be random values and causing crash in deallocation.

Initialize gkv for stream module when added to graph
tagged module list.

Change-Id: Ic2ee55677a5cec8b51b556710bc49a8c4e6195dd
diff --git a/service/src/graph.c b/service/src/graph.c
index 0e79b8c..f8efc1f 100644
--- a/service/src/graph.c
+++ b/service/src/graph.c
@@ -538,6 +538,7 @@
     size_t arraysize = 0;
     module_info_t *stream_module_list = NULL;
     module_info_t *hw_ep_module = NULL;
+    module_info_t *add_module = NULL;
 
     list_declare(node_sess);
     list_init(&node_sess);
@@ -616,10 +617,16 @@
                         goto free_graph_obj;
                     }
                     mod = mod_list->data;
-                    mod->miid = gsl_tag_entry->module_entry[0].module_iid;
-                    mod->mid = gsl_tag_entry->module_entry[0].module_id;
-                    AGM_LOGD("miid %x mid %x tag %x", mod->miid, mod->mid, mod->tag);
-                    ADD_MODULE(*mod, NULL);
+                    add_module = ADD_MODULE(*mod, NULL);
+                    if (!add_module) {
+                        AGM_LOGE("no memory to allocate add_module");
+                        ret = -ENOMEM;
+                        goto free_graph_obj;
+                    }
+                    add_module->miid = gsl_tag_entry->module_entry[0].module_iid;
+                    add_module->mid = gsl_tag_entry->module_entry[0].module_id;
+                    add_module->gkv = NULL;
+                    AGM_LOGD("miid %x mid %x tag %x", add_module->miid, add_module->mid, add_module->tag);
                     goto tag_list;
                 }
             }
@@ -641,8 +648,14 @@
                         goto free_graph_obj;
                     }
                     mod = mod_list->data;
-                    mod->miid = gsl_tag_entry->module_entry[0].module_iid;
-                    mod->mid = gsl_tag_entry->module_entry[0].module_id;
+                    add_module = ADD_MODULE(*mod, dev_obj);
+                    if (!add_module) {
+                        AGM_LOGE("no memory to allocate add_module");
+                        ret = -ENOMEM;
+                        goto free_graph_obj;
+                    }
+                    add_module->miid = gsl_tag_entry->module_entry[0].module_iid;
+                    add_module->mid = gsl_tag_entry->module_entry[0].module_id;
                     /*store GKV which describes/contains this module*/
                     gkv = calloc(1, sizeof(struct agm_key_vector_gsl));
                     if (!gkv) {
@@ -660,9 +673,8 @@
                     }
                     memcpy(gkv->kv, meta_data_kv->gkv.kv,
                           gkv->num_kvs * sizeof(struct agm_key_value));
-                    mod->gkv = gkv;
-                    AGM_LOGD("miid %x mid %x tag %x", mod->miid, mod->mid, mod->tag);
-                    ADD_MODULE(*mod, dev_obj);
+                    add_module->gkv = gkv;
+                    AGM_LOGD("miid %x mid %x tag %x", add_module->miid, add_module->mid, add_module->tag);
                     goto tag_list;
                 }
             }
@@ -1457,6 +1469,7 @@
         bool mod_present = false;
         size_t arraysize;
         module_info_t *hw_ep_module = NULL;
+        module_info_t *add_module = NULL;
 
         get_hw_ep_module_list_array(&hw_ep_module, &arraysize);
         if (dev_obj->hw_ep_info.dir == AUDIO_OUTPUT)
@@ -1474,8 +1487,6 @@
                           mod->tag);
             goto done;
         }
-        mod->miid = module_info->module_entry[0].module_iid;
-        mod->mid = module_info->module_entry[0].module_id;
         /**
          *Check if this is the same device object as was passed for graph open
          *or a new one.We do this by comparing the module_iid of the module
@@ -1484,7 +1495,7 @@
          */
         list_for_each(node, &graph_obj->tagged_mod_list) {
             temp_mod = node_to_item(node, module_info_t, list);
-            if (temp_mod->miid == mod->miid) {
+            if (temp_mod->miid == module_info->module_entry[0].module_iid) {
                 mod_present = true;
                 /**
                  * Module might have configured previously as we don't reset in
@@ -1501,6 +1512,14 @@
              */
             /*Make a local copy of gkv and use when we query gsl
             for tagged data*/
+            add_module = ADD_MODULE(*mod, dev_obj);
+            if (!add_module) {
+                AGM_LOGE("no memory to allocate add_module");
+                ret = -ENOMEM;
+                goto done;
+            }
+            add_module->miid = module_info->module_entry[0].module_iid;
+            add_module->mid = module_info->module_entry[0].module_id;
             gkv = calloc(1, sizeof(struct agm_key_vector_gsl));
             if (!gkv) {
                 AGM_LOGE("No memory to allocate for gkv\n");
@@ -1517,11 +1536,10 @@
             }
             memcpy(gkv->kv, meta_data_kv->gkv.kv,
                     gkv->num_kvs * sizeof(struct agm_key_value));
-            mod->gkv = gkv;
+            add_module->gkv = gkv;
             gkv = NULL;
             AGM_LOGD("Adding the new module tag %x mid %x miid %x\n",
-                                    mod->tag, mod->mid, mod->miid);
-            ADD_MODULE(*mod, dev_obj);
+                    add_module->tag, add_module->mid, add_module->miid);
         }
     }
     /* Configure the newly added modules only if graph is in start state,