Merge pull request #1750 in HDFFV/hdf5 from ~DEROBINS/hdf5_der:ohdr_test_memory_issue to develop

* commit '6a1e9a1fbe9606d0dd3452092cab17794e131e64':
  Fixed a problem in the ohdr test where hard-coded strings passed to H5Awrite() triggered -fsanitize failures.
This commit is contained in:
Dana Robinson 2019-06-19 10:23:04 -05:00
commit 4d20db43be
2 changed files with 149 additions and 201 deletions

View File

@ -771,7 +771,7 @@ H5A__write(H5A_t *attr, const H5T_t *mem_type, const void *buf)
if(NULL == (attr->shared->data = H5FL_BLK_MALLOC(attr_buf, dst_type_size * nelmts))) if(NULL == (attr->shared->data = H5FL_BLK_MALLOC(attr_buf, dst_type_size * nelmts)))
HGOTO_ERROR(H5E_RESOURCE, H5E_NOSPACE, FAIL, "memory allocation failed") HGOTO_ERROR(H5E_RESOURCE, H5E_NOSPACE, FAIL, "memory allocation failed")
/* Copy the attribute data into the user's buffer */ /* Copy the attribute data into the attribute data buffer */
H5MM_memcpy(attr->shared->data, buf, (dst_type_size * nelmts)); H5MM_memcpy(attr->shared->data, buf, (dst_type_size * nelmts));
} /* end else */ } /* end else */

View File

