[IOT-1569] Use OICMalloc/OICFree consistently
authorKevin Kane <kkane@microsoft.com>
Sat, 19 Nov 2016 00:17:11 +0000 (16:17 -0800)
committerPhil Coval <philippe.coval@osg.samsung.com>
Wed, 23 Nov 2016 10:35:41 +0000 (10:35 +0000)
Lingering and mixed uses of malloc/free exist throughout
IoTivity. Some buffers allocated with malloc are later freed
with OICFree; this is dangerous in case the implementation of
OICMalloc/OICFree ever changes.

This change harmonizes the code to use the OIC versions to
ensure code is always using the same heap allocation and free
functions with two exceptions:

* Sample code continues to use malloc/free; samples should not
refer to internal functions.
* The oc_logger module also still uses malloc/free; oic_malloc
calls oc_logger when operating in ENABLE_MALLOC_DEBUG mode, and
could result in a circular dependency if changed over.

Change-Id: Icd47243eec33500fe7fcd7d5d79ddf2128a9e717
Signed-off-by: Kevin Kane <kkane@microsoft.com>
Reviewed-on: https://gerrit.iotivity.org/gerrit/14623
Tested-by: jenkins-iotivity <jenkins-iotivity@opendaylight.org>
Reviewed-by: Mike Fenelon <mike.fenelon@microsoft.com>
Reviewed-by: Dan Mihai <Daniel.Mihai@microsoft.com>
Reviewed-by: Phil Coval <philippe.coval@osg.samsung.com>
resource/csdk/connectivity/test/ca_api_unittest.cpp
resource/csdk/connectivity/test/uqueue_test.cpp
resource/csdk/stack/include/ocpayload.h
resource/csdk/stack/src/ocpayload.c
resource/csdk/stack/src/oicgroup.c

index c2e5fff7960eefdabfa5adb6c3f8a188261f12cd..6435a56e857e3bfc89072470119bad08543aaf79 100644 (file)
@@ -24,6 +24,7 @@
 #include "cautilinterface.h"
 #include "cacommon.h"
 #include "oic_string.h"
+#include "oic_malloc.h"
 
 #define CA_TRANSPORT_ADAPTER_SCOPE  1000
 
@@ -343,7 +344,7 @@ TEST_F(CATests, SendResponseTestWithInvalidCode)
     memset(&responseData, 0, sizeof(CAInfo_t));
     responseData.type = CA_MSG_RESET;
     responseData.messageId = 1;
-    responseData.payload = (CAPayload_t)malloc(sizeof("response payload"));
+    responseData.payload = (CAPayload_t)OICMalloc(sizeof("response payload"));
     responseData.dataType = CA_RESPONSE_DATA;
 
     EXPECT_TRUE(responseData.payload != NULL);
@@ -362,7 +363,7 @@ TEST_F(CATests, SendResponseTestWithInvalidCode)
 
         CADestroyToken(tempToken);
         CADestroyEndpoint(tempRep);
-        free(responseData.payload);
+        OICFree(responseData.payload);
         tempRep = NULL;
     }
 }
index c393cd3a7ac572fc560fbe52862565afb467808d..f461d4636254ef68ef7501a41738cc73f8d94e93 100644 (file)
@@ -22,6 +22,8 @@
 
 #include "uqueue.h"
 
+#include "oic_malloc.h"
+
 class UQueueF : public testing::Test {
 public:
     UQueueF() :
@@ -47,7 +49,7 @@ protected:
 
 u_queue_message_t *CreateQueueMessage(void *data, uint32_t size)
 {
-    u_queue_message_t *message = (u_queue_message_t *) malloc(sizeof(u_queue_message_t));
+    u_queue_message_t *message = (u_queue_message_t *) OICMalloc(sizeof(u_queue_message_t));
     if (NULL == message)
     {
         return NULL;
index d84a7ab98b2658f067a2986a5dbba8d1b2046577..07cb1a771abe45600e8149ecd243e22feb1f5d5c 100644 (file)
@@ -280,7 +280,7 @@ OCStringLL* OCCreateOCStringLL(const char* text);
 /**
  * This function creates a string from a list (with separated contents if several)
  * @param ll           Pointer to list
- * @return newly allocated string
+ * @return newly allocated string. Caller takes ownership and must later free this memory with OICFree.
  * @note separator is ',' (according to rfc4180)
  **/
 char* OCCreateString(const OCStringLL* ll);
index 22dd491b78382c73db66cb7b8d8230e569dd9cf2..127cd2ef0ed43cdaf5417557d32470a287cb7ce2 100644 (file)
@@ -1501,7 +1501,7 @@ char* OCCreateString(const OCStringLL* ll)
         len += strlen(it->value) + 1;
     }
     len--; // remove trailing separator (just added above)
-    str = (char*) malloc(len + 1);
+    str = (char*) OICMalloc(len + 1);
     if (!str)
     {
         return NULL;
@@ -1515,7 +1515,7 @@ char* OCCreateString(const OCStringLL* ll)
         count = snprintf(pos, len + 1, "%s", it->value);
         if ((size_t)count < sublen)
         {
-            free(str);
+            OICFree(str);
             return NULL;
         }
         len -= sublen;
index 0ece45af43154a9d0c97ca541ff5fdd2b22bead7..660ad8fabeac5afb6e145104a6353a17199ca3f5 100755 (executable)
@@ -811,7 +811,7 @@ OCStackResult BuildStringFromActionSet(OCActionSet* actionset, char** desc)
         goto exit;
     }
 
-    actionTypeStr = (char *)malloc(1024);
+    actionTypeStr = (char *)OICMalloc(1024);
     if(actionTypeStr != NULL)
     {
         sprintf(actionTypeStr, "%ld %u", actionset->timesteps, actionset->type);
@@ -821,11 +821,11 @@ OCStackResult BuildStringFromActionSet(OCActionSet* actionset, char** desc)
             remaining -= strlen(actionTypeStr);
             strncat(temp, ACTION_DELIMITER, strlen(ACTION_DELIMITER));
             remaining -= strlen(ACTION_DELIMITER);
-            free(actionTypeStr);
+            OICFree(actionTypeStr);
         }
         else
         {
-            free(actionTypeStr);
+            OICFree(actionTypeStr);
             res = OC_STACK_ERROR;
             goto exit;
         }