diff options
author | Neil Fortner <nfortne2@hdfgroup.org> | 2009-06-10 19:22:09 (GMT) |
---|---|---|
committer | Neil Fortner <nfortne2@hdfgroup.org> | 2009-06-10 19:22:09 (GMT) |
commit | bc2bd03a466909e874220e3f08e6abe79a5dbe9b (patch) | |
tree | 3bedd4f4b9fc4d4c2e29f0231f0c9243093ceb8c /src/H5T.c | |
parent | 66153395600210692eac9170794bfff3804106c8 (diff) | |
download | hdf5-bc2bd03a466909e874220e3f08e6abe79a5dbe9b.zip hdf5-bc2bd03a466909e874220e3f08e6abe79a5dbe9b.tar.gz hdf5-bc2bd03a466909e874220e3f08e6abe79a5dbe9b.tar.bz2 |
[svn-r17026] Purpose: fix bug 1593
Description:
When using H5T_copy on committed datatypes that are already open, H5T_copy would
properly use the already existing shared struct, but would still deep copy all
of the fields in that struct. This would cause memory leaks, and in the case of
a compound containing a vlen (or reference), the change in size would cause the
size of the resulting type to be set to an incorrect value. Changed H5T_copy to
properly avoid deep copies when using a reopened shared struct.
Tested: jam, linew, smirom (h5committest), purify on jam
Diffstat (limited to 'src/H5T.c')
-rw-r--r-- | src/H5T.c | 200 |
1 files changed, 102 insertions, 98 deletions
@@ -3161,118 +3161,122 @@ H5T_copy(const H5T_t *old_dt, H5T_copy_t method) break; } /* end switch */ - /* Copy parent information, if we aren't sharing an already opened committed datatype */ - if(NULL == reopened_fo && old_dt->shared->parent) - new_dt->shared->parent = H5T_copy(old_dt->shared->parent, method); + /* Update fields in the new struct, if we aren't sharing an already opened + * committed datatype */ + if(!reopened_fo) { + /* Copy parent information */ + if(old_dt->shared->parent) + new_dt->shared->parent = H5T_copy(old_dt->shared->parent, method); + + switch(new_dt->shared->type) { + case H5T_COMPOUND: + { + int accum_change = 0; /* Amount of change in the offset of the fields */ - switch(new_dt->shared->type) { - case H5T_COMPOUND: - { - int accum_change = 0; /* Amount of change in the offset of the fields */ + /* + * Copy all member fields to new type, then overwrite the + * name and type fields of each new member with copied values. + * That is, H5T_copy() is a deep copy. + */ + /* Only malloc if space has been allocated for members - NAF */ + if(new_dt->shared->u.compnd.nalloc > 0) { + new_dt->shared->u.compnd.memb = H5MM_malloc(new_dt->shared->u.compnd.nalloc * + sizeof(H5T_cmemb_t)); + if (NULL==new_dt->shared->u.compnd.memb) + HGOTO_ERROR(H5E_RESOURCE, H5E_NOSPACE, NULL, "memory allocation failed"); + + HDmemcpy(new_dt->shared->u.compnd.memb, old_dt->shared->u.compnd.memb, + new_dt->shared->u.compnd.nmembs * sizeof(H5T_cmemb_t)); + } /* end if */ - /* - * Copy all member fields to new type, then overwrite the - * name and type fields of each new member with copied values. - * That is, H5T_copy() is a deep copy. - */ - /* Only malloc if space has been allocated for members - NAF */ - if(new_dt->shared->u.compnd.nalloc > 0) { - new_dt->shared->u.compnd.memb = H5MM_malloc(new_dt->shared->u.compnd.nalloc * - sizeof(H5T_cmemb_t)); - if (NULL==new_dt->shared->u.compnd.memb) - HGOTO_ERROR(H5E_RESOURCE, H5E_NOSPACE, NULL, "memory allocation failed"); + for(i = 0; i < new_dt->shared->u.compnd.nmembs; i++) { + unsigned j; + int old_match; - HDmemcpy(new_dt->shared->u.compnd.memb, old_dt->shared->u.compnd.memb, - new_dt->shared->u.compnd.nmembs * sizeof(H5T_cmemb_t)); - } /* end if */ + s = new_dt->shared->u.compnd.memb[i].name; + new_dt->shared->u.compnd.memb[i].name = H5MM_xstrdup(s); + tmp = H5T_copy (old_dt->shared->u.compnd.memb[i].type, method); + new_dt->shared->u.compnd.memb[i].type = tmp; + HDassert(tmp != NULL); - for(i = 0; i < new_dt->shared->u.compnd.nmembs; i++) { - unsigned j; - int old_match; + /* Apply the accumulated size change to the offset of the field */ + new_dt->shared->u.compnd.memb[i].offset += accum_change; + + if(old_dt->shared->u.compnd.sorted != H5T_SORT_VALUE) { + for(old_match = -1, j = 0; j < old_dt->shared->u.compnd.nmembs; j++) { + if(!HDstrcmp(new_dt->shared->u.compnd.memb[i].name, old_dt->shared->u.compnd.memb[j].name)) { + old_match = j; + break; + } /* end if */ + } /* end for */ + + /* check if we couldn't find a match */ + if(old_match < 0) + HGOTO_ERROR(H5E_DATATYPE, H5E_CANTCOPY, NULL, "fields in datatype corrupted"); + } /* end if */ + else + old_match = i; - s = new_dt->shared->u.compnd.memb[i].name; - new_dt->shared->u.compnd.memb[i].name = H5MM_xstrdup(s); - tmp = H5T_copy (old_dt->shared->u.compnd.memb[i].type, method); - new_dt->shared->u.compnd.memb[i].type = tmp; - HDassert(tmp != NULL); + /* If the field changed size, add that change to the accumulated size change */ + if(new_dt->shared->u.compnd.memb[i].type->shared->size != old_dt->shared->u.compnd.memb[old_match].type->shared->size) { + /* Adjust the size of the member */ + new_dt->shared->u.compnd.memb[i].size = (old_dt->shared->u.compnd.memb[old_match].size*tmp->shared->size)/old_dt->shared->u.compnd.memb[old_match].type->shared->size; - /* Apply the accumulated size change to the offset of the field */ - new_dt->shared->u.compnd.memb[i].offset += accum_change; + accum_change += (new_dt->shared->u.compnd.memb[i].type->shared->size - old_dt->shared->u.compnd.memb[old_match].type->shared->size); + } /* end if */ + } /* end for */ - if(old_dt->shared->u.compnd.sorted != H5T_SORT_VALUE) { - for(old_match = -1, j = 0; j < old_dt->shared->u.compnd.nmembs; j++) { - if(!HDstrcmp(new_dt->shared->u.compnd.memb[i].name, old_dt->shared->u.compnd.memb[j].name)) { - old_match = j; - break; - } /* end if */ - } /* end for */ + /* Apply the accumulated size change to the size of the compound struct */ + new_dt->shared->size += accum_change; - /* check if we couldn't find a match */ - if(old_match < 0) - HGOTO_ERROR(H5E_DATATYPE, H5E_CANTCOPY, NULL, "fields in datatype corrupted"); - } /* end if */ - else - old_match = i; + } + break; - /* If the field changed size, add that change to the accumulated size change */ - if(new_dt->shared->u.compnd.memb[i].type->shared->size != old_dt->shared->u.compnd.memb[old_match].type->shared->size) { - /* Adjust the size of the member */ - new_dt->shared->u.compnd.memb[i].size = (old_dt->shared->u.compnd.memb[old_match].size*tmp->shared->size)/old_dt->shared->u.compnd.memb[old_match].type->shared->size; + case H5T_ENUM: + /* + * Copy all member fields to new type, then overwrite the name fields + * of each new member with copied values. That is, H5T_copy() is a + * deep copy. + */ + new_dt->shared->u.enumer.name = H5MM_malloc(new_dt->shared->u.enumer.nalloc * + sizeof(char*)); + new_dt->shared->u.enumer.value = H5MM_malloc(new_dt->shared->u.enumer.nalloc * + new_dt->shared->size); + if(NULL == new_dt->shared->u.enumer.value) + HGOTO_ERROR(H5E_RESOURCE, H5E_NOSPACE, NULL, "memory allocation failed"); + HDmemcpy(new_dt->shared->u.enumer.value, old_dt->shared->u.enumer.value, + new_dt->shared->u.enumer.nmembs * new_dt->shared->size); + for(i = 0; i < new_dt->shared->u.enumer.nmembs; i++) { + s = old_dt->shared->u.enumer.name[i]; + new_dt->shared->u.enumer.name[i] = H5MM_xstrdup(s); + } /* end for */ + break; - accum_change += (new_dt->shared->u.compnd.memb[i].type->shared->size - old_dt->shared->u.compnd.memb[old_match].type->shared->size); + case H5T_VLEN: + case H5T_REFERENCE: + if(method == H5T_COPY_TRANSIENT || method == H5T_COPY_REOPEN) { + /* H5T_copy converts any type into a memory type */ + if(H5T_set_loc(new_dt, NULL, H5T_LOC_MEMORY) < 0) + HGOTO_ERROR(H5E_DATATYPE, H5E_CANTINIT, NULL, "invalid datatype location"); } /* end if */ - } /* end for */ - - /* Apply the accumulated size change to the size of the compound struct */ - new_dt->shared->size += accum_change; - - } - break; - - case H5T_ENUM: - /* - * Copy all member fields to new type, then overwrite the name fields - * of each new member with copied values. That is, H5T_copy() is a - * deep copy. - */ - new_dt->shared->u.enumer.name = H5MM_malloc(new_dt->shared->u.enumer.nalloc * - sizeof(char*)); - new_dt->shared->u.enumer.value = H5MM_malloc(new_dt->shared->u.enumer.nalloc * - new_dt->shared->size); - if(NULL == new_dt->shared->u.enumer.value) - HGOTO_ERROR(H5E_RESOURCE, H5E_NOSPACE, NULL, "memory allocation failed"); - HDmemcpy(new_dt->shared->u.enumer.value, old_dt->shared->u.enumer.value, - new_dt->shared->u.enumer.nmembs * new_dt->shared->size); - for(i = 0; i < new_dt->shared->u.enumer.nmembs; i++) { - s = old_dt->shared->u.enumer.name[i]; - new_dt->shared->u.enumer.name[i] = H5MM_xstrdup(s); - } /* end for */ - break; - - case H5T_VLEN: - case H5T_REFERENCE: - if(method == H5T_COPY_TRANSIENT || method == H5T_COPY_REOPEN) { - /* H5T_copy converts any type into a memory type */ - if(H5T_set_loc(new_dt, NULL, H5T_LOC_MEMORY) < 0) - HGOTO_ERROR(H5E_DATATYPE, H5E_CANTINIT, NULL, "invalid datatype location"); - } /* end if */ - break; + break; - case H5T_OPAQUE: - /* - * Copy the tag name. - */ - new_dt->shared->u.opaque.tag = H5MM_xstrdup(new_dt->shared->u.opaque.tag); - break; + case H5T_OPAQUE: + /* + * Copy the tag name. + */ + new_dt->shared->u.opaque.tag = H5MM_xstrdup(new_dt->shared->u.opaque.tag); + break; - case H5T_ARRAY: - /* Re-compute the array's size, in case it's base type changed size */ - new_dt->shared->size=new_dt->shared->u.array.nelem*new_dt->shared->parent->shared->size; - break; + case H5T_ARRAY: + /* Re-compute the array's size, in case it's base type changed size */ + new_dt->shared->size=new_dt->shared->u.array.nelem*new_dt->shared->parent->shared->size; + break; - default: - break; - } /* end switch */ + default: + break; + } /* end switch */ + } /* end if */ /* Set the cached location & name path if the original type was a named * type and the new type is also named. |