From d4f573ea1f2a0089fb7d53980ac39c39673391cb Mon Sep 17 00:00:00 2001 From: Jaehong Jo Date: Thu, 8 Dec 2016 14:04:57 +0900 Subject: [PATCH] [IOT-1675] Added Function of OICSafeFree. 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 Reviewed-on: https://gerrit.iotivity.org/gerrit/15251 Tested-by: jenkins-iotivity Reviewed-by: Dan Mihai --- resource/c_common/oic_malloc/include/oic_malloc.h | 11 ++++++++++- resource/c_common/oic_malloc/src/oic_malloc.c | 9 +++++++++ .../c_common/oic_malloc/test/linux/oic_malloc_tests.cpp | 16 ++++++++++++++++ 3 files changed, 35 insertions(+), 1 deletion(-) diff --git a/resource/c_common/oic_malloc/include/oic_malloc.h b/resource/c_common/oic_malloc/include/oic_malloc.h index 68588ff..f9ea210 100644 --- a/resource/c_common/oic_malloc/include/oic_malloc.h +++ b/resource/c_common/oic_malloc/include/oic_malloc.h @@ -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. diff --git a/resource/c_common/oic_malloc/src/oic_malloc.c b/resource/c_common/oic_malloc/src/oic_malloc.c index 5db7b03..be1f472 100644 --- a/resource/c_common/oic_malloc/src/oic_malloc.c +++ b/resource/c_common/oic_malloc/src/oic_malloc.c @@ -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 diff --git a/resource/c_common/oic_malloc/test/linux/oic_malloc_tests.cpp b/resource/c_common/oic_malloc/test/linux/oic_malloc_tests.cpp index 5a9222d..b750750 100644 --- a/resource/c_common/oic_malloc/test/linux/oic_malloc_tests.cpp +++ b/resource/c_common/oic_malloc/test/linux/oic_malloc_tests.cpp @@ -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); +} -- 2.7.4