[IOT-1675] Added Function of OICSafeFree.
authorJaehong Jo <jaehong.jo@samsung.com>
Thu, 8 Dec 2016 05:04:57 +0000 (14:04 +0900)
committerDan Mihai <Daniel.Mihai@microsoft.com>
Fri, 30 Dec 2016 18:17:16 +0000 (18:17 +0000)
Previous function just free and do not set to null.
So the caller needs to be set to null.
If use new function, do not need this operation.
So it can avoid use the dangering point.

Change-Id: I067220d9410f23d0b4ba45075f836ec528d83fbf
Signed-off-by: Jaehong Jo <jaehong.jo@samsung.com>
Reviewed-on: https://gerrit.iotivity.org/gerrit/15251
Tested-by: jenkins-iotivity <jenkins-iotivity@opendaylight.org>
Reviewed-by: Dan Mihai <Daniel.Mihai@microsoft.com>
resource/c_common/oic_malloc/include/oic_malloc.h
resource/c_common/oic_malloc/src/oic_malloc.c
resource/c_common/oic_malloc/test/linux/oic_malloc_tests.cpp

index 68588ff..f9ea210 100644 (file)
@@ -101,7 +101,16 @@ void *OICRealloc(void *ptr, size_t size);
 void *OICCalloc(size_t num, size_t size);
 
 /**
- * Deallocate a block of memory previously allocated by a call to OCMalloc
+ * Deallocate a block of memory previously allocated by a call to OICMalloc.
+ * Furthermore the pointer is set value to NULL.
+ *
+ * @param ptr - Double Pointer to block of memory previously allocated by OICMalloc.
+ *              If ptr is a null pointer, the function does nothing.
+ */
+void OICFreeAndSetToNull(void **ptr);
+
+/**
+ * Deallocate a block of memory previously allocated by a call to OICMalloc.
  *
  * NOTE: This function is intended to be used internally by the TB Stack.
  *       It is not intended to be used by applications.
index 5db7b03..be1f472 100644 (file)
@@ -126,6 +126,15 @@ void *OICRealloc(void* ptr, size_t size)
 #endif
 }
 
+void OICFreeAndSetToNull(void **ptr)
+{
+    if (*ptr)
+    {
+        OICFree(*ptr);
+        *ptr = NULL;
+    }
+}
+
 void OICFree(void *ptr)
 {
 #ifdef ENABLE_MALLOC_DEBUG
index 5a9222d..b750750 100644 (file)
@@ -141,3 +141,19 @@ TEST(OICCalloc, CallocFail4)
     EXPECT_TRUE(NULL == pBuffer);
     OICFree(pBuffer);
 }
+
+TEST(OICFreeAndSetToNull, FreeAndSetToNullPass1)
+{
+    // Try to deallocate a block of memory
+    pBuffer = (uint8_t *)OICCalloc(1, 1);
+    OICFreeAndSetToNull((void**)&pBuffer);
+    EXPECT_TRUE(NULL == pBuffer);
+}
+
+TEST(OICFreeAndSetToNull, FreeAndSetToNullPass2)
+{
+    // Try to deallocate a block of NULL
+    pBuffer = NULL;
+    OICFreeAndSetToNull((void**)&pBuffer);
+    EXPECT_TRUE(NULL == pBuffer);
+}