From 3e4f6138b60d8363c377e453d63f875775ec907a Mon Sep 17 00:00:00 2001 From: Dana Robinson Date: Thu, 9 Nov 2017 13:56:58 -0800 Subject: Removed asserts from H5MM calls that conflict with stated behavior and added a test to ensure that this doesn't happen in the future. --- src/H5MM.c | 8 +------- test/tmisc.c | 62 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++- 2 files changed, 62 insertions(+), 8 deletions(-) diff --git a/src/H5MM.c b/src/H5MM.c index ee3b28f..866dfbe 100644 --- a/src/H5MM.c +++ b/src/H5MM.c @@ -268,8 +268,6 @@ H5MM_malloc(size_t size) { void *ret_value = NULL; - HDassert(size); - /* Use FUNC_ENTER_NOAPI_NOINIT_NOERR here to avoid performance issues */ FUNC_ENTER_NOAPI_NOINIT_NOERR @@ -357,8 +355,6 @@ H5MM_calloc(size_t size) { void *ret_value = NULL; - HDassert(size); - /* Use FUNC_ENTER_NOAPI_NOINIT_NOERR here to avoid performance issues */ FUNC_ENTER_NOAPI_NOINIT_NOERR @@ -407,8 +403,6 @@ H5MM_realloc(void *mem, size_t size) /* Use FUNC_ENTER_NOAPI_NOINIT_NOERR here to avoid performance issues */ FUNC_ENTER_NOAPI_NOINIT_NOERR - HDassert(mem || size); - if(NULL == mem && 0 == size) /* Not defined in the standard, return NULL */ ret_value = NULL; @@ -454,7 +448,7 @@ H5MM_realloc(void *mem, size_t size) * NULL is an acceptable value for the input string. * * Return: Success: Pointer to a new string (NULL if s is NULL). - * Failure: abort() + * Failure: NULL * * Programmer: Robb Matzke * Jul 10 1997 diff --git a/test/tmisc.c b/test/tmisc.c index 102325a..6da4589 100644 --- a/test/tmisc.c +++ b/test/tmisc.c @@ -30,6 +30,7 @@ #include "testhdf5.h" #include "H5srcdir.h" #include "H5Dpkg.h" /* Datasets */ +#include "H5MMprivate.h" /* Memory */ /* Definitions for misc. test #1 */ #define MISC1_FILE "tmisc1.h5" @@ -5526,6 +5527,64 @@ test_misc33(void) } /* end test_misc33() */ +/**************************************************************** +** +** test_misc34(): Ensure zero-size memory allocations work +** +****************************************************************/ +static void +test_misc34(void) +{ + void *mem = NULL; /* allocated buffer */ + char *dup = NULL; /* 'duplicated' string */ + size_t sz = 0; /* buffer size */ + + /* Output message about test being performed */ + MESSAGE(5, ("Testing O and NULL behavior in H5MM API calls")); + + /* H5MM_xfree(): Ensure that passing NULL is allowed and returns NULL */ + mem = H5MM_xfree(mem); + CHECK_PTR_NULL(mem, "H5MM_xfree"); + + /* H5MM_malloc(): Ensure that size 0 returns NULL */ + mem = H5MM_malloc(sz); + CHECK_PTR_NULL(mem, "H5MM_malloc"); + mem = H5MM_xfree(mem); + + /* H5MM_calloc(): Ensure that size 0 returns NULL */ + mem = H5MM_calloc(sz); + CHECK_PTR_NULL(mem, "H5MM_calloc"); + mem = H5MM_xfree(mem); + + /* H5MM_realloc(): Check behavior: + * + * H5MM_realloc(NULL, size) <==> H5MM_malloc(size) + * H5MM_realloc(ptr, 0) <==> H5MM_xfree(ptr) + * H5MM_realloc(NULL, 0) <==> NULL + */ + mem = H5MM_xfree(mem); + + sz = 1024; + mem = H5MM_realloc(mem, sz); + CHECK_PTR(mem, "H5MM_realloc (case 1)"); + /* Don't free mem here! */ + + sz = 0; + mem = H5MM_realloc(mem, sz); + CHECK_PTR_NULL(mem, "H5MM_realloc (case 2)"); + mem = H5MM_xfree(mem); + + mem = H5MM_realloc(mem, sz); + CHECK_PTR_NULL(mem, "H5MM_realloc (case 3)"); + mem = H5MM_xfree(mem); + + /* H5MM_xstrdup(): Ensure NULL returns NULL */ + dup = H5MM_xstrdup((const char *)mem); + CHECK_PTR_NULL(dup, "H5MM_xstrdup"); + dup = (char *)H5MM_xfree((void *)dup); + +} /* end test_misc34() */ + /**************************************************************** ** @@ -5575,6 +5634,7 @@ test_misc(void) test_misc31(); /* Test Reentering library through deprecated routines after H5close() */ test_misc32(); /* Test filter memory allocation functions */ test_misc33(); /* Test to verify that H5HL_offset_into() returns error if offset exceeds heap block */ + test_misc34(); /* Test behavior of 0 and NULL in H5MM API calls */ } /* test_misc() */ @@ -5632,5 +5692,5 @@ cleanup_misc(void) HDremove(MISC29_COPY_FILE); HDremove(MISC30_FILE); HDremove(MISC31_FILE); -} +} /* end cleanup_misc() */ -- cgit v0.12