[IOT-995] Fix memory reallocation for security resources
authorHabib Virji <habib.virji@samsung.com>
Thu, 24 Mar 2016 22:26:46 +0000 (22:26 +0000)
committerHabib Virji <habib.virji@samsung.com>
Fri, 25 Mar 2016 08:52:51 +0000 (08:52 +0000)
When CborOutOfMemory is generated reallocation should be based on
encoder.ptr incremented value.

It also fixes allocation for security payload. It allocates value
based on the payload size.

Change-Id: I5213679d65e48bb91cd590d3cc654a6cd80b2af2
Signed-off-by: Habib Virji <habib.virji@samsung.com>
Reviewed-on: https://gerrit.iotivity.org/gerrit/6241
Tested-by: jenkins-iotivity <jenkins-iotivity@opendaylight.org>
Reviewed-by: Chul Lee <chuls.lee@samsung.com>
Reviewed-by: Yonggoo Kang <ygace.kang@samsung.com>
resource/csdk/stack/src/ocpayloadconvert.c
resource/csdk/stack/src/ocpayloadparse.c

index 6514142..c8a60fe 100644 (file)
@@ -79,9 +79,20 @@ OCStackResult OCConvertPayload(OCPayload* payload, uint8_t** outPayload, size_t*
     VERIFY_PARAM_NON_NULL(TAG, size, "size parameter is NULL");
 
     OIC_LOG_V(INFO, TAG, "Converting payload of type %d", payload->type);
-
-    out = (uint8_t *)OICCalloc(1, curSize);
-    VERIFY_PARAM_NON_NULL(TAG, out, "Failed to allocate payload");
+    if (PAYLOAD_TYPE_SECURITY == payload->type)
+    {
+        size_t securityPayloadSize = ((OCSecurityPayload *)payload)->payloadSize;
+        if (securityPayloadSize > 0)
+        {
+            out = (uint8_t *)OICCalloc(1, ((OCSecurityPayload *)payload)->payloadSize);
+            VERIFY_PARAM_NON_NULL(TAG, out, "Failed to allocate security payload");
+        }
+    }
+    if (out == NULL)
+    {
+        out = (uint8_t *)OICCalloc(1, curSize);
+        VERIFY_PARAM_NON_NULL(TAG, out, "Failed to allocate payload");
+    }
     err = OCConvertPayloadHelper(payload, out, &curSize);
     ret = OC_STACK_NO_MEMORY;
 
@@ -112,7 +123,8 @@ OCStackResult OCConvertPayload(OCPayload* payload, uint8_t** outPayload, size_t*
 
         *size = curSize;
         *outPayload = out;
-        OIC_LOG_V(DEBUG, TAG, "Payload Size: %zd Payload : %s \n", *size, *outPayload);
+        OIC_LOG_V(DEBUG, TAG, "Payload Size: %zd Payload : ", *size);
+        OIC_LOG_BUFFER(DEBUG, TAG, *outPayload, *size);
         return OC_STACK_OK;
     }
 
@@ -170,17 +182,6 @@ static int64_t checkError(int64_t err, CborEncoder* encoder, uint8_t* outPayload
 static int64_t OCConvertSecurityPayload(OCSecurityPayload* payload, uint8_t* outPayload,
         size_t* size)
 {
-    if (*size < payload->payloadSize)
-    {
-        uint8_t *out2 = (uint8_t *)OICRealloc(outPayload, payload->payloadSize);
-        if (!out2)
-        {
-            OICFree(outPayload);
-            return CborErrorOutOfMemory;
-        }
-        outPayload = out2;
-    }
-
     memcpy(outPayload, payload->securityData1, payload->payloadSize);
     *size = payload->payloadSize;
 
index f2bbd53..24a30b5 100644 (file)
@@ -55,7 +55,9 @@ OCStackResult OCParsePayload(OCPayload **outPayload, OCPayloadType payloadType,
     VERIFY_PARAM_NON_NULL(TAG, outPayload, "Conversion of outPayload failed");
     VERIFY_PARAM_NON_NULL(TAG, payload, "Invalid cbor payload value");
 
-    OIC_LOG_V(INFO, TAG, "CBOR Parsing size: %zu", payloadSize);
+    OIC_LOG_V(INFO, TAG, "CBOR Parsing size: %zu of Payload Type: %d, Payload:",
+            payloadSize, payloadType);
+    OIC_LOG_BUFFER(DEBUG, TAG, payload, payloadSize);
 
     CborParser parser;
     CborValue rootValue;