From 22be11f0d9cd3e0e366b2b4dfe14933fdcf3687e Mon Sep 17 00:00:00 2001 From: Quincey Koziol Date: Tue, 2 Oct 2007 15:00:21 -0500 Subject: [svn-r14175] Description: Minor fixes to avoid memory leaks when 'realloc' fails. Tested on: Linux/32 2.6 (chicago) Linux/64 2.6 (chicago2) --- src/H5Distore.c | 8 ++++++-- src/H5Gname.c | 10 ++++++++-- src/H5Gtraverse.c | 14 ++++++++------ src/H5HFsection.c | 10 ++++++++-- src/H5L.c | 2 +- 5 files changed, 31 insertions(+), 13 deletions(-) diff --git a/src/H5Distore.c b/src/H5Distore.c index 687e6d6..66fc41a 100644 --- a/src/H5Distore.c +++ b/src/H5Distore.c @@ -1015,12 +1015,16 @@ H5D_istore_iter_copy(H5F_t *f_src, hid_t dxpl_id, const void *_lt_key, /* Resize the buf if it is too small to hold the data */ if(nbytes > buf_size) { + void *new_buf; /* New buffer for data */ + /* Re-allocate memory for copying the chunk */ - if(NULL == (udata->buf = H5MM_realloc(udata->buf, nbytes))) + if(NULL == (new_buf = H5MM_realloc(udata->buf, nbytes))) HGOTO_ERROR(H5E_RESOURCE, H5E_NOSPACE, H5_ITER_ERROR, "memory allocation failed for raw data chunk") + udata->buf = new_buf; if(udata->bkg) { - if(NULL == (udata->bkg = H5MM_realloc(udata->bkg, nbytes))) + if(NULL == (new_buf = H5MM_realloc(udata->bkg, nbytes))) HGOTO_ERROR(H5E_RESOURCE, H5E_NOSPACE, H5_ITER_ERROR, "memory allocation failed for raw data chunk") + udata->bkg = new_buf; if(!udata->cpy_info->expand_ref) HDmemset((uint8_t *)udata->bkg + buf_size, 0, (size_t)(nbytes - buf_size)); diff --git a/src/H5Gname.c b/src/H5Gname.c index a041dcd..2bc0174 100644 --- a/src/H5Gname.c +++ b/src/H5Gname.c @@ -1079,8 +1079,11 @@ H5G_refname_iterator(hid_t group, const char *name, const H5L_info_t *link_info, /* Build the object's full name */ len_needed = HDstrlen(udata->container) + HDstrlen(name) + 2; if(len_needed > udata->max_container_len) { - if(NULL == (udata->container = H5MM_realloc(udata->container, len_needed))) + void *new_container; /* Pointer to new container */ + + if(NULL == (new_container = H5MM_realloc(udata->container, len_needed))) HGOTO_ERROR(H5E_SYM, H5E_NOSPACE, H5_ITER_ERROR, "can't allocate path string") + udata->container = new_container; udata->max_container_len = len_needed; } /* end if */ HDstrcat(udata->container, name); @@ -1129,8 +1132,11 @@ H5G_refname_iterator(hid_t group, const char *name, const H5L_info_t *link_info, len = HDstrlen(udata->container); len_needed = len + HDstrlen(name) + 2; if(len_needed > udata->max_container_len) { - if(NULL == (udata->container = H5MM_realloc(udata->container, len_needed))) + void *new_container; /* Pointer to new container */ + + if(NULL == (new_container = H5MM_realloc(udata->container, len_needed))) HGOTO_ERROR(H5E_SYM, H5E_NOSPACE, H5_ITER_ERROR, "can't allocate path string") + udata->container = new_container; udata->max_container_len = len_needed; } /* end if */ if(!udata->is_root_group) diff --git a/src/H5Gtraverse.c b/src/H5Gtraverse.c index 80d02cb..4fc345c 100644 --- a/src/H5Gtraverse.c +++ b/src/H5Gtraverse.c @@ -607,13 +607,15 @@ H5G_traverse_real(const H5G_loc_t *_loc, const char *name, unsigned target, HGOTO_ERROR(H5E_SYM, H5E_CANTOPENOBJ, FAIL, "unable to reset location") /* Check for needing a larger buffer for the individual path name components */ - if(HDstrlen(name) + 1 > H5G_comp_alloc_g) { - H5G_comp_alloc_g = MAX3(1024, 2 * H5G_comp_alloc_g, HDstrlen(name) + 1); - H5G_comp_g = H5MM_realloc(H5G_comp_g, H5G_comp_alloc_g); - if(!H5G_comp_g) { - H5G_comp_alloc_g = 0; + if((HDstrlen(name) + 1) > H5G_comp_alloc_g) { + char *new_comp; /* New component buffer */ + size_t new_alloc; /* New component buffer size */ + + new_alloc = MAX3(1024, (2 * H5G_comp_alloc_g), (HDstrlen(name) + 1)); + if(NULL == (new_comp = H5MM_realloc(H5G_comp_g, new_alloc))) HGOTO_ERROR(H5E_SYM, H5E_NOSPACE, FAIL, "unable to allocate component buffer") - } /* end if */ + H5G_comp_g = new_comp; + H5G_comp_alloc_g = new_alloc; } /* end if */ /* Traverse the path */ diff --git a/src/H5HFsection.c b/src/H5HFsection.c index e031ed2..62af976 100644 --- a/src/H5HFsection.c +++ b/src/H5HFsection.c @@ -3909,9 +3909,12 @@ HDfprintf(stderr, "%s: nrows_moved2 = %u\n", FUNC, nrows_moved2); /* Check if we need to move additional rows */ if(nrows_moved2 > 0) { + H5HF_free_section_t **new_dir_rows; /* Pointer to new array of direct row pointers */ + /* Extend the first section's row array */ - if(NULL == (sect1->u.indirect.dir_rows = H5MM_realloc(sect1->u.indirect.dir_rows, sizeof(H5HF_free_section_t *) * new_dir_nrows1))) + if(NULL == (new_dir_rows = H5MM_realloc(sect1->u.indirect.dir_rows, sizeof(H5HF_free_section_t *) * new_dir_nrows1))) HGOTO_ERROR(H5E_HEAP, H5E_NOSPACE, FAIL, "allocation failed for row section pointer array") + sect1->u.indirect.dir_rows = new_dir_rows; /* Transfer the second section's rows to first section */ HDmemcpy(§1->u.indirect.dir_rows[sect1->u.indirect.dir_nrows], @@ -3952,9 +3955,12 @@ HDfprintf(stderr, "%s: nrows_moved2 = %u\n", FUNC, nrows_moved2); sect2->u.indirect.indir_ents = NULL; } /* end if */ else { + H5HF_free_section_t **new_indir_ents; /* Pointer to new array of indirect entries */ + /* Extend the first section's entry array */ - if(NULL == (sect1->u.indirect.indir_ents = H5MM_realloc(sect1->u.indirect.indir_ents, sizeof(H5HF_free_section_t *) * new_indir_nents1))) + if(NULL == (new_indir_ents = H5MM_realloc(sect1->u.indirect.indir_ents, sizeof(H5HF_free_section_t *) * new_indir_nents1))) HGOTO_ERROR(H5E_HEAP, H5E_NOSPACE, FAIL, "allocation failed for row section pointer array") + sect1->u.indirect.indir_ents = new_indir_ents; /* Transfer the second section's entries to first section */ HDmemcpy(§1->u.indirect.indir_ents[sect1->u.indirect.indir_nents], diff --git a/src/H5L.c b/src/H5L.c index 49ef879..d9f8e0d 100644 --- a/src/H5L.c +++ b/src/H5L.c @@ -1354,7 +1354,7 @@ H5L_register(const H5L_class_t *cls) /* Filter not already registered */ if(i >= H5L_table_used_g) { if(H5L_table_used_g >= H5L_table_alloc_g) { - size_t n = MAX(H5L_MIN_TABLE_SIZE, 2 * H5L_table_alloc_g); + size_t n = MAX(H5L_MIN_TABLE_SIZE, (2 * H5L_table_alloc_g)); H5L_class_t *table = (H5L_class_t *)H5MM_realloc(H5L_table_g, (n * sizeof(H5L_class_t))); if(!table) HGOTO_ERROR(H5E_RESOURCE, H5E_NOSPACE, FAIL, "unable to extend link type table") -- cgit v0.12