From 86c18b7e4f2fd322b3571a9050c4eb051027391a Mon Sep 17 00:00:00 2001 From: Raymond Lu Date: Wed, 9 Feb 2005 10:54:44 -0500 Subject: [svn-r9966] Purpose: Bug fix Description: H5Tget_member_value didn't return correct value if called after H5Tenum_valueof. It's because there's a sorting on the members of enum type in H5Tenum_valueof which changed the order of members. Solution: Made a copy of original type and do sorting on it to protect the original order. Platforms tested: fuss; tested v1.6 with h5committest Misc. update: RELEASE.txt --- release_docs/RELEASE.txt | 3 +++ src/H5T.c | 2 +- src/H5Tenum.c | 63 +++++++++++++++++++++++++++++++++--------------- test/dtypes.c | 57 +++++++++++++++++++++++++++++++++++++------ 4 files changed, 97 insertions(+), 28 deletions(-) diff --git a/release_docs/RELEASE.txt b/release_docs/RELEASE.txt index 511cf3a..c352c7c 100644 --- a/release_docs/RELEASE.txt +++ b/release_docs/RELEASE.txt @@ -201,6 +201,9 @@ Bug Fixes since HDF5-1.6.0 release Library ------- + - H5Tget_member_value calls for enum datatype didn't return correct + value if H5Tenum_valueof was called first. It's fixed. SLU - + 2005/02/08 - For variable-length string, H5Tget_class returned H5T_STRING as its class. But H5Tdetect_class and H5Tget_member_class considered it as H5T_VLEN. This is fixed to let all these 3 functions treat it diff --git a/src/H5T.c b/src/H5T.c index 69cf02b..45479cd 100644 --- a/src/H5T.c +++ b/src/H5T.c @@ -3410,7 +3410,7 @@ H5T_free(H5T_t *dt) /* Close the parent */ if (dt->shared->parent && H5T_close(dt->shared->parent)<0) - HGOTO_ERROR(H5E_DATATYPE, H5E_CANTINIT, FAIL, "unable to close parent data type"); + HGOTO_ERROR(H5E_DATATYPE, H5E_CANTCLOSEOBJ, FAIL, "unable to close parent data type"); done: FUNC_LEAVE_NOAPI(ret_value); diff --git a/src/H5Tenum.c b/src/H5Tenum.c index 10870d7..84a71b0 100644 --- a/src/H5Tenum.c +++ b/src/H5Tenum.c @@ -414,15 +414,19 @@ done: * Monday, January 4, 1999 * * Modifications: - * + * Raymond Lu + * Wednesday, Febuary 9, 2005 + * Made a copy of original datatype and do sorting and search + * on that copy, to protect the original order of members. *------------------------------------------------------------------------- */ static char * H5T_enum_nameof(const H5T_t *dt, const void *value, char *name/*out*/, size_t size) { unsigned lt, md=0, rt; /*indices for binary search */ - int cmp=(-1); /*comparison result */ - char *ret_value; /* Return value */ + int cmp=(-1); /*comparison result */ + H5T_t *copied_dt = NULL; /*do sorting in copied datatype */ + char *ret_value; /* Return value */ FUNC_ENTER_NOAPI(H5T_enum_nameof, NULL) @@ -436,15 +440,19 @@ H5T_enum_nameof(const H5T_t *dt, const void *value, char *name/*out*/, size_t si if (dt->shared->u.enumer.nmembs == 0) HGOTO_ERROR(H5E_DATATYPE, H5E_NOTFOUND, NULL, "datatype has no members") - /* Do a binary search over the values to find the correct one */ - if(H5T_sort_value(dt, NULL)<0) + /* Do a binary search over the values to find the correct one. Do sorting + * and search on the copied datatype to protect the original order. */ + if (NULL==(copied_dt=H5T_copy(dt, H5T_COPY_ALL))) + HGOTO_ERROR(H5E_DATATYPE, H5E_CANTINIT, NULL, "unable to copy data type"); + if(H5T_sort_value(copied_dt, NULL)<0) HGOTO_ERROR(H5E_INTERNAL, H5E_CANTCOMPARE, NULL, "value sort failed") + lt = 0; - rt = dt->shared->u.enumer.nmembs; + rt = copied_dt->shared->u.enumer.nmembs; while (ltshared->u.enumer.value+md*dt->shared->size, dt->shared->size); + cmp = HDmemcmp(value, copied_dt->shared->u.enumer.value+md*copied_dt->shared->size, copied_dt->shared->size); if (cmp<0) { rt = md; } else if (cmp>0) { @@ -458,15 +466,18 @@ H5T_enum_nameof(const H5T_t *dt, const void *value, char *name/*out*/, size_t si HGOTO_ERROR(H5E_DATATYPE, H5E_NOTFOUND, NULL, "value is currently not defined") /* Save result name */ - if (!name && NULL==(name=H5MM_malloc(HDstrlen(dt->shared->u.enumer.name[md])+1))) + if (!name && NULL==(name=H5MM_malloc(HDstrlen(copied_dt->shared->u.enumer.name[md])+1))) HGOTO_ERROR(H5E_RESOURCE, H5E_NOSPACE, NULL, "memory allocation failed"); - HDstrncpy(name, dt->shared->u.enumer.name[md], size); - if (HDstrlen(dt->shared->u.enumer.name[md])>=size) + HDstrncpy(name, copied_dt->shared->u.enumer.name[md], size); + if (HDstrlen(copied_dt->shared->u.enumer.name[md])>=size) HGOTO_ERROR(H5E_DATATYPE, H5E_NOSPACE, NULL, "name has been truncated") /* Set return value */ ret_value=name; + if (H5T_close(copied_dt)<0) + HGOTO_ERROR(H5E_DATATYPE, H5E_CANTCLOSEOBJ, NULL, "unable to close data type"); + done: FUNC_LEAVE_NOAPI(ret_value) } @@ -488,7 +499,10 @@ done: * Monday, January 4, 1999 * * Modifications: - * + * Raymond Lu + * Wednesday, Febuary 9, 2005 + * Made a copy of original datatype and do sorting and search + * on that copy, to protect the original order of members. *------------------------------------------------------------------------- */ herr_t @@ -534,15 +548,19 @@ done: * Monday, January 4, 1999 * * Modifications: - * + * Raymond Lu + * Wednesday, Febuary 9, 2005 + * Made a copy of original datatype and do sorting and search + * on that copy, to protect the original order of members. *------------------------------------------------------------------------- */ static herr_t H5T_enum_valueof(const H5T_t *dt, const char *name, void *value/*out*/) { unsigned lt, md=0, rt; /*indices for binary search */ - int cmp=(-1); /*comparison result */ - herr_t ret_value=SUCCEED; /* Return value */ + int cmp=(-1); /*comparison result */ + H5T_t *copied_dt = NULL; /*do sorting in copied datatype */ + herr_t ret_value=SUCCEED; /* Return value */ FUNC_ENTER_NOAPI(H5T_enum_valueof, FAIL) @@ -555,15 +573,19 @@ H5T_enum_valueof(const H5T_t *dt, const char *name, void *value/*out*/) if (dt->shared->u.enumer.nmembs == 0) HGOTO_ERROR(H5E_DATATYPE, H5E_NOTFOUND, FAIL, "datatype has no members") - /* Do a binary search over the names to find the correct one */ - if(H5T_sort_name(dt, NULL)<0) + /* Do a binary search over the names to find the correct one. Do sorting + * and search on the copied datatype to protect the original order. */ + if (NULL==(copied_dt=H5T_copy(dt, H5T_COPY_ALL))) + HGOTO_ERROR(H5E_DATATYPE, H5E_CANTINIT, FAIL, "unable to copy data type"); + if(H5T_sort_name(copied_dt, NULL)<0) HGOTO_ERROR(H5E_INTERNAL, H5E_CANTCOMPARE, FAIL, "value sort failed") + lt = 0; - rt = dt->shared->u.enumer.nmembs; + rt = copied_dt->shared->u.enumer.nmembs; while (ltshared->u.enumer.name[md]); + cmp = HDstrcmp(name, copied_dt->shared->u.enumer.name[md]); if (cmp<0) { rt = md; } else if (cmp>0) { @@ -576,7 +598,10 @@ H5T_enum_valueof(const H5T_t *dt, const char *name, void *value/*out*/) if (cmp!=0) HGOTO_ERROR(H5E_DATATYPE, H5E_NOTFOUND, FAIL, "string doesn't exist in the enumeration type") - HDmemcpy(value, dt->shared->u.enumer.value+md*dt->shared->size, dt->shared->size); + HDmemcpy(value, copied_dt->shared->u.enumer.value+md*copied_dt->shared->size, copied_dt->shared->size); + + if (H5T_close(copied_dt)<0) + HGOTO_ERROR(H5E_DATATYPE, H5E_CANTCLOSEOBJ, FAIL, "unable to close data type"); done: FUNC_LEAVE_NOAPI(ret_value) diff --git a/test/dtypes.c b/test/dtypes.c index 77254ea..c119eb7 100644 --- a/test/dtypes.c +++ b/test/dtypes.c @@ -2267,7 +2267,10 @@ test_compound_12(void) * Thursday, April 4, 2002 * * Modifications: - * + * Raymond Lu + * Wednesday, Febuary 9, 2005 + * Added test for H5Tenum_valueof, H5Tenum_nameof, and + * H5Tget_member_value. *------------------------------------------------------------------------- */ static int @@ -2283,6 +2286,7 @@ test_query(void) char filename[1024]; char compnd_type[]="Compound_type", enum_type[]="Enum_type"; short enum_val; + char enum_name[16]; TESTING("query functions of compound and enumeration types"); @@ -2324,27 +2328,27 @@ test_query(void) printf("Can't create enumerate type\n"); goto error; } /* end if */ - if(H5Tenum_insert(tid2, "RED", (enum_val=0,&enum_val))<0) { + if(H5Tenum_insert(tid2, "RED", (enum_val=10,&enum_val))<0) { H5_FAILED(); printf("Can't insert field into enumeration type\n"); goto error; } /* end if */ - if(H5Tenum_insert(tid2, "GREEN", (enum_val=1,&enum_val))<0) { + if(H5Tenum_insert(tid2, "GREEN", (enum_val=11,&enum_val))<0) { H5_FAILED(); printf("Can't insert field into enumeration type\n"); goto error; } /* end if */ - if(H5Tenum_insert(tid2, "BLUE", (enum_val=2,&enum_val))<0) { + if(H5Tenum_insert(tid2, "BLUE", (enum_val=12,&enum_val))<0) { H5_FAILED(); printf("Can't insert field into enumeration type\n"); goto error; } /* end if */ - if(H5Tenum_insert(tid2, "ORANGE", (enum_val=3,&enum_val))<0) { + if(H5Tenum_insert(tid2, "ORANGE", (enum_val=13,&enum_val))<0) { H5_FAILED(); printf("Can't insert field into enumeration type\n"); goto error; } /* end if */ - if(H5Tenum_insert(tid2, "YELLOW", (enum_val=4,&enum_val))<0) { + if(H5Tenum_insert(tid2, "YELLOW", (enum_val=14,&enum_val))<0) { H5_FAILED(); printf("Can't insert field into enumeration type\n"); goto error; @@ -2362,7 +2366,7 @@ test_query(void) goto error; } /* end if */ - /* Query member number and member index by name, for enumeration type. */ + /* Query member number and member index by member name, for enumeration type. */ if(H5Tget_nmembers(tid2)!=5) { H5_FAILED(); printf("Can't get member number\n"); @@ -2422,7 +2426,7 @@ test_query(void) goto error; } /* end if */ - /* Query member number and member index by name, for enumeration type */ + /* Query member number and member index by member name, for enumeration type */ if(H5Tget_nmembers(tid2)!=5) { H5_FAILED(); printf("Can't get member number\n"); @@ -2434,6 +2438,43 @@ test_query(void) goto error; } /* end if */ + /* Query member value by member name, for enumeration type */ + if(H5Tenum_valueof (tid2, "ORANGE", &enum_val)<0) { + H5_FAILED(); + printf("Can't get value for enumerate member\n"); + goto error; + } /* end if */ + if(enum_val!=13) { + H5_FAILED(); + printf("Incorrect value for enum member\n"); + goto error; + } /* end if */ + + /* Query member value by member index, for enumeration type */ + if(H5Tget_member_value (tid2, 2, &enum_val)<0) { + H5_FAILED(); + printf("Can't get value for enum member\n"); + goto error; + } /* end if */ + if(enum_val!=12) { + H5_FAILED(); + printf("Incorrect value for enum member\n"); + goto error; + } /* end if */ + + /* Query member name by member value, for enumeration type */ + enum_val = 14; + if(H5Tenum_nameof(tid2, &enum_val, enum_name, 16)<0) { + H5_FAILED(); + printf("Can't get name for enum member\n"); + goto error; + } /* end if */ + if(strcmp("YELLOW", enum_name)) { + H5_FAILED(); + printf("Incorrect name for enum member\n"); + goto error; + } /* end if */ + /* Close data type and file */ if(H5Tclose(tid1)<0) { H5_FAILED(); -- cgit v0.12