mot-agent: Perform deep copy in cloning OCProvisionDev_t
authorSaurav Babu <saurav.babu@samsung.com>
Fri, 16 Feb 2018 11:07:28 +0000 (16:37 +0530)
committersaerome.kim <saerome.kim@samsung.com>
Mon, 2 Jul 2018 10:38:48 +0000 (19:38 +0900)
Signed-off-by: Saurav Babu <saurav.babu@samsung.com>
src/mot-agent/ma-subowner.c

index 77edf7a..9d453f2 100755 (executable)
@@ -388,6 +388,75 @@ static FILE* _fopen_prvn_mng(const char* path, const char* mode)
        return fopen(data_dir, mode);
 }
 
+static OCProvisionDev_t* _clone_ocprovision_dev(const OCProvisionDev_t* src)
+{
+       MA_LOGD("");
+
+       if (!src)
+       {
+               MA_LOGD("Invalid parameter");
+               return NULL;
+       }
+
+       OCProvisionDev_t* newDev = g_try_malloc0(sizeof(OCProvisionDev_t));
+
+       memcpy(&newDev->endpoint, &src->endpoint, sizeof(OCDevAddr));
+
+       if (src->pstat)
+       {
+               newDev->pstat= g_try_malloc0(sizeof(OicSecPstat_t));
+
+               memcpy(newDev->pstat, src->pstat, sizeof(OicSecPstat_t));
+               // We have to assign NULL for not necessary information to prevent memory corruption.
+               newDev->pstat->sm = g_try_malloc0(sizeof(OicSecDpom_t));
+               memcpy(newDev->pstat->sm, src->pstat->sm, sizeof(OicSecDpom_t));
+       }
+
+       if (src->doxm)
+       {
+               newDev->doxm = g_try_malloc0(sizeof(OicSecDoxm_t));
+
+               memcpy(newDev->doxm, src->doxm, sizeof(OicSecDoxm_t));
+
+               newDev->doxm->oxmType = g_try_malloc0(src->doxm->oxmTypeLen + 1);
+               memcpy(newDev->doxm->oxmType, src->doxm->oxmType,
+                          src->doxm->oxmTypeLen);
+
+               newDev->doxm->oxm = g_try_malloc0(sizeof(OicSecOxm_t));
+               memcpy(newDev->doxm->oxm, src->doxm->oxm, sizeof(OicSecOxm_t));
+
+               newDev->doxm->mom = g_try_malloc0(sizeof(OicSecMom_t));
+               memcpy(newDev->doxm->mom, src->doxm->mom, sizeof(OicSecMom_t));
+
+               if (src->doxm->subOwners) {
+                       OicSecSubOwner_t* subowner = NULL;
+                       OicSecSubOwner_t* temp = NULL;
+                       OicSecSubOwner_t* temp2 = NULL;
+                       LL_FOREACH_SAFE(src->doxm->subOwners, subowner, temp) {
+                               temp2 = g_try_malloc0(sizeof(OicSecSubOwner_t));
+                               memcpy(temp2, subowner, sizeof(OicSecSubOwner_t));
+                               LL_APPEND(newDev->doxm->subOwners, temp2);
+                       }
+               }
+       }
+
+       if (0 == strlen(src->secVer))
+       {
+               g_strlcpy(newDev->secVer, "0.0.0", OIC_SEC_MAX_VER_LEN);
+       }
+       else
+       {
+               g_strlcpy(newDev->secVer, src->secVer, OIC_SEC_MAX_VER_LEN);
+       }
+
+       newDev->securePort = src->securePort;
+       newDev->devStatus = src->devStatus;
+       newDev->connType = src->connType;
+       newDev->next = NULL;
+
+       return newDev;
+}
+
 #ifdef THREAD_COND_WAIT_USED
 /*
  * data structure
@@ -514,7 +583,7 @@ static void _remove_mot_client()
        ma_check_null_ret("g_client", g_client);
 
        if (g_client->g_motdev_list) {
-               //OCDeleteDiscoveredDevices(g_client->g_motdev_list);
+               OCDeleteDiscoveredDevices(g_client->g_motdev_list);
                g_client->g_motdev_list = NULL;
        }
 }
@@ -907,9 +976,8 @@ static void _update_mot_dev_list(ma_req_cb_s *con, OCProvisionDev_t *list)
                        OCProvisionDev_t *temp2;
                        OCProvisionDev_t *temp = iter;
                        temp->next = NULL;
-                       temp2 = PMCloneOCProvisionDev(temp);
-                       temp2->next = dev_list;
-                       dev_list = temp2;
+                       temp2 = _clone_ocprovision_dev(temp);
+                       LL_APPEND(dev_list, temp2);
 
                        /* Notify Mot Enabled Device added */
                        g_variant_builder_init(&builder, G_VARIANT_TYPE("a{sv}"));