From fa7caf843508b250f085e88cf5edfc2606da1205 Mon Sep 17 00:00:00 2001 From: Dana Robinson <43805+derobins@users.noreply.github.com> Date: Mon, 11 Jul 2022 08:59:51 -0700 Subject: Fixes C++ sign-conversion warnings w/ clang 14 (#1870) * Fixes sign-conversion warnings w/ clang 14 * Committing clang-format changes Co-authored-by: github-actions <41898282+github-actions[bot]@users.noreply.github.com> --- c++/src/H5ArrayType.cpp | 5 ++++- c++/src/H5Attribute.cpp | 19 ++++++++++++------- c++/src/H5DataSet.cpp | 6 ++++-- c++/src/H5DcreatProp.cpp | 19 +++++++++++++------ c++/src/H5DxferProp.cpp | 9 +++++++-- c++/src/H5Exception.cpp | 18 ++++++++++++++---- c++/src/H5IdComponent.cpp | 11 ++++++++--- c++/src/H5Location.cpp | 12 ++++++++---- c++/src/H5Object.cpp | 16 +++++++++++----- c++/test/dsets.cpp | 3 ++- c++/test/tattr.cpp | 7 +++---- c++/test/tdspl.cpp | 8 ++++---- c++/test/trefer.cpp | 26 ++++++++++++++------------ c++/test/ttypes.cpp | 2 +- c++/test/tvlstr.cpp | 6 +++--- 15 files changed, 108 insertions(+), 59 deletions(-) diff --git a/c++/src/H5ArrayType.cpp b/c++/src/H5ArrayType.cpp index 6999f1b..953c355 100644 --- a/c++/src/H5ArrayType.cpp +++ b/c++/src/H5ArrayType.cpp @@ -70,8 +70,11 @@ ArrayType::ArrayType(const ArrayType &original) : DataType(original) //-------------------------------------------------------------------------- ArrayType::ArrayType(const DataType &base_type, int ndims, const hsize_t *dims) : DataType() { + if (ndims < 0 || ndims > H5S_MAX_RANK) + throw DataTypeIException("ArrayType constructor", "ndims not in range [0..H5S_MAX_RANK]"); + // Call C API to create an array data type - hid_t new_type_id = H5Tarray_create2(base_type.getId(), ndims, dims); + hid_t new_type_id = H5Tarray_create2(base_type.getId(), static_cast(ndims), dims); if (new_type_id < 0) throw DataTypeIException("ArrayType constructor", "H5Tarray_create2 failed"); diff --git a/c++/src/H5Attribute.cpp b/c++/src/H5Attribute.cpp index a0aa33f..c9490ee 100644 --- a/c++/src/H5Attribute.cpp +++ b/c++/src/H5Attribute.cpp @@ -235,8 +235,9 @@ Attribute::getInMemDataSize() const } // Calculate and return the size of the data - size_t data_size = type_size * num_elements; - return (data_size); + size_t data_size = type_size * static_cast(num_elements); + + return data_size; } //-------------------------------------------------------------------------- @@ -324,11 +325,15 @@ Attribute::getName() const } // Attribute's name exists, retrieve it else if (name_size > 0) { + // The actual size is the cast value + 1 for the terminal ASCII NUL + // (unfortunate in/out type sign mismatch) + size_t actual_name_size = static_cast(name_size) + 1; + // Create buffer for C string - char *name_C = new char[name_size + 1](); + char *name_C = new char[actual_name_size](); // Use overloaded function - name_size = getName(name_C, name_size + 1); + name_size = getName(name_C, actual_name_size); // Convert the C attribute name to return attr_name = name_C; @@ -338,7 +343,7 @@ Attribute::getName() const } // Return attribute's name - return (attr_name); + return attr_name; } //-------------------------------------------------------------------------- @@ -390,7 +395,7 @@ Attribute::getName(H5std_string &attr_name, size_t len) const // If no length is provided, get the entire attribute name if (len == 0) { attr_name = getName(); - name_size = attr_name.length(); + name_size = static_cast(attr_name.length()); } // If length is provided, get that number of characters in name else { @@ -409,7 +414,7 @@ Attribute::getName(H5std_string &attr_name, size_t len) const // Otherwise, keep attr_name intact // Return name size - return (name_size); + return name_size; } //-------------------------------------------------------------------------- diff --git a/c++/src/H5DataSet.cpp b/c++/src/H5DataSet.cpp index 68ddefa..3ee0ec5 100644 --- a/c++/src/H5DataSet.cpp +++ b/c++/src/H5DataSet.cpp @@ -268,8 +268,10 @@ DataSet::getInMemDataSize() const } // Calculate and return the size of the data - size_t data_size = type_size * num_elements; - return (data_size); + // Note that large datasets can overflow a size_t + size_t data_size = type_size * static_cast(num_elements); + + return data_size; } //-------------------------------------------------------------------------- diff --git a/c++/src/H5DcreatProp.cpp b/c++/src/H5DcreatProp.cpp index da19d8d..97c6ddb 100644 --- a/c++/src/H5DcreatProp.cpp +++ b/c++/src/H5DcreatProp.cpp @@ -202,7 +202,7 @@ DSetCreatPropList::getLayout() const if (layout == H5D_LAYOUT_ERROR) { throw PropListIException("DSetCreatPropList::getLayout", "H5Pget_layout returns H5D_LAYOUT_ERROR"); } - return (layout); + return layout; } //-------------------------------------------------------------------------- @@ -220,7 +220,12 @@ DSetCreatPropList::getLayout() const void DSetCreatPropList::setDeflate(int level) const { - herr_t ret_value = H5Pset_deflate(id, level); + if (level < 0) { + throw PropListIException("DSetCreatPropList::setDeflate", "level can't be negative"); + } + + herr_t ret_value = H5Pset_deflate(id, static_cast(level)); + if (ret_value < 0) { throw PropListIException("DSetCreatPropList::setDeflate", "H5Pset_deflate failed"); } @@ -436,13 +441,15 @@ DSetCreatPropList::getFilter(int filter_number, unsigned int &flags, size_t &cd_ unsigned int *cd_values, size_t namelen, char name[], unsigned int &filter_config) const { - H5Z_filter_t filter_id; - filter_id = - H5Pget_filter2(id, filter_number, &flags, &cd_nelmts, cd_values, namelen, name, &filter_config); + if (filter_number < 0) + throw PropListIException("DSetCreatPropList::getFilter", "filter_number can't be negative"); + + H5Z_filter_t filter_id = H5Pget_filter2(id, static_cast(filter_number), &flags, &cd_nelmts, + cd_values, namelen, name, &filter_config); if (filter_id == H5Z_FILTER_ERROR) throw PropListIException("DSetCreatPropList::getFilter", "H5Pget_filter2 returned H5Z_FILTER_ERROR"); else - return (filter_id); + return filter_id; } //-------------------------------------------------------------------------- diff --git a/c++/src/H5DxferProp.cpp b/c++/src/H5DxferProp.cpp index 1fd6878..ccb49be 100644 --- a/c++/src/H5DxferProp.cpp +++ b/c++/src/H5DxferProp.cpp @@ -322,11 +322,16 @@ DSetMemXferPropList::getDataTransform() const // If expression exists, calls C routine again to get it else if (exp_len > 0) { + + // The actual size is the cast value + 1 for the terminal ASCII NUL + // (unfortunate in/out type sign mismatch) + size_t actual_exp_len = static_cast(exp_len) + 1; + // Temporary buffer for char* expression - char *exp_C = new char[exp_len + 1](); + char *exp_C = new char[actual_exp_len](); // Used overloaded function - exp_len = getDataTransform(exp_C, exp_len + 1); + exp_len = getDataTransform(exp_C, actual_exp_len); // Convert the C expression to return expression = exp_C; diff --git a/c++/src/H5Exception.cpp b/c++/src/H5Exception.cpp index a42c151..5687514 100644 --- a/c++/src/H5Exception.cpp +++ b/c++/src/H5Exception.cpp @@ -73,9 +73,14 @@ Exception::getMajorString(hid_t err_major) const if (mesg_size < 0) throw IdComponentException("Exception::getMajorString", "H5Eget_msg failed"); + // The actual message size is the cast value + 1 for the terminal ASCII NUL + // (unfortunate in/out type sign mismatch) + size_t actual_mesg_size = static_cast(mesg_size) + 1; + // Call H5Eget_msg again to get the actual message - char *mesg_C = new char[mesg_size + 1]; // temporary C-string for C API - mesg_size = H5Eget_msg(err_major, NULL, mesg_C, mesg_size + 1); + char *mesg_C = new char[actual_mesg_size]; // temporary C-string for C API + + mesg_size = H5Eget_msg(err_major, NULL, mesg_C, actual_mesg_size); // Check for failure again if (mesg_size < 0) { @@ -110,9 +115,14 @@ Exception::getMinorString(hid_t err_minor) const if (mesg_size < 0) throw IdComponentException("Exception::getMinorString", "H5Eget_msg failed"); + // The actual message size is the cast value + 1 for the terminal ASCII NUL + // (unfortunate in/out type sign mismatch) + size_t actual_mesg_size = static_cast(mesg_size) + 1; + // Call H5Eget_msg again to get the actual message - char *mesg_C = new char[mesg_size + 1]; // temporary C-string for C API - mesg_size = H5Eget_msg(err_minor, NULL, mesg_C, mesg_size + 1); + char *mesg_C = new char[actual_mesg_size]; // temporary C-string for C API + + mesg_size = H5Eget_msg(err_minor, NULL, mesg_C, actual_mesg_size); // Check for failure again if (mesg_size < 0) { diff --git a/c++/src/H5IdComponent.cpp b/c++/src/H5IdComponent.cpp index e1fc687..ce52fb0 100644 --- a/c++/src/H5IdComponent.cpp +++ b/c++/src/H5IdComponent.cpp @@ -397,10 +397,14 @@ IdComponent::p_get_file_name() const throw IdComponentException("", "H5Fget_name failed"); } + // The actual message size is the cast value + 1 for the terminal ASCII NUL + // (unfortunate in/out type sign mismatch) + size_t actual_name_size = static_cast(name_size) + 1; + // Call H5Fget_name again to get the actual file name - char *name_C = new char[name_size + 1](); + char *name_C = new char[actual_name_size](); - name_size = H5Fget_name(temp_id, name_C, name_size + 1); + name_size = H5Fget_name(temp_id, name_C, actual_name_size); // Check for failure again if (name_size < 0) { @@ -411,7 +415,8 @@ IdComponent::p_get_file_name() const // Convert the C file name and return H5std_string file_name(name_C); delete[] name_C; - return (file_name); + + return file_name; } // diff --git a/c++/src/H5Location.cpp b/c++/src/H5Location.cpp index 915f2a9..e128303 100644 --- a/c++/src/H5Location.cpp +++ b/c++/src/H5Location.cpp @@ -363,7 +363,7 @@ H5Location::getComment(const char *name, size_t buf_size) const // If buffer size is not provided, use comment length if (tmp_len == 0) - tmp_len = comment_len; + tmp_len = static_cast(comment_len); // Temporary buffer for char* comment char *comment_C = new char[tmp_len + 1](); @@ -2044,11 +2044,15 @@ H5Location::getObjnameByIdx(hsize_t idx) const if (name_len < 0) throwException("getObjnameByIdx", "H5Lget_name_by_idx failed"); + // The actual size is the cast value + 1 for the terminal ASCII NUL + // (unfortunate in/out type sign mismatch) + size_t actual_name_len = static_cast(name_len) + 1; + // Create buffer for C string - char *name_C = new char[name_len + 1](); + char *name_C = new char[actual_name_len](); - name_len = - H5Lget_name_by_idx(getId(), ".", H5_INDEX_NAME, H5_ITER_INC, idx, name_C, name_len + 1, H5P_DEFAULT); + name_len = H5Lget_name_by_idx(getId(), ".", H5_INDEX_NAME, H5_ITER_INC, idx, name_C, actual_name_len, + H5P_DEFAULT); if (name_len < 0) { delete[] name_C; diff --git a/c++/src/H5Object.cpp b/c++/src/H5Object.cpp index cb803af..d092ef0 100644 --- a/c++/src/H5Object.cpp +++ b/c++/src/H5Object.cpp @@ -497,11 +497,16 @@ H5Object::getObjName() const } // Object's name exists, retrieve it else if (name_size > 0) { + + // The actual size is the cast value + 1 for the terminal ASCII NUL + // (unfortunate in/out type sign mismatch) + size_t actual_name_size = static_cast(name_size) + 1; + // Create buffer for C string - char *name_C = new char[name_size + 1](); + char *name_C = new char[actual_name_size](); // Use overloaded function - name_size = getObjName(name_C, name_size + 1); + name_size = getObjName(name_C, actual_name_size); // Convert the C object name to return obj_name = name_C; @@ -509,8 +514,9 @@ H5Object::getObjName() const // Clean up resource delete[] name_C; } + // Return object's name - return (obj_name); + return obj_name; } //-------------------------------------------------------------------------- @@ -534,7 +540,7 @@ H5Object::getObjName(H5std_string &obj_name, size_t len) const // If no length is provided, get the entire object name if (len == 0) { obj_name = getObjName(); - name_size = obj_name.length(); + name_size = static_cast(obj_name.length()); } // If length is provided, get that number of characters in name else { @@ -553,7 +559,7 @@ H5Object::getObjName(H5std_string &obj_name, size_t len) const // Otherwise, keep obj_name intact // Return name size - return (name_size); + return name_size; } #ifndef DOXYGEN_SHOULD_SKIP_THIS diff --git a/c++/test/dsets.cpp b/c++/test/dsets.cpp index 6e170bf..432abb8 100644 --- a/c++/test/dsets.cpp +++ b/c++/test/dsets.cpp @@ -228,7 +228,8 @@ test_simple_io(H5File &file) // Check that the values read are the same as the values written for (i = 0; i < 100; i++) for (j = 0; j < 200; j++) { - int status = check_values(i, j, points[i][j], check[i][j]); + int status = + check_values(static_cast(i), static_cast(j), points[i][j], check[i][j]); if (status == -1) throw Exception("DataSet::read"); } diff --git a/c++/test/tattr.cpp b/c++/test/tattr.cpp index dc968f9..0a1402c 100644 --- a/c++/test/tattr.cpp +++ b/c++/test/tattr.cpp @@ -727,13 +727,12 @@ test_attr_compound_read() // Verify that the fields have the same names as when the type // was created - int j; - for (j = 0; j < fields; j++) { - H5std_string fieldname = datatype.getMemberName(j); + for (int j = 0; j < fields; j++) { + H5std_string fieldname = datatype.getMemberName(static_cast(j)); if (!((fieldname == ATTR4_FIELDNAME1) || (fieldname == ATTR4_FIELDNAME2) || (fieldname == ATTR4_FIELDNAME3))) TestErrPrintf("%d:invalid field name for field #%d: %s\n", __LINE__, j, fieldname.c_str()); - } /* end for */ + } offset = datatype.getMemberOffset(0); verify_val(offset, attr4_field1_off, "DataType::getMemberOffset", __LINE__, __FILE__); diff --git a/c++/test/tdspl.cpp b/c++/test/tdspl.cpp index 5a78133..d692a5b 100644 --- a/c++/test/tdspl.cpp +++ b/c++/test/tdspl.cpp @@ -60,8 +60,8 @@ test_transfplist() // Find out the length of the transform expression, allocate the buffer // for it, then read and verify the expression from the copied plist - ssize_t tran_len = dxpl_c_to_f_copy.getDataTransform(NULL); - char * c_to_f_read = static_cast(HDmalloc(tran_len + 1)); + size_t tran_len = static_cast(dxpl_c_to_f_copy.getDataTransform(NULL)); + char * c_to_f_read = static_cast(HDmalloc(tran_len + 1)); HDmemset(c_to_f_read, 0, tran_len + 1); dxpl_c_to_f_copy.getDataTransform(c_to_f_read, tran_len + 1); verify_val(const_cast(c_to_f_read), const_cast(c_to_f), @@ -75,7 +75,7 @@ test_transfplist() // Get and verify the expression with: // ssize_t getDataTransform(char* exp, const size_t buf_size [default=0]) - tran_len = dxpl_c_to_f.getDataTransform(NULL); + tran_len = static_cast(dxpl_c_to_f.getDataTransform(NULL)); c_to_f_read = static_cast(HDmalloc(tran_len + 1)); HDmemset(c_to_f_read, 0, tran_len + 1); dxpl_c_to_f.getDataTransform(c_to_f_read, tran_len + 1); @@ -91,7 +91,7 @@ test_transfplist() // Get and verify the expression with: // ssize_t getDataTransform(char* exp, const size_t buf_size) - tran_len = dxpl_utrans_inv.getDataTransform(NULL, 0); + tran_len = static_cast(dxpl_utrans_inv.getDataTransform(NULL, 0)); char *utrans_inv_read = static_cast(HDmalloc(tran_len + 1)); HDmemset(utrans_inv_read, 0, tran_len + 1); dxpl_utrans_inv.getDataTransform(utrans_inv_read, tran_len + 1); diff --git a/c++/test/trefer.cpp b/c++/test/trefer.cpp index 66fa040..2a0fe1e 100644 --- a/c++/test/trefer.cpp +++ b/c++/test/trefer.cpp @@ -80,10 +80,10 @@ test_reference_params() *tbuf; // temp. buffer read from disk // Allocate write & read buffers - int temp_size = MAX(sizeof(unsigned), sizeof(hobj_ref_t)); - wbuf = static_cast(HDmalloc(temp_size * SPACE1_DIM1)); - rbuf = static_cast(HDmalloc(temp_size * SPACE1_DIM1)); - tbuf = static_cast(HDmalloc(temp_size * SPACE1_DIM1)); + size_t temp_size = MAX(sizeof(unsigned), sizeof(hobj_ref_t)); + wbuf = static_cast(HDmalloc(temp_size * SPACE1_DIM1)); + rbuf = static_cast(HDmalloc(temp_size * SPACE1_DIM1)); + tbuf = static_cast(HDmalloc(temp_size * SPACE1_DIM1)); // Create file FILE1 file1 = new H5File(FILE1, H5F_ACC_TRUNC); @@ -102,7 +102,7 @@ test_reference_params() DataSet dataset = group.createDataSet(DSET1_NAME, PredType::NATIVE_UINT, sid1); unsigned *tu32; // Temporary pointer to uint32 data - int i; + unsigned i; for (tu32 = reinterpret_cast(wbuf), i = 0; i < SPACE1_DIM1; i++) *tu32++ = i * 3; // from C test @@ -194,7 +194,6 @@ test_reference_params() static void test_reference_obj() { - int i; // counting variables const H5std_string write_comment = "Foo!"; // Comments for group // Output message about test being performed @@ -207,10 +206,10 @@ test_reference_obj() *tbuf; // temp. buffer read from disk // Allocate write & read buffers - int temp_size = MAX(sizeof(unsigned), sizeof(hobj_ref_t)); - wbuf = static_cast(HDmalloc(temp_size * SPACE1_DIM1)); - rbuf = static_cast(HDmalloc(temp_size * SPACE1_DIM1)); - tbuf = static_cast(HDmalloc(temp_size * SPACE1_DIM1)); + size_t temp_size = MAX(sizeof(unsigned), sizeof(hobj_ref_t)); + wbuf = static_cast(HDmalloc(temp_size * SPACE1_DIM1)); + rbuf = static_cast(HDmalloc(temp_size * SPACE1_DIM1)); + tbuf = static_cast(HDmalloc(temp_size * SPACE1_DIM1)); // Create file FILE1 file1 = new H5File(FILE1, H5F_ACC_TRUNC); @@ -232,6 +231,7 @@ test_reference_obj() DataSet dataset = group.createDataSet(DSET1_NAME, PredType::NATIVE_UINT, sid1); unsigned *tu32; // Temporary pointer to uint32 data + unsigned i; for (tu32 = reinterpret_cast(wbuf), i = 0; i < SPACE1_DIM1; i++) *tu32++ = i * 3; // from C test @@ -714,7 +714,8 @@ test_reference_region_1D() verify_val(static_cast(nelms), 15, "DataSpace::getSelectNpoints", __LINE__, __FILE__); /* Allocate space for the hyperslab blocks */ - coords = static_cast(HDmalloc(nelms * SPACE3_RANK * sizeof(hsize_t) * 2)); + coords = + static_cast(HDmalloc(static_cast(nelms) * SPACE3_RANK * sizeof(hsize_t) * 2)); // Get the list of hyperslab blocks currently selected reg_sp.getSelectHyperBlocklist(0, static_cast(nelms), coords); @@ -773,7 +774,8 @@ test_reference_region_1D() verify_val(static_cast(nelmspts), 10, "DataSpace::getSelectNpoints", __LINE__, __FILE__); /* Allocate space for the hyperslab blocks */ - coords = static_cast(HDmalloc(nelmspts * SPACE3_RANK * sizeof(hsize_t))); + coords = + static_cast(HDmalloc(static_cast(nelmspts) * SPACE3_RANK * sizeof(hsize_t))); // Get the list of element points currently selected elm_sp.getSelectElemPointlist(0, static_cast(nelmspts), coords); diff --git a/c++/test/ttypes.cpp b/c++/test/ttypes.cpp index 794820e..db32cc0 100644 --- a/c++/test/ttypes.cpp +++ b/c++/test/ttypes.cpp @@ -241,7 +241,7 @@ test_detect_type_class() */ // Create an array datatype with an atomic base type - unsigned rank = 2; // Rank for array datatype + int rank = 2; // Rank for array datatype hsize_t dims[2] = {3, 3}; // Dimensions for array datatype ArrayType atom_arr(PredType::STD_REF_OBJ, rank, dims); diff --git a/c++/test/tvlstr.cpp b/c++/test/tvlstr.cpp index 405ca07..6ace763 100644 --- a/c++/test/tvlstr.cpp +++ b/c++/test/tvlstr.cpp @@ -360,7 +360,7 @@ test_vlstrings_special() hsize_t ii; // counting variable for (ii = 0; ii < SPACE1_DIM1; ii++) if (rdata[ii] != NULL) - TestErrPrintf("VL doesn't match!, rdata[%d]=%p\n", static_cast(ii), rdata[ii]); + TestErrPrintf("VL doesn't match!, rdata[%d]=%s\n", static_cast(ii), rdata[ii]); // Write dataset to disk, then read it back. dataset.write(wdata, vlst); @@ -409,7 +409,7 @@ test_vlstrings_special() // Check data read in. for (ii = 0; ii < SPACE1_DIM1; ii++) if (rdata[ii] != NULL) - TestErrPrintf("VL doesn't match!, rdata[%d]=%p\n", static_cast(ii), rdata[ii]); + TestErrPrintf("VL doesn't match!, rdata[%d]=%s\n", static_cast(ii), rdata[ii]); // Try to write nil strings to disk. dataset.write(wdata2, vlst); @@ -420,7 +420,7 @@ test_vlstrings_special() // Check data read in. for (ii = 0; ii < SPACE1_DIM1; ii++) if (rdata[ii] != NULL) - TestErrPrintf("VL doesn't match!, rdata[%d]=%p\n", static_cast(ii), rdata[ii]); + TestErrPrintf("VL doesn't match!, rdata[%d]=%s\n", static_cast(ii), rdata[ii]); // Close objects and file. dataset.close(); -- cgit v0.12