From 438248d01de7c779e6b7f3b9a75c3f706bbe14f4 Mon Sep 17 00:00:00 2001 From: Quincey Koziol Date: Tue, 26 Aug 2003 13:35:37 -0500 Subject: [svn-r7412] Purpose: Bug fix Description: H5Gget_objname_by_idx should allow NULL for the 'name' parameter, to allow for querying for the name's length. Solution: Allow NULL for the 'name' parameter, the internal functions were already ready to handle that case. Clean up RM information about H5Gget_objname_by_idx and H5Gget_objtype_by_idx also. Platforms tested: FreeBSD 4.8 (sleipnir) h5committest --- doc/html/RM_H5G.html | 28 +++++++++++++------------- release_docs/RELEASE.txt | 3 +++ src/H5G.c | 11 ++++++----- test/titerate.c | 51 +++++++++++++++++++++++++++--------------------- 4 files changed, 52 insertions(+), 41 deletions(-) diff --git a/doc/html/RM_H5G.html b/doc/html/RM_H5G.html index ee7b596..34ae2bf 100644 --- a/doc/html/RM_H5G.html +++ b/doc/html/RM_H5G.html @@ -857,7 +857,7 @@ create or access function.
Name: H5Gget_objname_by_idx
Signature: -
ssize_t H5Gget_objname_by_idx(hid_t group_id, +
ssize_t H5Gget_objname_by_idx(hid_t loc_id, hsize_t idx, char *name, size_t* size ) @@ -865,10 +865,10 @@ create or access function.
Returns a name of an object specified by an index.
Description:
H5Gget_objname_by_idx returns a name of the object - specified by the index idx in the group group_id. + specified by the index idx in the group loc_id.

- The group is specified by a group identifier group_id. - If preferred, a file identifier may be passed in group_id; + The group is specified by a group identifier loc_id. + If preferred, a file identifier may be passed in loc_id; that file's root group will be assumed.

idx is the transient index used to iterate through @@ -886,12 +886,13 @@ create or access function. the object name is truncated to max_size - 1 characters.

Note that if the size of the object's name is unkown, a - preliminary call to H5Gget_objname_by_idx will return - that value. A second call to H5Gget_objname_by_idx + preliminary call to H5Gget_objname_by_idx with name + set to NULL will return the length of the object's name. + A second call to H5Gget_objname_by_idx can then be used to retrieve the actual name.

Parameters:
-
hid_t group_id +
hid_t loc_id
IN: Group or file identifier.
hsize_t idx
IN: Transient index identifying object. @@ -902,8 +903,7 @@ create or access function.
Returns:
Returns the size of the object name if successful, - or 0 if no name is associated with the group identifier. - Otherwise returns a negative value. + otherwise returns a negative value.
@@ -911,16 +911,16 @@ create or access function.
Name: H5Gget_objtype_by_idx
Signature: -
int H5Gget_objtype_by_idx(hid_t group_id, +
int H5Gget_objtype_by_idx(hid_t loc_id, hsize_t idx )
Purpose:
Returns the type of an object specified by an index.
Description:
H5Gget_objtype_by_idx returns the type of the object - specified by the index idx in the group group_id. + specified by the index idx in the group loc_id.

- The group is specified by a group identifier group_id. - If preferred, a file identifier may be passed in group_id; + The group is specified by a group identifier loc_id. + If preferred, a file identifier may be passed in loc_id; that file's root group will be assumed.

idx is the transient index used to iterate through @@ -978,7 +978,7 @@ create or access function.

Parameters:
-
hid_t group_id +
hid_t loc_id
IN: Group or file identifier.
hsize_t idx
IN: Transient index identifying object. diff --git a/release_docs/RELEASE.txt b/release_docs/RELEASE.txt index 2d9b586..524184c 100644 --- a/release_docs/RELEASE.txt +++ b/release_docs/RELEASE.txt @@ -74,6 +74,9 @@ Bug Fixes since HDF5-1.6.0 release Library ------- + - Corrected bug in H5Gget_objname_by_idx which was not allowing NULL + for the name when just querying for the object name's length. + QAK - 2003/08/25 - Corrected bug in variable-length string handling which could generate a core dump on writing variable-length strings as part of a compound datatype on certain architectures. QAK - 2003/08/25 diff --git a/src/H5G.c b/src/H5G.c index 896b9bc..cf38421 100644 --- a/src/H5G.c +++ b/src/H5G.c @@ -520,8 +520,6 @@ H5Gget_objname_by_idx(hid_t loc_id, hsize_t idx, char *name, size_t size) HGOTO_ERROR (H5E_ARGS, H5E_BADTYPE, FAIL, "not a location ID"); if(H5G_get_type(loc,H5AC_ind_dxpl_id)!=H5G_GROUP) HGOTO_ERROR (H5E_ARGS, H5E_BADTYPE, FAIL, "not a group"); - if (!name) - HGOTO_ERROR(H5E_ARGS, H5E_BADTYPE, FAIL, "nil pointer for name"); if (H5G_get_num_objs(loc, &num_objs, H5AC_ind_dxpl_id)<0) HGOTO_ERROR(H5E_ARGS, H5E_BADTYPE, FAIL, "unable to retrieve number of members"); @@ -2634,12 +2632,15 @@ H5G_get_objname_by_idx(H5G_entry_t *loc, hsize_t idx, char* name, size_t size, h H5G_node_name, loc->cache.stab.btree_addr, &udata))<0) HGOTO_ERROR(H5E_ARGS, H5E_BADTYPE, FAIL, "iteration operator failed"); + /* Get the length of the name */ ret_value = (ssize_t)HDstrlen(udata.name); - if(name && size>0) { - HDstrncpy(name, udata.name, MIN((size_t)(ret_value+1),size-1)); + + /* Copy the name into the user's buffer, if given */ + if(name) { + HDstrncpy(name, udata.name, MIN((size_t)(ret_value+1),size)); if((size_t)ret_value >= size) name[size-1]='\0'; - } + } /* end if */ done: /* Free the duplicated name */ diff --git a/test/titerate.c b/test/titerate.c index ee926ea..1624196 100644 --- a/test/titerate.c +++ b/test/titerate.c @@ -118,7 +118,7 @@ static void test_iter_group(void) int idx; /* Index in the group */ char name[NAMELEN]; /* temporary name buffer */ char *dnames[NDATASETS];/* Names of the datasets created */ - char dataset_name[16]; /* dataset name */ + char dataset_name[NAMELEN]; /* dataset name */ iter_info info; /* Custom iteration information */ hsize_t num_membs; /* Number of group members */ herr_t ret; /* Generic return value */ @@ -199,17 +199,17 @@ static void test_iter_group(void) for(i=0; i< (int)num_membs; i++) { H5G_obj_t obj_type; /* Type of object in file */ - ret = H5Gget_objname_by_idx(root_group, (hsize_t)i, dataset_name, 32); - CHECK(ret, FAIL, "H5Gget_objsname_by_idx"); + ret = H5Gget_objname_by_idx(root_group, (hsize_t)i, dataset_name, NAMELEN); + CHECK(ret, FAIL, "H5Gget_objname_by_idx"); obj_type = H5Gget_objtype_by_idx(root_group, (hsize_t)i); - CHECK(obj_type, H5G_UNKNOWN, "H5Gget_objsname_by_idx"); + CHECK(obj_type, H5G_UNKNOWN, "H5Gget_objtype_by_idx"); } H5E_BEGIN_TRY { - ret = (herr_t)H5Gget_objname_by_idx(root_group, (hsize_t)(NDATASETS+3), dataset_name, 16); + ret = (herr_t)H5Gget_objname_by_idx(root_group, (hsize_t)(NDATASETS+3), dataset_name, NAMELEN); } H5E_END_TRY; - VERIFY(ret, FAIL, "H5Gget_objsname_by_idx"); + VERIFY(ret, FAIL, "H5Gget_objname_by_idx"); ret = H5Gclose(root_group); CHECK(ret, FAIL, "H5Gclose"); @@ -227,17 +227,17 @@ static void test_iter_group(void) for(i=0; i< (int)num_membs; i++) { H5G_obj_t obj_type; /* Type of object in file */ - ret = H5Gget_objname_by_idx(file, (hsize_t)i, dataset_name, 32); - CHECK(ret, FAIL, "H5Gget_objsname_by_idx"); + ret = H5Gget_objname_by_idx(file, (hsize_t)i, dataset_name, NAMELEN); + CHECK(ret, FAIL, "H5Gget_objname_by_idx"); obj_type = H5Gget_objtype_by_idx(file, (hsize_t)i); - CHECK(obj_type, H5G_UNKNOWN, "H5Gget_objsname_by_idx"); + CHECK(obj_type, H5G_UNKNOWN, "H5Gget_objtype_by_idx"); } H5E_BEGIN_TRY { - ret = (herr_t)H5Gget_objname_by_idx(file, (hsize_t)(NDATASETS+3), dataset_name, 16); + ret = (herr_t)H5Gget_objname_by_idx(file, (hsize_t)(NDATASETS+3), dataset_name, NAMELEN); } H5E_END_TRY; - VERIFY(ret, FAIL, "H5Gget_objsname_by_idx"); + VERIFY(ret, FAIL, "H5Gget_objname_by_idx"); } /* Test invalid indices for starting iteration */ @@ -730,7 +730,8 @@ static void test_grp_memb_funcs(void) char name[NAMELEN]; /* temporary name buffer */ char *dnames[NDATASETS+2];/* Names of the datasets created */ char *obj_names[NDATASETS+2];/* Names of the objects in group */ - char dataset_name[16]; /* dataset name */ + char dataset_name[NAMELEN]; /* dataset name */ + ssize_t name_len; /* Length of object's name */ hsize_t num_membs; /* Number of group members */ herr_t ret; /* Generic return value */ @@ -791,7 +792,6 @@ static void test_grp_memb_funcs(void) /* Sort the dataset names */ qsort(dnames,NDATASETS+2,sizeof(char *),iter_strcmp); - /* Iterate through the datasets in the root group in various ways */ file=H5Fopen(DATAFILE, H5F_ACC_RDONLY, H5P_DEFAULT); CHECK(file, FAIL, "H5Fopen"); @@ -809,28 +809,35 @@ static void test_grp_memb_funcs(void) for(i=0; i< (int)num_membs; i++) { H5G_obj_t obj_type; /* Type of object in file */ - ret = H5Gget_objname_by_idx(root_group, (hsize_t)i, dataset_name, 32); - CHECK(ret, FAIL, "H5Gget_objsname_by_idx"); + /* Test with NULL for name, to query length */ + name_len = H5Gget_objname_by_idx(root_group, (hsize_t)i, NULL, NAMELEN); + CHECK(name_len, FAIL, "H5Gget_objname_by_idx"); + + ret = H5Gget_objname_by_idx(root_group, (hsize_t)i, dataset_name, NAMELEN); + CHECK(ret, FAIL, "H5Gget_objname_by_idx"); + + /* Double-check that the length is the same */ + VERIFY(ret, name_len, "H5Gget_objname_by_idx"); /* Keep a copy of the dataset names around for later */ obj_names[i]=HDstrdup(dataset_name); - CHECK(dnames[i], NULL, "strdup"); + CHECK(obj_names[i], NULL, "strdup"); obj_type = H5Gget_objtype_by_idx(root_group, (hsize_t)i); - CHECK(obj_type, H5G_UNKNOWN, "H5Gget_objsname_by_idx"); + CHECK(obj_type, H5G_UNKNOWN, "H5Gget_objtype_by_idx"); if(!HDstrcmp(dataset_name, "grp")) - VERIFY(obj_type, H5G_GROUP, "H5Gget_objsname_by_idx"); + VERIFY(obj_type, H5G_GROUP, "H5Gget_objname_by_idx"); if(!HDstrcmp(dataset_name, "dtype")) - VERIFY(obj_type, H5G_TYPE, "H5Gget_objsname_by_idx"); + VERIFY(obj_type, H5G_TYPE, "H5Gget_objname_by_idx"); if(!HDstrncmp(dataset_name, "Dataset", 7)) - VERIFY(obj_type, H5G_DATASET, "H5Gget_objsname_by_idx"); + VERIFY(obj_type, H5G_DATASET, "H5Gget_objname_by_idx"); } H5E_BEGIN_TRY { - ret = (herr_t)H5Gget_objname_by_idx(root_group, (hsize_t)(NDATASETS+3), dataset_name, 16); + ret = (herr_t)H5Gget_objname_by_idx(root_group, (hsize_t)(NDATASETS+3), dataset_name, NAMELEN); } H5E_END_TRY; - VERIFY(ret, FAIL, "H5Gget_objsname_by_idx"); + VERIFY(ret, FAIL, "H5Gget_objname_by_idx"); /* Sort the dataset names */ qsort(obj_names,NDATASETS+2,sizeof(char *),iter_strcmp); -- cgit v0.12