@ -750,27 +750,6 @@ error:
return FAIL; return FAIL;
} /* test_unknown() */ } /* test_unknown() */
/*
* Set an attribute with the given information.
* If the out parameter `attr_id` is negative, a new attribute will be
* created with the given information. Else, it will attempt to update the
* attribute with the new value.
*
* `dataspace_id` ignored if `attribute_id` >= 0
*/
static herr_t
put_attribute(hid_t loc_id, const char *attrname, const void *attrvalue, hid_t datatype_id, hid_t dataspace_id, hid_t *attribute_id)
{
if((*attribute_id) < 0) {
hid_t id = -1;
id = H5Acreate2(loc_id, attrname, datatype_id, dataspace_id, H5P_DEFAULT, H5P_DEFAULT);
if(id < 0)
return FAIL;
*attribute_id = id;
}
return H5Awrite(*attribute_id, datatype_id, attrvalue);
} /* put_attribute */
/* /*
* Count the number of attributes attached to an object. * Count the number of attributes attached to an object.
* Returns negative in event of error. * Returns negative in event of error.
@ -830,216 +809,181 @@ oh_compare(hid_t did1, hid_t did2)
* Conduct additions side-by-side with a standard datataset and one with * Conduct additions side-by-side with a standard datataset and one with
* minimized dataset object headers. * minimized dataset object headers.
*/ */
#define ATTR_NAME_MAX 16
#define ATTR_SHORT "first"
#define ATTR_LONG "second"
#define N_ATTRS 64
static herr_t static herr_t
test_minimized_dset_ohdr_attribute_addition(hid_t fapl_id) test_minimized_dset_ohdr_attribute_addition(hid_t fapl_id)
{ {
hsize_t array_10[1] = {10}; /* dataspace extent */ hsize_t dims[1] = {0}; /* dataspace extent */
char buffer[10] = ""; /* to inspect string attribute */ char filename[512] = "";
int a_out = 0; char attr_name[ATTR_NAME_MAX] = "";
char filename[512] = ""; hid_t fid = H5I_INVALID_HID;
hid_t int_type_id = -1; hid_t dcpl_id = H5I_INVALID_HID;
hid_t char_type_id = -1; hid_t sid = H5I_INVALID_HID;
hid_t dcpl_id = -1; hid_t did = H5I_INVALID_HID;
hid_t dspace_id = -1; hid_t aid = H5I_INVALID_HID;
hid_t dspace_scalar_id = -1; char *in_buf = NULL;
hid_t dset_id = -1; char *out_buf = NULL;
hid_t mindset_id = -1; size_t buf_size = 0;
hid_t attr_1_id = -1; int out_val = 0;
hid_t attr_1a_id = -1; int in_val = 0;
hid_t attr_2_id = -1; int i;
hid_t attr_2a_id = -1;
hid_t attr_3_id = -1;
hid_t attr_3a_id = -1;
hid_t file_id = -1;
herr_t ret;
int count = 0;
TESTING("minimized dset object headers attribute additions") TESTING("adding attributes to datasets created with H5Pset_dset_no_attrs_hint()")
/********* /* Create the test file */
* SETUP * if(NULL == h5_fixname(FILENAME[1], fapl_id, filename, sizeof(filename)))
*********/ TEST_ERROR;
if((fid = H5Fcreate(filename, H5F_ACC_TRUNC, H5P_DEFAULT, fapl_id)) == H5I_INVALID_HID)
TEST_ERROR;
if(h5_fixname(FILENAME[1], fapl_id, filename, sizeof(filename)) == NULL) /* Set the 'no attrs' hint on the dcpl */
TEST_ERROR if((dcpl_id = H5Pcreate(H5P_DATASET_CREATE)) == H5I_INVALID_HID)
TEST_ERROR;
if(H5Pset_dset_no_attrs_hint(dcpl_id, TRUE) < 0)
TEST_ERROR;
dspace_id = H5Screate_simple(1, array_10, NULL); /* The dataset doesn't need to contain data */
if(dspace_id < 0) TEST_ERROR dims[0] = 0;
if((sid = H5Screate_simple(1, dims, NULL)) == H5I_INVALID_HID)
TEST_ERROR;
dspace_scalar_id = H5Screate(H5S_SCALAR); /* Create the dataset */
if(dspace_scalar_id < 0) TEST_ERROR if((did = H5Dcreate2(fid, "H5Pset_dset_no_attrs_hint", H5T_NATIVE_INT, sid, H5P_DEFAULT, dcpl_id, H5P_DEFAULT)) == H5I_INVALID_HID)
TEST_ERROR;
char_type_id = H5Tcopy(H5T_NATIVE_CHAR); /* Close */
if(char_type_id < 0) TEST_ERROR if(H5Pclose(dcpl_id) < 0)
TEST_ERROR;
if(H5Sclose(sid) < 0)
TEST_ERROR;
int_type_id = H5Tcopy(H5T_NATIVE_INT); /**********************************************
if(int_type_id < 0) TEST_ERROR * ADD A (STRING) ATTRIBUTE AND MANIPULATE IT *
**********************************************/
dcpl_id = H5Pcreate(H5P_DATASET_CREATE); buf_size = HDstrlen(ATTR_LONG) + 1;
if(dcpl_id < 0) TEST_ERROR if(NULL == (in_buf = (char *)HDcalloc(buf_size, sizeof(char))))
TEST_ERROR;
if(NULL == (out_buf = (char *)HDcalloc(buf_size, sizeof(char))))
TEST_ERROR;
ret = H5Pset_dset_no_attrs_hint(dcpl_id, TRUE); /* Create a string attribute on the dataset
if(ret < 0) TEST_ERROR *
* It has to be long enough to hold the longest string we're going to write
file_id = H5Fcreate(filename, H5F_ACC_TRUNC, H5P_DEFAULT, fapl_id); * to it.
if(file_id < 0) TEST_ERROR
H5E_BEGIN_TRY {
count = count_attributes(dset_id);
} H5E_END_TRY;
if(count != -1) TEST_ERROR
dset_id = H5Dcreate2(file_id, "dataset", int_type_id, dspace_id, H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT);
if(dset_id < 0) TEST_ERROR
mindset_id = H5Dcreate2(file_id, "mindataset", int_type_id, dspace_id, H5P_DEFAULT, dcpl_id, H5P_DEFAULT);
if(mindset_id < 0) TEST_ERROR
/********************
* TEST/DEMONSTRATE *
********************/
/* -------------------
* no attributes added
*/ */
dims[0] = buf_size;
if((sid = H5Screate_simple(1, dims, NULL)) == H5I_INVALID_HID)
TEST_ERROR;
if((aid = H5Acreate2(did, "string_attr", H5T_NATIVE_CHAR, sid, H5P_DEFAULT, H5P_DEFAULT)) == H5I_INVALID_HID)
TEST_ERROR;
count = count_attributes(dset_id); /* Write attribute data */
if(count != 0) TEST_ERROR HDstrcpy(in_buf, ATTR_SHORT);
count = count_attributes(mindset_id); if(H5Awrite(aid, H5T_NATIVE_CHAR, in_buf) < 0)
if(count != 0) TEST_ERROR TEST_ERROR;
/* ----------------- /* Make sure the count is correct */
* add one attribute if(count_attributes(did) != 1)
*/ TEST_ERROR;
ret = put_attribute(dset_id, "PURPOSE", "DEMO", char_type_id, dspace_id, &attr_1_id);
if(ret < 0) TEST_ERROR
ret = put_attribute(mindset_id, "PURPOSE", "DEMO", char_type_id, dspace_id, &attr_1a_id); /* Read the data back and verify */
if(ret < 0) TEST_ERROR if(H5Aread(aid, H5T_NATIVE_CHAR, out_buf) < 0)
TEST_ERROR;
if(HDstrcmp(in_buf, out_buf))
TEST_ERROR;
count = count_attributes(dset_id); /* modify the string attribute */
if(count != 1) TEST_ERROR HDmemset(in_buf, 0, buf_size);
count = count_attributes(mindset_id); HDstrcpy(in_buf, ATTR_LONG);
if(count != 1) TEST_ERROR if(H5Awrite(aid, H5T_NATIVE_CHAR, in_buf) < 0)
TEST_ERROR;
ret = H5Aread(attr_1_id, char_type_id, buffer); if(count_attributes(did) != 1)
if(ret < 0) TEST_ERROR TEST_ERROR;
if(HDstrcmp("DEMO", buffer)) TEST_ERROR
ret = H5Aread(attr_1a_id, char_type_id, buffer); /* Read the data back and verify */
if(ret < 0) TEST_ERROR if(H5Aread(aid, H5T_NATIVE_CHAR, out_buf) < 0)
if(HDstrcmp("DEMO", buffer)) TEST_ERROR TEST_ERROR;
if(HDstrcmp(in_buf, out_buf))
TEST_ERROR;
/* ----------------- /* Close */
* modify one attribute if(H5Sclose(sid) < 0)
*/ TEST_ERROR;
if(H5Aclose(aid) < 0)
TEST_ERROR;
ret = put_attribute(dset_id, "PURPOSE", "REWRITE", char_type_id, -1, &attr_1_id); /***************************************
if(ret < 0) TEST_ERROR * ADD A BUNCH OF (INTEGER) ATTRIBUTES *
***************************************/
ret = put_attribute(mindset_id, "PURPOSE", "REWRITE", char_type_id, -1, &attr_1a_id); if((sid = H5Screate(H5S_SCALAR)) == H5I_INVALID_HID)
if(ret < 0) TEST_ERROR TEST_ERROR;
count = count_attributes(dset_id); /* Loop over a reasonable number of attributes */
if(count != 1) TEST_ERROR for(i = 0; i < N_ATTRS; i++) {
count = count_attributes(mindset_id);
if(count != 1) TEST_ERROR
ret = H5Aread(attr_1_id, char_type_id, buffer); /* Set the attribute's name */
if(ret < 0) TEST_ERROR if(HDsnprintf(attr_name, ATTR_NAME_MAX, "int_attr_%d", i) < 0)
if(HDstrcmp("REWRITE", buffer)) TEST_ERROR TEST_ERROR;
ret = H5Aread(attr_1a_id, char_type_id, buffer); /* Create an integer attribute on the dataset */
if(ret < 0) TEST_ERROR if((aid = H5Acreate2(did, attr_name, H5T_NATIVE_INT, sid, H5P_DEFAULT, H5P_DEFAULT)) == H5I_INVALID_HID)
if(HDstrcmp("REWRITE", buffer)) TEST_ERROR TEST_ERROR;
/* ----------------- /* Write attribute data */
* add second attribute in_val = i;
*/ if(H5Awrite(aid, H5T_NATIVE_INT, &in_val) < 0)
TEST_ERROR;
a_out = 5; /* Make sure the count is correct (already has one attribute) */
ret = put_attribute(dset_id, "RANK", &a_out, int_type_id, dspace_scalar_id, &attr_2_id); if(count_attributes(did) != i + 2)
if(ret < 0) TEST_ERROR TEST_ERROR;
a_out = 3; /* Read the data back and verify */
ret = put_attribute(mindset_id, "RANK", &a_out, int_type_id, dspace_scalar_id, &attr_2a_id); if(H5Aread(aid, H5T_NATIVE_INT, &out_val) < 0)
if(ret < 0) TEST_ERROR TEST_ERROR;
if(in_val != out_val)
TEST_ERROR;
count = count_attributes(dset_id); /* Close */
if(count != 2) TEST_ERROR if(H5Aclose(aid) < 0)
count = count_attributes(mindset_id); TEST_ERROR;
if(count != 2) TEST_ERROR }
ret = H5Aread(attr_2_id, int_type_id, &a_out); /* Close */
if(ret < 0) TEST_ERROR if(H5Sclose(sid) < 0)
if(a_out != 5) TEST_ERROR TEST_ERROR;
ret = H5Aread(attr_2a_id, int_type_id, &a_out); /* Close the remaining IDs */
if(ret < 0) TEST_ERROR if(H5Dclose(did) < 0)
if(a_out != 3) TEST_ERROR TEST_ERROR;
if(H5Fclose(fid) < 0)
TEST_ERROR;
/* ----------------- /* Free memory */
* add third attribute HDfree(in_buf);
*/ HDfree(out_buf);
a_out = -86; PASSED();
ret = put_attribute(dset_id, "FLAVOR", &a_out, int_type_id, dspace_scalar_id, &attr_3_id);
if(ret < 0) TEST_ERROR
a_out = 2185;
ret = put_attribute(mindset_id, "FLAVOR", &a_out, int_type_id, dspace_scalar_id, &attr_3a_id);
if(ret < 0) TEST_ERROR
count = count_attributes(dset_id);
if(count != 3) TEST_ERROR
count = count_attributes(mindset_id);
if(count != 3) TEST_ERROR
ret = H5Aread(attr_3_id, int_type_id, &a_out);
if(ret < 0) TEST_ERROR
if(a_out != -86) TEST_ERROR
ret = H5Aread(attr_3a_id, int_type_id, &a_out);
if(ret < 0) TEST_ERROR
if(a_out != 2185) TEST_ERROR
/************
* TEARDOWN *
************/
if(H5Tclose(int_type_id) < 0) TEST_ERROR
if(H5Tclose(char_type_id) < 0) TEST_ERROR
if(H5Pclose(dcpl_id) < 0) TEST_ERROR
if(H5Sclose(dspace_id) < 0) TEST_ERROR
if(H5Dclose(dset_id) < 0) TEST_ERROR
if(H5Dclose(mindset_id) < 0) TEST_ERROR
if(H5Aclose(attr_1_id) < 0) TEST_ERROR
if(H5Aclose(attr_1a_id) < 0) TEST_ERROR
if(H5Aclose(attr_2_id) < 0) TEST_ERROR
if(H5Aclose(attr_2a_id) < 0) TEST_ERROR
if(H5Aclose(attr_3_id) < 0) TEST_ERROR
if(H5Aclose(attr_3a_id) < 0) TEST_ERROR
if(H5Fclose(file_id) < 0) TEST_ERROR
PASSED()
return SUCCEED; return SUCCEED;
error : error :
H5E_BEGIN_TRY { H5E_BEGIN_TRY {
(void)H5Tclose(int_type_id);
(void)H5Tclose(char_type_id);
(void)H5Pclose(dcpl_id); (void)H5Pclose(dcpl_id);
(void)H5Sclose(dspace_id); (void)H5Sclose(sid);
(void)H5Dclose(dset_id); (void)H5Dclose(did);
(void)H5Dclose(mindset_id); (void)H5Aclose(aid);
(void)H5Aclose(attr_1_id); (void)H5Fclose(fid);
(void)H5Aclose(attr_1a_id);
(void)H5Aclose(attr_2_id);
(void)H5Aclose(attr_2a_id);
(void)H5Aclose(attr_3_id);
(void)H5Aclose(attr_3a_id);
(void)H5Fclose(file_id);
} H5E_END_TRY; } H5E_END_TRY;
HDfree(in_buf);
HDfree(out_buf);
return FAIL; return FAIL;
} /* test_minimized_dset_ohdr_attribute_addition */ } /* test_minimized_dset_ohdr_attribute_addition */
@ -1085,19 +1029,24 @@ test_minimized_dset_ohdr_size_comparisons(hid_t fapl_id)
* file-minimized | F_x | F_Y | F_N * file-minimized | F_x | F_Y | F_N
*/ */
TESTING("minimized dset object headers size comparisons");
/********* /*********
* SETUP * * SETUP *
*********/ *********/
/* Set filenames (not in a test, can't use TEST_ERROR) */
if(h5_fixname(FILENAME[1], fapl_id, filename_a, sizeof(filename_a)) == NULL) if(h5_fixname(FILENAME[1], fapl_id, filename_a, sizeof(filename_a)) == NULL)
TEST_ERROR return FAIL;
if(h5_fixname(FILENAME[2], fapl_id, filename_b, sizeof(filename_b)) == NULL) if(h5_fixname(FILENAME[2], fapl_id, filename_b, sizeof(filename_b)) == NULL)
TEST_ERROR return FAIL;
for (compact = 0; compact < 2; compact++) { /* 0 or 1 */ for (compact = 0; compact < 2; compact++) { /* 0 or 1 */
if(compact)
TESTING("minimized dset object headers size comparisons (compact)")
else
TESTING("minimized dset object headers size comparisons")
dcpl_default = H5Pcreate(H5P_DATASET_CREATE); dcpl_default = H5Pcreate(H5P_DATASET_CREATE);
if(dcpl_default < 0) TEST_ERROR if(dcpl_default < 0) TEST_ERROR
@ -1112,15 +1061,13 @@ test_minimized_dset_ohdr_size_comparisons(hid_t fapl_id)
if(ret < 0) TEST_ERROR if(ret < 0) TEST_ERROR
if(compact) { if(compact) {
HDprintf("...compact ");
ret = H5Pset_layout(dcpl_default, H5D_COMPACT); ret = H5Pset_layout(dcpl_default, H5D_COMPACT);
if(ret < 0) TEST_ERROR if(ret < 0) TEST_ERROR
ret = H5Pset_layout(dcpl_minimize, H5D_COMPACT); ret = H5Pset_layout(dcpl_minimize, H5D_COMPACT);
if(ret < 0) TEST_ERROR if(ret < 0) TEST_ERROR
ret = H5Pset_layout(dcpl_dontmin, H5D_COMPACT); ret = H5Pset_layout(dcpl_dontmin, H5D_COMPACT);
if(ret < 0) TEST_ERROR if(ret < 0) TEST_ERROR
} else }
HDprintf("...not compact ");
dspace_id = H5Screate_simple(1, array_10, NULL); dspace_id = H5Screate_simple(1, array_10, NULL);
if(dspace_id < 0) TEST_ERROR if(dspace_id < 0) TEST_ERROR
@ -1191,9 +1138,10 @@ test_minimized_dset_ohdr_size_comparisons(hid_t fapl_id)
if(H5Dclose(dset_F_N_id) < 0) TEST_ERROR if(H5Dclose(dset_F_N_id) < 0) TEST_ERROR
if(H5Dclose(dset_F_Y_id) < 0) TEST_ERROR if(H5Dclose(dset_F_Y_id) < 0) TEST_ERROR
PASSED()
} /* compact and non-compact */ } /* compact and non-compact */
PASSED()
return SUCCEED; return SUCCEED;
error : error :