summaryrefslogtreecommitdiffstats
path: root/tools/h5repack/h5repack.c
diff options
context:
space:
mode:
authorQuincey Koziol <koziol@hdfgroup.org>2011-02-08 00:53:45 (GMT)
committerQuincey Koziol <koziol@hdfgroup.org>2011-02-08 00:53:45 (GMT)
commita6d5fa2c7db165fa2cecee86bdbd201339349968 (patch)
treeb7b11d751b526c91b0e0695d34c66991b80d74b3 /tools/h5repack/h5repack.c
parent6b1297a1ba07a06acc22f76614278000896f94dd (diff)
downloadhdf5-a6d5fa2c7db165fa2cecee86bdbd201339349968.zip
hdf5-a6d5fa2c7db165fa2cecee86bdbd201339349968.tar.gz
hdf5-a6d5fa2c7db165fa2cecee86bdbd201339349968.tar.bz2
[svn-r20061] Description:
Bring changes from Coverity branch to trunk: r19930: Fix memory leaks involving VL attributes in h5repack and h5diff. The buffers in copy_attr and diff_attr were not checked for the presence of a vlen before being freed, and vlen storage was never reclaimed. Added checks and calls to H5D_vlen_reclaim(). r19933: Purpose: Fix memory leak in H5L_move_cb() Description: H5L_move_cb copied the source link using H5O_msg_copy() but freed it manually using H5MM_xfree(). Since H5O_link_copy allocates the link using H5FL_MALLOC, this causes the link to be allocated from the free list but is never put back on the free list when it is freed. This prevents the link free list from shutting down properly. Modified H5L_move_cb() and H5L_move_dest_cb() to free the link properly using H5O_msg_free(). r19973: Fix resource leaks by freeing string created by HD5f2string r19974: Issue #345: Inialize buf variable to null Tested on: Mac OS X/32 10.6.6 (amazon) w/debug & production (h5committested on Coverity branch)
Diffstat (limited to 'tools/h5repack/h5repack.c')
-rw-r--r--tools/h5repack/h5repack.c86
1 files changed, 45 insertions, 41 deletions
diff --git a/tools/h5repack/h5repack.c b/tools/h5repack/h5repack.c
index 0f16e87..8c46638 100644
--- a/tools/h5repack/h5repack.c
+++ b/tools/h5repack/h5repack.c
@@ -407,7 +407,7 @@ int copy_attr(hid_t loc_in,
hid_t ftype_id=-1; /* file type ID */
hid_t wtype_id=-1; /* read/write type ID */
size_t msize; /* size of type */
- void *buf=NULL; /* data buffer */
+ void *buf = NULL; /* data buffer */
hsize_t nelmts; /* number of elements in dataset */
int rank; /* rank of dataset */
htri_t is_named; /* Whether the datatype is named */
@@ -424,28 +424,23 @@ int copy_attr(hid_t loc_in,
* copy all attributes
*-------------------------------------------------------------------------
*/
-
- for ( u = 0; u < (unsigned)oinfo.num_attrs; u++)
- {
- buf=NULL;
-
+ for(u = 0; u < (unsigned)oinfo.num_attrs; u++) {
/* open attribute */
if((attr_id = H5Aopen_by_idx(loc_in, ".", H5_INDEX_CRT_ORDER, H5_ITER_INC, (hsize_t)u, H5P_DEFAULT, H5P_DEFAULT)) < 0)
goto error;
/* get name */
- if (H5Aget_name( attr_id, (size_t)255, name ) < 0)
+ if(H5Aget_name(attr_id, (size_t)255, name) < 0)
goto error;
/* get the file datatype */
- if ((ftype_id = H5Aget_type( attr_id )) < 0 )
+ if((ftype_id = H5Aget_type(attr_id)) < 0 )
goto error;
/* Check if the datatype is committed */
if((is_named = H5Tcommitted(ftype_id)) < 0)
goto error;
- if(is_named && travt)
- {
+ if(is_named && travt) {
hid_t fidout;
/* Create out file id */
@@ -462,29 +457,28 @@ int copy_attr(hid_t loc_in,
if(H5Fclose(fidout) < 0)
goto error;
- }
- else
- {
- if (options->use_native==1)
+ } /* end if */
+ else {
+ if(options->use_native == 1)
wtype_id = h5tools_get_native_type(ftype_id);
else
wtype_id = H5Tcopy(ftype_id);
- } /* end if */
+ } /* end else */
/* get the dataspace handle */
- if ((space_id = H5Aget_space( attr_id )) < 0 )
+ if((space_id = H5Aget_space(attr_id)) < 0)
goto error;
/* get dimensions */
- if ( (rank = H5Sget_simple_extent_dims(space_id, dims, NULL)) < 0 )
+ if((rank = H5Sget_simple_extent_dims(space_id, dims, NULL)) < 0)
goto error;
- nelmts=1;
- for (j=0; j<rank; j++)
- nelmts*=dims[j];
+ nelmts = 1;
+ for(j = 0; j < rank; j++)
+ nelmts *= dims[j];
- if ((msize=H5Tget_size(wtype_id))==0)
+ if((msize = H5Tget_size(wtype_id)) == 0)
goto error;
/*-------------------------------------------------------------------------
@@ -495,23 +489,20 @@ int copy_attr(hid_t loc_in,
*-------------------------------------------------------------------------
*/
- if (H5T_REFERENCE==H5Tget_class(wtype_id))
- {
+ if(H5T_REFERENCE == H5Tget_class(wtype_id)) {
;
}
- else
- {
+ else {
/*-------------------------------------------------------------------------
* read to memory
*-------------------------------------------------------------------------
*/
buf = (void *)HDmalloc((size_t)(nelmts * msize));
- if(buf == NULL)
- {
+ if(buf == NULL) {
error_msg("h5repack", "cannot read into memory\n" );
goto error;
- }
+ } /* end if */
if(H5Aread(attr_id, wtype_id, buf) < 0)
goto error;
@@ -529,10 +520,12 @@ int copy_attr(hid_t loc_in,
if(H5Aclose(attr_out) < 0)
goto error;
-
- if(buf)
- free(buf);
-
+ /* Check if we have VL data in the attribute's datatype that must
+ * be reclaimed */
+ if(TRUE == H5Tdetect_class(wtype_id, H5T_VLEN))
+ H5Dvlen_reclaim(wtype_id, space_id, H5P_DEFAULT, buf);
+ HDfree(buf);
+ buf = NULL;
} /*H5T_REFERENCE*/
@@ -544,28 +537,39 @@ int copy_attr(hid_t loc_in,
*-------------------------------------------------------------------------
*/
- if (H5Tclose(ftype_id) < 0) goto error;
- if (H5Tclose(wtype_id) < 0) goto error;
- if (H5Sclose(space_id) < 0) goto error;
- if (H5Aclose(attr_id) < 0) goto error;
-
+ if(H5Tclose(ftype_id) < 0)
+ goto error;
+ if(H5Tclose(wtype_id) < 0)
+ goto error;
+ if(H5Sclose(space_id) < 0)
+ goto error;
+ if(H5Aclose(attr_id) < 0)
+ goto error;
} /* u */
-
return 0;
error:
H5E_BEGIN_TRY {
+ if(buf) {
+ /* Check if we have VL data in the attribute's datatype that must
+ * be reclaimed */
+ if(TRUE == H5Tdetect_class(wtype_id, H5T_VLEN))
+ H5Dvlen_reclaim(wtype_id, space_id, H5P_DEFAULT, buf);
+
+ /* Free buf */
+ free(buf);
+ } /* end if */
+
H5Tclose(ftype_id);
H5Tclose(wtype_id);
H5Sclose(space_id);
H5Aclose(attr_id);
H5Aclose(attr_out);
- if (buf)
- free(buf);
} H5E_END_TRY;
+
return -1;
-}
+} /* end copy_attr() */
/*-------------------------------------------------------------------------
* Function: check_options