summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorRaymond Lu <songyulu@hdfgroup.org>2005-02-08 22:20:48 (GMT)
committerRaymond Lu <songyulu@hdfgroup.org>2005-02-08 22:20:48 (GMT)
commit7a420dad641c76d53904840ce1ae24fc9e27f5ad (patch)
tree80dcd4fa1e48dd9e7d185df54dc278e3a5ac682c
parent99c08dd282650fcf968e37efdba00fa65bd349b7 (diff)
downloadhdf5-7a420dad641c76d53904840ce1ae24fc9e27f5ad.zip
hdf5-7a420dad641c76d53904840ce1ae24fc9e27f5ad.tar.gz
hdf5-7a420dad641c76d53904840ce1ae24fc9e27f5ad.tar.bz2
[svn-r9963] Purpose: Bug fix
Description: For enum datatype, if H5Tget_member_value was called after H5Tenum_valueof, it wouldn't return correct value. It happened because there's sorting in H5Tenum_valueof which changed the order of enum members. Solution: Made a copy of original enum datatype and do sorting on the copied datatype, to protect the original order. Also added test case in dtypes.c Platforms tested: h5committest and fuss. Misc. update: RELEASE.txt
-rw-r--r--release_docs/RELEASE.txt3
-rw-r--r--src/H5T.c2
-rw-r--r--src/H5Tenum.c46
-rw-r--r--test/dtypes.c50
4 files changed, 78 insertions, 23 deletions
diff --git a/release_docs/RELEASE.txt b/release_docs/RELEASE.txt
index 4d67be3..3a16cbd 100644
--- a/release_docs/RELEASE.txt
+++ b/release_docs/RELEASE.txt
@@ -70,6 +70,9 @@ Bug Fixes since HDF5-1.6.3 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 72dcdd0..96443cc 100644
--- a/src/H5T.c
+++ b/src/H5T.c
@@ -3204,7 +3204,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..5f61e89 100644
--- a/src/H5Tenum.c
+++ b/src/H5Tenum.c
@@ -421,8 +421,9 @@ 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 +437,18 @@ 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 (lt<rt) {
md = (lt+rt)/2;
- cmp = HDmemcmp(value, dt->shared->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 +462,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)
}
@@ -541,8 +548,9 @@ 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 +563,18 @@ 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 (lt<rt) {
md = (lt+rt)/2;
- cmp = HDstrcmp(name, dt->shared->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 +587,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 c15d484..8b3b653 100644
--- a/test/dtypes.c
+++ b/test/dtypes.c
@@ -2244,6 +2244,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");
@@ -2285,27 +2286,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;
@@ -2383,7 +2384,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");
@@ -2395,6 +2396,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();