Memory leak fixes 79/214379/1
authorVitalii Irkha <v.irkha@samsung.com>
Mon, 16 Sep 2019 10:44:38 +0000 (13:44 +0300)
committerSudipto <sudipto.bal@samsung.com>
Fri, 20 Sep 2019 16:42:20 +0000 (22:12 +0530)
Memory leak fixes in sec. res. provider

https://github.sec.samsung.net/RS7-IOTIVITY/IoTivity/commit/e7985fedfadaa0fc1371c8a0fc376c9d85fafaac
(cherry-picked from e7985fedfadaa0fc1371c8a0fc376c9d85fafaac)

Change-Id: Iabb45f6c26ed1559314922e31871c0077522fb85
Signed-off-by: Vitalii Irkha <v.irkha@samsung.com>
Signed-off-by: Sudipto <sudipto.bal@samsung.com>
resource/csdk/security/provisioning/src/secureresourceprovider.c

index af5ecf9..9948a13 100644 (file)
@@ -559,7 +559,8 @@ OCStackResult SRPProvisionTrustCertChain(void *ctx, OicSecCredType_t type, uint1
 OCStackResult SRPSaveTrustCertChain(uint8_t *trustCertChain, size_t chainSize,
                                             OicEncodingType_t encodingType, uint16_t *credId)
 {
-    OIC_LOG(DEBUG, TAG, "IN SRPSaveTrustCertChain");
+    OIC_LOG_V(DEBUG, TAG, "In %s", __func__);
+
     VERIFY_NON_NULL_RET(TAG, trustCertChain, ERROR,  OC_STACK_INVALID_PARAM);
     VERIFY_NON_NULL_RET(TAG, credId, ERROR,  OC_STACK_INVALID_PARAM);
 
@@ -580,17 +581,16 @@ OCStackResult SRPSaveTrustCertChain(uint8_t *trustCertChain, size_t chainSize,
     res = GetDoxmDeviceID(&cred->subject);
     if (OC_STACK_OK != res)
     {
-        OIC_LOG(ERROR, TAG, "Cann't get the device id(GetDoxmDeviceID)");
-        DeleteCredList(cred);
-        return res;
+        OIC_LOG(ERROR, TAG, "Can't get the device id(GetDoxmDeviceID)");
+        goto exit;
     }
 
     cred->credUsage= (char *)OICCalloc(1, strlen(TRUST_CA)+1 );
     if (cred->credUsage == NULL)
     {
-        OIC_LOG_V(ERROR, TAG, "%s cant alloc credUsage", __func__);
-        OICFree(cred);
-        return OC_STACK_NO_MEMORY;
+        OIC_LOG_V(ERROR, TAG, "%s can't alloc credUsage", __func__);
+        res = OC_STACK_NO_MEMORY;
+        goto exit;
     }
     OICStrcpy(cred->credUsage, strlen(TRUST_CA) + 1, TRUST_CA);
 
@@ -601,16 +601,21 @@ OCStackResult SRPSaveTrustCertChain(uint8_t *trustCertChain, size_t chainSize,
         cred->optionalData.data = (uint8_t *)OICCalloc(1, chainSize + 1);
         if (cred->optionalData.data == NULL)
         {
-            OIC_LOG_V(ERROR, TAG, "%s cant alloc cred->optionalData.data", __func__);
-            OICFree(cred);
-            return OC_STACK_NO_MEMORY;
+            OIC_LOG_V(ERROR, TAG, "%s can't alloc cred->optionalData.data", __func__);
+            res = OC_STACK_NO_MEMORY;
+            goto exit;
         }
         cred->optionalData.len = chainSize + 1;
     }
     else
     {
-        cred->optionalData.data = (uint8_t *)OICCalloc(1, chainSize);
-        VERIFY_NON_NULL_RET(TAG, cred->optionalData.data, ERROR, OC_STACK_NO_MEMORY);
+        cred->optionalData.data = (uint8_t *)OICCalloc(chainSize, sizeof(uint8_t));
+        if (NULL == cred->optionalData.data)
+       {
+            OIC_LOG_V(ERROR, TAG, "%s can't alloc cred->optionalData.data", __func__);
+            res = OC_STACK_NO_MEMORY;
+            goto exit;
+       }
         cred->optionalData.len = chainSize;
     }
     memcpy(cred->optionalData.data, trustCertChain, chainSize);
@@ -620,22 +625,27 @@ OCStackResult SRPSaveTrustCertChain(uint8_t *trustCertChain, size_t chainSize,
     res = AddCredential(cred);
     if(res != OC_STACK_OK)
     {
-        DeleteCredList(cred);
-        return res;
+        OIC_LOG_V(ERROR, TAG, "%s can't add cred", __func__);
+        goto exit;
     }
     *credId = cred->credId;
 
     if (g_trustCertChainNotifier.callback)
     {
-        uint8_t *certChain = (uint8_t*)OICCalloc(1, sizeof(uint8_t) * chainSize);
+        uint8_t *certChain = (uint8_t*)OICCalloc(chainSize, sizeof(uint8_t));
         VERIFY_NON_NULL_RET(TAG, certChain, ERROR, OC_STACK_NO_MEMORY);
         memcpy(certChain, trustCertChain, chainSize);
         g_trustCertChainNotifier.callback(g_trustCertChainNotifier.context, *credId,
                 certChain, chainSize);
         OICFree(certChain);
     }
+exit:
+    if (OC_STACK_OK != res)
+    {
+        DeleteCredList(cred);
+    }
 
-    OIC_LOG(DEBUG, TAG, "OUT SRPSaveTrustCertChain");
+    OIC_LOG_V(DEBUG, TAG, "Out %s", __func__);
 
     return res;
 }
@@ -652,44 +662,46 @@ OCStackResult SRPSaveOwnCertChain(OicSecKey_t * cert, OicSecKey_t * key, uint16_
 
     OCStackResult res = OC_STACK_ERROR;
 
-    OicSecCred_t *cred = (OicSecCred_t *)OICCalloc(1, sizeof(*cred));
+    OicSecCred_t *cred = (OicSecCred_t *)OICCalloc(1, sizeof(OicSecCred_t));
     VERIFY_NON_NULL_RET(TAG, cred, ERROR, OC_STACK_NO_MEMORY);
 
-    OIC_LOG_V(DEBUG, TAG, "IN: %s", __func__);
-
     res = GetDoxmDeviceID(&cred->subject);
     if (OC_STACK_OK != res)
     {
-        OIC_LOG(ERROR, TAG, "Cann't get the device id(GetDoxmDeviceID)");
-        DeleteCredList(cred);
-        return res;
+        OIC_LOG(ERROR, TAG, "Can't get the device id(GetDoxmDeviceID)");
+        goto exit;
     }
 
-    cred->credUsage= (char *)OICCalloc(1, strlen(PRIMARY_CERT)+1 );
+    cred->credUsage= (char *)OICCalloc(strlen(PRIMARY_CERT) + 1, sizeof(char));
     if (cred->credUsage == NULL)
     {
-        OIC_LOG_V(ERROR, TAG, "%s cant alloc credUsage", __func__);
-        OICFree(cred);
-        return OC_STACK_NO_MEMORY;
+        OIC_LOG_V(ERROR, TAG, "%s can't alloc credUsage", __func__);
+        res = OC_STACK_NO_MEMORY;
+        goto exit;
     }
     OICStrcpy(cred->credUsage, strlen(PRIMARY_CERT) + 1, PRIMARY_CERT) ;
 
     cred->credType = SIGNED_ASYMMETRIC_KEY;
 
     OicSecKey_t *publicData = &cred->publicData;
-    publicData->data = (uint8_t *)OICCalloc(1, cert->len);
-    VERIFY_NON_NULL_RET(TAG, publicData->data, ERROR, OC_STACK_NO_MEMORY);
+    publicData->data = (uint8_t *)OICCalloc(cert->len, sizeof(uint8_t));
+    if (NULL == publicData->data)
+    {
+        OIC_LOG_V(ERROR, TAG, "%s can't alloc publicData", __func__);
+        res = OC_STACK_NO_MEMORY;
+        goto exit;
+    }
     memcpy(publicData->data, cert->data, cert->len);
     publicData->len = cert->len;
     publicData->encoding = cert->encoding;
 
     OicSecKey_t *privateData = &cred->privateData;
-    privateData->data = (uint8_t *)OICCalloc(1, key->len);
-    if (privateData->data == NULL)
+    privateData->data = (uint8_t *)OICCalloc(key->len, sizeof(uint8_t));
+    if (NULL == privateData->data)
     {
-        OIC_LOG_V(ERROR, TAG, "%s cant alloc publicData->data", __func__);
-        OICFree(publicData->data);
-        return OC_STACK_NO_MEMORY;
+        OIC_LOG_V(ERROR, TAG, "%s can't alloc privateData->data", __func__);
+        res = OC_STACK_NO_MEMORY;
+        goto exit;
     }
     memcpy(privateData->data, key->data, key->len);
     privateData->len = key->len;
@@ -698,11 +710,15 @@ OCStackResult SRPSaveOwnCertChain(OicSecKey_t * cert, OicSecKey_t * key, uint16_
     res = AddCredential(cred);
     if(res != OC_STACK_OK)
     {
-        DeleteCredList(cred);
-        return res;
+        OIC_LOG_V(ERROR, TAG, "%s can't add cred", __func__);
+        goto exit;
     }
     *credId = cred->credId;
-
+exit:
+    if (OC_STACK_OK != res)
+    {
+        DeleteCredList(cred);
+    }
     OIC_LOG_V(DEBUG, TAG, "Out %s", __func__);
 
     return res;