summaryrefslogtreecommitdiffstats
path: root/tools/lib/h5trav.c
diff options
context:
space:
mode:
authorQuincey Koziol <koziol@hdfgroup.org>2014-03-16 04:44:13 (GMT)
committerQuincey Koziol <koziol@hdfgroup.org>2014-03-16 04:44:13 (GMT)
commit164b4c75341ee4aa31dcd47c189e415a36ae35e2 (patch)
tree121120c9d2df2d601730e4c371afbf5f82004f92 /tools/lib/h5trav.c
parent288c7760a4a55d56df7a9019a8522625e305b9b7 (diff)
downloadhdf5-164b4c75341ee4aa31dcd47c189e415a36ae35e2.zip
hdf5-164b4c75341ee4aa31dcd47c189e415a36ae35e2.tar.gz
hdf5-164b4c75341ee4aa31dcd47c189e415a36ae35e2.tar.bz2
[svn-r24804] Description:
Brought changes from Coverity branch back to trunk, and cleaned up misc. other warnings & formatting issues: r20833: Fixed Coverity 667 and 668 with real integer overflow tests this time. r20834: Use HDstrncpy and HDstrncat. --gh r20835: Change to use strncpy - use base_len + 1 for line 156, use HDstrlen(path) + 1 for line 159 r20836: Fixed coverity 585 by casting output of fgetc() to a char. r20837: Changed sprintf calls to snprintf with size 1 less than the allocated buffer to address coverity issue #967. Tested on: Mac OSX/64 10.9.2 (amazon) w/C++, FORTRAN & parallel (too minor to require h5committest)
Diffstat (limited to 'tools/lib/h5trav.c')
-rw-r--r--tools/lib/h5trav.c102
1 files changed, 40 insertions, 62 deletions
diff --git a/tools/lib/h5trav.c b/tools/lib/h5trav.c
index fca3096..d0a4a76 100644
--- a/tools/lib/h5trav.c
+++ b/tools/lib/h5trav.c
@@ -21,13 +21,15 @@
* local typedefs
*-------------------------------------------------------------------------
*/
+typedef struct trav_addr_path_t {
+ haddr_t addr;
+ char *path;
+} trav_addr_path_t;
+
typedef struct trav_addr_t {
size_t nalloc;
size_t nused;
- struct {
- haddr_t addr;
- char *path;
- } *objs;
+ trav_addr_path_t *objs;
} trav_addr_t;
typedef struct {
@@ -132,7 +134,7 @@ trav_addr_add(trav_addr_t *visited, haddr_t addr, const char *path)
/* Allocate space if necessary */
if(visited->nused == visited->nalloc) {
visited->nalloc = MAX(1, visited->nalloc * 2);;
- visited->objs = HDrealloc(visited->objs, visited->nalloc * sizeof(visited->objs[0]));
+ visited->objs = (trav_addr_path_t *)HDrealloc(visited->objs, visited->nalloc * sizeof(trav_addr_path_t));
} /* end if */
/* Append it */
@@ -194,14 +196,15 @@ traverse_cb(hid_t loc_id, const char *path, const H5L_info_t *linfo,
/* Create the full path name for the link */
if(udata->is_absolute) {
size_t base_len = HDstrlen(udata->base_grp_name);
- size_t add_slash = base_len ? ((udata->base_grp_name)[base_len-1] != '/') : 1;
+ size_t add_slash = base_len ? ((udata->base_grp_name)[base_len - 1] != '/') : 1;
+ size_t new_name_len = base_len + add_slash + HDstrlen(path) + 1;
- if(NULL == (new_name = (char*)HDmalloc(base_len + add_slash + HDstrlen(path) + 1)))
+ if(NULL == (new_name = (char*)HDmalloc(new_name_len)))
return(H5_ITER_ERROR);
- HDstrcpy(new_name, udata->base_grp_name);
- if (add_slash)
- new_name[base_len] = '/';
- HDstrcpy(new_name + base_len + add_slash, path);
+ if(add_slash)
+ HDsnprintf(new_name, new_name_len, "%s/%s", udata->base_grp_name, path);
+ else
+ HDsnprintf(new_name, new_name_len, "%s%s", udata->base_grp_name, path);
full_name = new_name;
} /* end if */
else
@@ -216,7 +219,7 @@ traverse_cb(hid_t loc_id, const char *path, const H5L_info_t *linfo,
if(new_name)
HDfree(new_name);
return(H5_ITER_ERROR);
- }
+ } /* end if */
/* If the object has multiple links, add it to the list of addresses
* already visited, if it isn't there already
@@ -231,7 +234,7 @@ traverse_cb(hid_t loc_id, const char *path, const H5L_info_t *linfo,
if(new_name)
HDfree(new_name);
return(H5_ITER_ERROR);
- }
+ } /* end if */
} /* end if */
else {
/* Make 'visit link' callback */
@@ -240,7 +243,7 @@ traverse_cb(hid_t loc_id, const char *path, const H5L_info_t *linfo,
if(new_name)
HDfree(new_name);
return(H5_ITER_ERROR);
- }
+ } /* end if */
} /* end else */
if(new_name)
@@ -730,7 +733,7 @@ trav_table_add(trav_table_t *table,
if(table->nobjs == table->size) {
table->size = MAX(1, table->size * 2);
- table->objs = (trav_obj_t*)HDrealloc(table->objs, table->size * sizeof(trav_obj_t));
+ table->objs = (trav_obj_t *)HDrealloc(table->objs, table->size * sizeof(trav_obj_t));
} /* end if */
new_obj = table->nobjs++;
@@ -774,7 +777,7 @@ trav_table_addlink(trav_table_t *table, haddr_t objno, const char *path)
/* allocate space if necessary */
if(table->objs[i].nlinks == (unsigned)table->objs[i].sizelinks) {
table->objs[i].sizelinks = MAX(1, table->objs[i].sizelinks * 2);
- table->objs[i].links = (trav_link_t*)HDrealloc(table->objs[i].links, table->objs[i].sizelinks * sizeof(trav_link_t));
+ table->objs[i].links = (trav_link_t *)HDrealloc(table->objs[i].links, table->objs[i].sizelinks * sizeof(trav_link_t));
} /* end if */
/* insert it */
@@ -1168,31 +1171,21 @@ h5trav_visit(hid_t fid, const char *grp_name, hbool_t visit_start,
*
* Date: September 5, 2008
*
- * Modified:
- * Jonathan Kim
- * - Moved from h5ls.c to share among tools. (Sep 16, 2010)
- * - Renamed from elink_trav_add to symlink_visit_add for both soft and
- * external links. (May 25, 2010)
- * - Add type parameter to distingush between soft and external link for
- * sure, which prevent from mixing up visited link when the target names
- * are same between the soft and external link, as code marks with the
- * target name. (May 25,2010)
- *
*-------------------------------------------------------------------------
*/
herr_t
symlink_visit_add(symlink_trav_t *visited, H5L_type_t type, const char *file, const char *path)
{
size_t idx; /* Index of address to use */
- void *tmp_ptr;
/* Allocate space if necessary */
- if(visited->nused == visited->nalloc)
- {
+ if(visited->nused == visited->nalloc) {
+ void *tmp_ptr;
+
visited->nalloc = MAX(1, visited->nalloc * 2);
- if(NULL == (tmp_ptr = HDrealloc(visited->objs, visited->nalloc * sizeof(visited->objs[0]))))
+ if(NULL == (tmp_ptr = HDrealloc(visited->objs, visited->nalloc * sizeof(symlink_trav_path_t))))
return -1;
- visited->objs = tmp_ptr;
+ visited->objs = (symlink_trav_path_t *)tmp_ptr;
} /* end if */
/* Append it */
@@ -1202,22 +1195,19 @@ symlink_visit_add(symlink_trav_t *visited, H5L_type_t type, const char *file, co
visited->objs[idx].file = NULL;
visited->objs[idx].path = NULL;
- if (type == H5L_TYPE_EXTERNAL)
- {
- if(NULL == (visited->objs[idx].file = HDstrdup(file)))
- {
+ if(type == H5L_TYPE_EXTERNAL) {
+ if(NULL == (visited->objs[idx].file = HDstrdup(file))) {
visited->nused--;
return -1;
- }
- }
+ } /* end if */
+ } /* end if */
- if(NULL == (visited->objs[idx].path = HDstrdup(path)))
- {
+ if(NULL == (visited->objs[idx].path = HDstrdup(path))) {
visited->nused--;
- if (visited->objs[idx].file)
+ if(visited->objs[idx].file)
HDfree (visited->objs[idx].file);
return -1;
- }
+ } /* end if */
return 0;
} /* end symlink_visit_add() */
@@ -1235,16 +1225,6 @@ symlink_visit_add(symlink_trav_t *visited, H5L_type_t type, const char *file, co
*
* Date: September 5, 2008
*
- * Modified:
- * Jonathan Kim
- * - Moved from h5ls.c to share among tools. (Sep 16, 2010)
- * - Renamed from elink_trav_visited to symlink_is_visited for both soft and
- * external links. (May 25, 2010)
- * - Add type parameter to distingush between soft and external link for
- * sure, which prevent from mixing up visited link when the target names
- * are same between the soft and external link, as code marks with the
- * target name. (May 25,2010)
- *
*-------------------------------------------------------------------------
*/
hbool_t
@@ -1253,21 +1233,19 @@ symlink_is_visited(symlink_trav_t *visited, H5L_type_t type, const char *file, c
size_t u; /* Local index variable */
/* Look for symlink */
- for(u = 0; u < visited->nused; u++)
- {
+ for(u = 0; u < visited->nused; u++) {
/* Check for symlink values already in array */
/* check type and path pair to distingush between symbolic links */
- if ((visited->objs[u].type == type) && !HDstrcmp(visited->objs[u].path, path))
- {
+ if((visited->objs[u].type == type) && !HDstrcmp(visited->objs[u].path, path)) {
/* if external link, file need to be matched as well */
- if (visited->objs[u].type == H5L_TYPE_EXTERNAL)
- {
- if (!HDstrcmp(visited->objs[u].file, file))
- return (TRUE);
- }
+ if(visited->objs[u].type == H5L_TYPE_EXTERNAL)
+ if(!HDstrcmp(visited->objs[u].file, file))
+ return(TRUE);
+
return (TRUE);
- }
- }
+ } /* end if */
+ } /* end for */
+
/* Didn't find symlink */
return(FALSE);
} /* end symlink_is_visited() */