From 4c5a8baaa86a193d88a3d43eb958e38759a0eb04 Mon Sep 17 00:00:00 2001 From: Kevin Kane Date: Fri, 18 Nov 2016 16:17:11 -0800 Subject: [PATCH] [IOT-1569] Use OICMalloc/OICFree consistently 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 Reviewed-on: https://gerrit.iotivity.org/gerrit/14623 Tested-by: jenkins-iotivity Reviewed-by: Mike Fenelon Reviewed-by: Dan Mihai Reviewed-by: Phil Coval (cherry picked from commit 8141de8815b7c7a39b8b7d8ce9acb91608cf9e17) Reviewed-on: https://gerrit.iotivity.org/gerrit/14697 Reviewed-by: Omar Maabreh --- resource/csdk/connectivity/test/ca_api_unittest.cpp | 5 +++-- resource/csdk/connectivity/test/uqueue_test.cpp | 4 +++- resource/csdk/stack/include/ocpayload.h | 2 +- resource/csdk/stack/src/ocpayload.c | 4 ++-- resource/csdk/stack/src/oicgroup.c | 6 +++--- 5 files changed, 12 insertions(+), 9 deletions(-) diff --git a/resource/csdk/connectivity/test/ca_api_unittest.cpp b/resource/csdk/connectivity/test/ca_api_unittest.cpp index dca4e0c..8fb19fa 100644 --- a/resource/csdk/connectivity/test/ca_api_unittest.cpp +++ b/resource/csdk/connectivity/test/ca_api_unittest.cpp @@ -24,6 +24,7 @@ #include "cautilinterface.h" #include "cacommon.h" #include "oic_string.h" +#include "oic_malloc.h" #define CA_TRANSPORT_ADAPTER_SCOPE 1000 @@ -311,7 +312,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); @@ -330,7 +331,7 @@ TEST_F(CATests, SendResponseTestWithInvalidCode) CADestroyToken(tempToken); CADestroyEndpoint(tempRep); - free(responseData.payload); + OICFree(responseData.payload); tempRep = NULL; } } diff --git a/resource/csdk/connectivity/test/uqueue_test.cpp b/resource/csdk/connectivity/test/uqueue_test.cpp index c393cd3..f461d46 100644 --- a/resource/csdk/connectivity/test/uqueue_test.cpp +++ b/resource/csdk/connectivity/test/uqueue_test.cpp @@ -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; diff --git a/resource/csdk/stack/include/ocpayload.h b/resource/csdk/stack/include/ocpayload.h index 50f2464..0c534e9 100644 --- a/resource/csdk/stack/include/ocpayload.h +++ b/resource/csdk/stack/include/ocpayload.h @@ -269,7 +269,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); diff --git a/resource/csdk/stack/src/ocpayload.c b/resource/csdk/stack/src/ocpayload.c index 04fee58..6958938 100644 --- a/resource/csdk/stack/src/ocpayload.c +++ b/resource/csdk/stack/src/ocpayload.c @@ -1496,7 +1496,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; @@ -1510,7 +1510,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; diff --git a/resource/csdk/stack/src/oicgroup.c b/resource/csdk/stack/src/oicgroup.c index f0c0d4a..082f277 100755 --- a/resource/csdk/stack/src/oicgroup.c +++ b/resource/csdk/stack/src/oicgroup.c @@ -773,7 +773,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); @@ -783,11 +783,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; } -- 2.7.4