summaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorQuincey Koziol <koziol@hdfgroup.org>2010-01-08 19:39:09 (GMT)
committerQuincey Koziol <koziol@hdfgroup.org>2010-01-08 19:39:09 (GMT)
commit1b6cd030d3b48c3a23b106a9dc8f6086c1b4d220 (patch)
treedc5dcb0366856defb234a94bc5caa8827e4dc434 /src
parent73508da070ec8a922605a964c572c60b9a5fa96b (diff)
downloadhdf5-1b6cd030d3b48c3a23b106a9dc8f6086c1b4d220.zip
hdf5-1b6cd030d3b48c3a23b106a9dc8f6086c1b4d220.tar.gz
hdf5-1b6cd030d3b48c3a23b106a9dc8f6086c1b4d220.tar.bz2
[svn-r18087] Description:
Correct Coverity issue #7 by cleaning up correctly on error Tested on: Mac OS X/32 10.6.3 (amazon) w/debug
Diffstat (limited to 'src')
-rw-r--r--src/H5HG.c51
1 files changed, 28 insertions, 23 deletions
diff --git a/src/H5HG.c b/src/H5HG.c
index ace07ce..48da6c6 100644
--- a/src/H5HG.c
+++ b/src/H5HG.c
@@ -161,28 +161,25 @@ static haddr_t
H5HG_create (H5F_t *f, hid_t dxpl_id, size_t size)
{
H5HG_heap_t *heap = NULL;
- haddr_t ret_value = HADDR_UNDEF;
uint8_t *p = NULL;
haddr_t addr;
size_t n;
+ haddr_t ret_value = HADDR_UNDEF;
FUNC_ENTER_NOAPI_NOINIT(H5HG_create)
/* Check args */
- assert (f);
- if (size<H5HG_MINSIZE)
+ HDassert(f);
+ if(size < H5HG_MINSIZE)
size = H5HG_MINSIZE;
size = H5HG_ALIGN(size);
/* Create it */
- H5_CHECK_OVERFLOW(size,size_t,hsize_t);
- if ( HADDR_UNDEF==
- (addr=H5MF_alloc(f, H5FD_MEM_GHEAP, dxpl_id, (hsize_t)size)))
- HGOTO_ERROR (H5E_HEAP, H5E_CANTINIT, HADDR_UNDEF, \
- "unable to allocate file space for global heap");
- if (NULL==(heap = H5FL_MALLOC (H5HG_heap_t)))
- HGOTO_ERROR (H5E_RESOURCE, H5E_NOSPACE, HADDR_UNDEF, \
- "memory allocation failed");
+ H5_CHECK_OVERFLOW(size, size_t, hsize_t);
+ if(HADDR_UNDEF == (addr = H5MF_alloc(f, H5FD_MEM_GHEAP, dxpl_id, (hsize_t)size)))
+ HGOTO_ERROR(H5E_HEAP, H5E_CANTINIT, HADDR_UNDEF, "unable to allocate file space for global heap")
+ if(NULL == (heap = H5FL_MALLOC(H5HG_heap_t)))
+ HGOTO_ERROR(H5E_RESOURCE, H5E_NOSPACE, HADDR_UNDEF, "memory allocation failed")
heap->addr = addr;
heap->size = size;
@@ -203,14 +200,14 @@ HDmemset(heap->chunk, 0, size);
*p++ = 0; /*reserved*/
*p++ = 0; /*reserved*/
*p++ = 0; /*reserved*/
- H5F_ENCODE_LENGTH (f, p, size);
+ H5F_ENCODE_LENGTH(f, p, size);
/*
* Padding so free space object is aligned. If malloc returned memory
* which was always at least H5HG_ALIGNMENT aligned then we could just
* align the pointer, but this might not be the case.
*/
- n = H5HG_ALIGN(p-heap->chunk) - (p-heap->chunk);
+ n = H5HG_ALIGN(p - heap->chunk) - (p - heap->chunk);
#ifdef OLD_WAY
/* Don't bother zeroing out the rest of the info in the heap -QAK */
HDmemset(p, 0, n);
@@ -225,7 +222,7 @@ HDmemset(heap->chunk, 0, size);
UINT16ENCODE(p, 0); /*object ID*/
UINT16ENCODE(p, 0); /*reference count*/
UINT32ENCODE(p, 0); /*reserved*/
- H5F_ENCODE_LENGTH (f, p, heap->obj[0].size);
+ H5F_ENCODE_LENGTH(f, p, heap->obj[0].size);
#ifdef OLD_WAY
/* Don't bother zeroing out the rest of the info in the heap -QAK */
HDmemset (p, 0, (size_t)((heap->chunk+heap->size) - p));
@@ -244,21 +241,29 @@ HDmemset(heap->chunk, 0, size);
MIN(f->shared->ncwfs, H5HG_NCWFS - 1) * sizeof(H5HG_heap_t *));
f->shared->cwfs[0] = heap;
f->shared->ncwfs = MIN(H5HG_NCWFS, f->shared->ncwfs+1);
- }
+ } /* end else */
/* Add the heap to the cache */
- if (H5AC_set (f, dxpl_id, H5AC_GHEAP, addr, heap, H5AC__NO_FLAGS_SET)<0)
- HGOTO_ERROR (H5E_HEAP, H5E_CANTINIT, HADDR_UNDEF, \
- "unable to cache global heap collection");
+ if(H5AC_set(f, dxpl_id, H5AC_GHEAP, addr, heap, H5AC__NO_FLAGS_SET) < 0)
+ HGOTO_ERROR(H5E_HEAP, H5E_CANTINIT, HADDR_UNDEF, "unable to cache global heap collection")
ret_value = addr;
done:
- if ( ! ( H5F_addr_defined(addr) ) && heap) {
- if ( H5HG_dest(f,heap) < 0 )
- HDONE_ERROR(H5E_HEAP, H5E_CANTFREE, HADDR_UNDEF, \
- "unable to destroy global heap collection");
- }
+ /* Cleanup on error */
+ if(HADDR_UNDEF == ret_value) {
+ if(H5F_addr_defined(addr)) {
+ /* Release the space on disk */
+ if(H5MF_xfree(f, H5FD_MEM_GHEAP, dxpl_id, addr, (hsize_t)size) < 0)
+ HDONE_ERROR(H5E_BTREE, H5E_CANTFREE, HADDR_UNDEF, "unable to free global heap")
+
+ /* Check if the heap object was allocated */
+ if(heap)
+ /* Destroy the heap object */
+ if(H5HG_dest(f, heap) < 0)
+ HDONE_ERROR(H5E_HEAP, H5E_CANTFREE, HADDR_UNDEF, "unable to destroy global heap collection")
+ } /* end if */
+ } /* end if */
FUNC_LEAVE_NOAPI(ret_value);
} /* H5HG_create() */