From 20ea68f5c4ea9d2e7708b5b4a9f6f2ba8d94e558 Mon Sep 17 00:00:00 2001 From: Quincey Koziol Date: Tue, 31 Aug 2010 17:59:49 -0500 Subject: [svn-r19328] Description: Add some extra range checking for invalid chunk sizes. Tweak default chunk sizes to be zero, so that it's more likely that application developers will be forced to set them correctly. Add a few tests to verify these checks. Tested on: Mac OS X/32 10.6.4 (amazon) w/debug & production (too minor to require h5committest) --- src/H5Dchunk.c | 11 ++++++++++- src/H5Pdcpl.c | 2 +- test/dsets.c | 44 +++++++++++++++++++++++++++++++++----------- 3 files changed, 44 insertions(+), 13 deletions(-) diff --git a/src/H5Dchunk.c b/src/H5Dchunk.c index 88c4ab9..5768da3 100644 --- a/src/H5Dchunk.c +++ b/src/H5Dchunk.c @@ -384,6 +384,10 @@ H5D_chunk_construct(H5F_t UNUSED *f, H5D_t *dset) HDassert(f); HDassert(dset); + /* Check for invalid chunk dimension rank */ + if(0 == dset->shared->layout.u.chunk.ndims) + HGOTO_ERROR(H5E_DATASET, H5E_BADVALUE, FAIL, "no chunk information set?") + /* Set up layout information */ if((ndims = H5S_GET_EXTENT_NDIMS(dset->shared->space)) < 0) HGOTO_ERROR(H5E_DATASET, H5E_CANTGET, FAIL, "unable to get rank") @@ -406,13 +410,18 @@ H5D_chunk_construct(H5F_t UNUSED *f, H5D_t *dset) HGOTO_ERROR(H5E_DATASET, H5E_CANTINIT, FAIL, "unable to query maximum dimensions") /* Sanity check dimensions */ - for(u = 0; u < dset->shared->layout.u.chunk.ndims - 1; u++) + for(u = 0; u < dset->shared->layout.u.chunk.ndims - 1; u++) { + /* Don't allow zero-sized chunk dimensions */ + if(0 == dset->shared->layout.u.chunk.dim[u]) + HGOTO_ERROR(H5E_DATASET, H5E_CANTINIT, FAIL, "chunk size must be > 0, dim = %u ", u) + /* * The chunk size of a dimension with a fixed size cannot exceed * the maximum dimension size */ if(max_dim[u] != H5S_UNLIMITED && max_dim[u] < dset->shared->layout.u.chunk.dim[u]) HGOTO_ERROR(H5E_DATASET, H5E_CANTINIT, FAIL, "chunk size must be <= maximum dimension size for fixed-sized dimensions") + } /* end for */ /* Compute the total size of a chunk */ /* (Use 64-bit value to ensure that we can detect >4GB chunks) */ diff --git a/src/H5Pdcpl.c b/src/H5Pdcpl.c index 6fe1b39..6ceed64 100644 --- a/src/H5Pdcpl.c +++ b/src/H5Pdcpl.c @@ -53,7 +53,7 @@ #define H5D_DEF_STORAGE_COMPACT_INIT {(hbool_t)FALSE, (size_t)0, NULL} #define H5D_DEF_STORAGE_CONTIG_INIT {HADDR_UNDEF, (hsize_t)0} #define H5D_DEF_STORAGE_CHUNK_INIT {H5D_CHUNK_BTREE, HADDR_UNDEF, NULL, {{NULL}}} -#define H5D_DEF_LAYOUT_CHUNK_INIT {(unsigned)1, {1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1}, (uint32_t)0, (hsize_t)0, {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}, {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}} +#define H5D_DEF_LAYOUT_CHUNK_INIT {(unsigned)0, {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}, (uint32_t)0, (hsize_t)0, {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}, {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}} #ifdef H5_HAVE_C99_DESIGNATED_INITIALIZER #define H5D_DEF_STORAGE_COMPACT {H5D_COMPACT, { .compact = H5D_DEF_STORAGE_COMPACT_INIT }} #define H5D_DEF_STORAGE_CONTIG {H5D_CONTIGUOUS, { .contig = H5D_DEF_STORAGE_CONTIG_INIT }} diff --git a/test/dsets.c b/test/dsets.c index 8a474b4..e686fa0 100644 --- a/test/dsets.c +++ b/test/dsets.c @@ -876,6 +876,7 @@ test_layout_extend(hid_t fapl) hsize_t cur_size[1] = {10}; /* Current size of dataspace */ hsize_t max_unlim[1] = {H5S_UNLIMITED}; /* Maximum size of dataspace (unlimited) */ hsize_t max_fix[1] = {100}; /* Maximum size of dataspace (fixed) */ + hsize_t chunk_dim[1] = {10}; /* Chunk size */ TESTING("extendible dataset with various layout"); @@ -931,6 +932,7 @@ test_layout_extend(hid_t fapl) FAIL_STACK_ERROR if(H5Pset_layout(dcpl_chunked, H5D_CHUNKED) < 0) FAIL_STACK_ERROR + if(H5Pset_chunk(dcpl_chunked, 1, chunk_dim) < 0) FAIL_STACK_ERROR /* Create dataset with extendible dataspace (fixed max_dims) should succeed */ if((did_fixed = H5Dcreate2(fid, "chunked_fixed", H5T_NATIVE_INT, sid_fix, H5P_DEFAULT, dcpl_chunked, H5P_DEFAULT)) < 0) @@ -6326,8 +6328,6 @@ error: * Programmer: Quincey Koziol * Tuesday, July 27, 2004 * - * Modifications: - * *------------------------------------------------------------------------- */ static herr_t @@ -6335,19 +6335,41 @@ test_zero_dims(hid_t file) { hid_t s=-1, d=-1, dcpl=-1; hsize_t dsize=0, dmax=H5S_UNLIMITED, csize=5; + herr_t ret; TESTING("I/O on datasets with zero-sized dims"); - if((s = H5Screate_simple(1, &dsize, &dmax)) < 0) TEST_ERROR; - if((dcpl = H5Pcreate(H5P_DATASET_CREATE)) < 0) TEST_ERROR; - if(H5Pset_chunk(dcpl, 1, &csize) < 0) TEST_ERROR; - if((d = H5Dcreate2(file, ZERODIM_DATASET, H5T_NATIVE_INT, s, H5P_DEFAULT, dcpl, H5P_DEFAULT)) < 0) TEST_ERROR; + if((s = H5Screate_simple(1, &dsize, &dmax)) < 0) FAIL_STACK_ERROR - if(H5Dwrite(d, H5T_NATIVE_INT, s, s, H5P_DEFAULT, (void*)911) < 0) TEST_ERROR; + /* Try creating chunked dataset with zero-sized chunk dimensions */ + if((dcpl = H5Pcreate(H5P_DATASET_CREATE)) < 0) FAIL_STACK_ERROR + if(H5Pset_layout(dcpl, H5D_CHUNKED) < 0) FAIL_STACK_ERROR + H5E_BEGIN_TRY { + d = H5Dcreate2(file, ZERODIM_DATASET, H5T_NATIVE_INT, s, H5P_DEFAULT, dcpl, H5P_DEFAULT); + } H5E_END_TRY; + if(d > 0) { + H5Dclose(d); + FAIL_PUTS_ERROR("created dataset with undefined chunk dimensions") + } /* end if */ - if(H5Pclose(dcpl) < 0) TEST_ERROR; - if(H5Sclose(s) < 0) TEST_ERROR; - if(H5Dclose(d) < 0) TEST_ERROR; + H5E_BEGIN_TRY { + ret = H5Pset_chunk(dcpl, 1, &dsize); + } H5E_END_TRY; + if(ret > 0) + FAIL_PUTS_ERROR("set zero-sized chunk dimensions") + + if(H5Pclose(dcpl) < 0) FAIL_STACK_ERROR + + if((dcpl = H5Pcreate(H5P_DATASET_CREATE)) < 0) FAIL_STACK_ERROR + if(H5Pset_chunk(dcpl, 1, &csize) < 0) FAIL_STACK_ERROR + if((d = H5Dcreate2(file, ZERODIM_DATASET, H5T_NATIVE_INT, s, H5P_DEFAULT, dcpl, H5P_DEFAULT)) < 0) FAIL_STACK_ERROR + + if(H5Dwrite(d, H5T_NATIVE_INT, s, s, H5P_DEFAULT, (void*)911) < 0) FAIL_STACK_ERROR + + if(H5Dclose(d) < 0) FAIL_STACK_ERROR + if(H5Pclose(dcpl) < 0) FAIL_STACK_ERROR + + if(H5Sclose(s) < 0) FAIL_STACK_ERROR PASSED(); return 0; @@ -6460,7 +6482,7 @@ error: H5Sclose(s); } H5E_END_TRY; return -1; -} /* end test_zero_dims() */ +} /* end test_missing_chunk() */ /*------------------------------------------------------------------------- -- cgit v0.12