summaryrefslogtreecommitdiffstats
path: root/tools/test
diff options
context:
space:
mode:
authorDana Robinson <43805+derobins@users.noreply.github.com>2022-07-18 14:35:43 (GMT)
committerGitHub <noreply@github.com>2022-07-18 14:35:43 (GMT)
commit079df9d216b61b38b0082a3c77472e1546d24ddb (patch)
tree68075e93fe68df4d440dc15f4952390e5e46373b /tools/test
parent08b797de73f0ff2ed8bfa3904b42064b26373e96 (diff)
downloadhdf5-079df9d216b61b38b0082a3c77472e1546d24ddb.zip
hdf5-079df9d216b61b38b0082a3c77472e1546d24ddb.tar.gz
hdf5-079df9d216b61b38b0082a3c77472e1546d24ddb.tar.bz2
Fixes a stack overflow in the h5dump test generator (#1903)
* Fixes a stack overflow in the h5dump test generator The opaque type size was declared to be the size of the "buffer of two opaque elements", causing a memcpy call in H5Dwrite to read outside the buffer, dumping garbage into the file. Note that this only affected the test generator and not h5dump itself. * Committing clang-format changes Co-authored-by: github-actions <41898282+github-actions[bot]@users.noreply.github.com>
Diffstat (limited to 'tools/test')
-rw-r--r--tools/test/h5dump/h5dumpgentest.c51
1 files changed, 26 insertions, 25 deletions
diff --git a/tools/test/h5dump/h5dumpgentest.c b/tools/test/h5dump/h5dumpgentest.c
index 949aa43..caacc2c 100644
--- a/tools/test/h5dump/h5dumpgentest.c
+++ b/tools/test/h5dump/h5dumpgentest.c
@@ -2570,41 +2570,42 @@ gent_nestcomp(void)
static void
gent_opaque(void)
{
- hid_t file, type, dataset, space;
- char test[100][2];
- int x;
- hsize_t dim = 2;
+ hid_t file = H5I_INVALID_HID;
+ hid_t type = H5I_INVALID_HID;
+ hid_t dataset = H5I_INVALID_HID;
+ hid_t space = H5I_INVALID_HID;
- for (x = 0; x < 100; x++) {
- test[x][0] = (char)x;
- test[x][1] = (char)(99 - x);
- }
+ const uint8_t OPAQUE_NBYTES = 100;
+ const int N_ELEMENTS = 2;
- /*
- * Create the data space.
- */
- space = H5Screate_simple(1, &dim, NULL);
+ /* The dataset contains N_ELEMENTS elements of OPAQUE_NBYTES bytes */
+ uint8_t data[OPAQUE_NBYTES][N_ELEMENTS];
+ hsize_t dim = N_ELEMENTS;
- /*
- * Create the file.
- */
file = H5Fcreate(FILE19, H5F_ACC_TRUNC, H5P_DEFAULT, H5P_DEFAULT);
- /*
- * Create the memory datatype.
- */
- type = H5Tcreate(H5T_OPAQUE, sizeof(char) * 100 * 2);
+ /* The opaque datatype is OPAQUE_NBYTES bytes in size */
+ type = H5Tcreate(H5T_OPAQUE, sizeof(uint8_t) * OPAQUE_NBYTES);
H5Tset_tag(type, "test opaque type");
- /*
- * Create the dataset.
- */
+ space = H5Screate_simple(1, &dim, NULL);
dataset = H5Dcreate2(file, "opaque test", type, space, H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT);
- /*
- * Write data to the dataset;
+ /* Given the data fill algorithm, make sure that the number of bytes
+ * in the opaque type isn't so big that i or (OPAQUE_NBYTES - 1) - i
+ * don't fit in a uint8_t value..
*/
- H5Dwrite(dataset, type, H5S_ALL, H5S_ALL, H5P_DEFAULT, test);
+ HDcompile_assert(OPAQUE_NBYTES < UINT8_MAX);
+
+ /* Write out two opaque data elements with predictable data to
+ * the file.
+ */
+ for (uint8_t i = 0; i < OPAQUE_NBYTES; i++) {
+ data[i][0] = i;
+ data[i][1] = (OPAQUE_NBYTES - 1) - i;
+ }
+
+ H5Dwrite(dataset, type, H5S_ALL, H5S_ALL, H5P_DEFAULT, data);
H5Tclose(type);
H5Sclose(space);