Memory leak for equal cred contents 91/182891/1
authorVitalii Irkha <v.irkha@samsung.com>
Mon, 25 Jun 2018 15:33:11 +0000 (18:33 +0300)
committerAmit KS <amit.s12@samsung.com>
Thu, 28 Jun 2018 10:40:00 +0000 (16:10 +0530)
Fix the issue in AddCredential api

https://github.sec.samsung.net/RS7-IOTIVITY/IoTivity/pull/300
(cherry picked from commit 0538b11eb0600edbf18bc43af4c3493369f99e09)

Change-Id: I0211724897f604ba88dcfb33147cb219a502ef65
Signed-off-by: Vitalii Irkha <v.irkha@samsung.com>
Signed-off-by: Amit KS <amit.s12@samsung.com>
resource/csdk/security/src/credresource.c

index ad0e8b7..d3ac759 100644 (file)
@@ -1462,6 +1462,7 @@ OCStackResult AddCredential(OicSecCred_t * newCred)
     }
     else
     {
+        OicSecCred_t *prev = NULL;
         LL_FOREACH(gCred, temp)
         {
             CredCompareResult_t cmpRes = CompareCredential(temp, newCred);
@@ -1470,6 +1471,18 @@ OCStackResult AddCredential(OicSecCred_t * newCred)
                 OIC_LOG_V(WARNING, TAG, "Detected same credential ID(%d)" \
                           "new credential's ID will be replaced.", temp->credId);
                 newCred->credId = temp->credId;
+                newCred->next = temp->next;
+
+                if(NULL == prev)
+                {
+                    gCred = newCred;
+                }
+                else
+                {
+                    prev->next = newCred;
+                }
+
+                FreeCred(temp);
                 ret = OC_STACK_OK;
                 validFlag = false;
                 break;
@@ -1482,6 +1495,8 @@ OCStackResult AddCredential(OicSecCred_t * newCred)
                 validFlag = false;
                 break;
             }
+
+            prev = temp;
         }
     }