diff options
author | Dana Robinson <43805+derobins@users.noreply.github.com> | 2022-07-11 15:59:51 (GMT) |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-07-11 15:59:51 (GMT) |
commit | fa7caf843508b250f085e88cf5edfc2606da1205 (patch) | |
tree | 8ecc6f13d62952ab8ee49ea59bae9c2a47684e9b /c++/src | |
parent | f599e2ac7fb7fb146b49e2f349a9c100e897f7ef (diff) | |
download | hdf5-fa7caf843508b250f085e88cf5edfc2606da1205.zip hdf5-fa7caf843508b250f085e88cf5edfc2606da1205.tar.gz hdf5-fa7caf843508b250f085e88cf5edfc2606da1205.tar.bz2 |
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>
Diffstat (limited to 'c++/src')
-rw-r--r-- | c++/src/H5ArrayType.cpp | 5 | ||||
-rw-r--r-- | c++/src/H5Attribute.cpp | 19 | ||||
-rw-r--r-- | c++/src/H5DataSet.cpp | 6 | ||||
-rw-r--r-- | c++/src/H5DcreatProp.cpp | 19 | ||||
-rw-r--r-- | c++/src/H5DxferProp.cpp | 9 | ||||
-rw-r--r-- | c++/src/H5Exception.cpp | 18 | ||||
-rw-r--r-- | c++/src/H5IdComponent.cpp | 11 | ||||
-rw-r--r-- | c++/src/H5Location.cpp | 12 | ||||
-rw-r--r-- | c++/src/H5Object.cpp | 16 |
9 files changed, 81 insertions, 34 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<unsigned>(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<size_t>(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<size_t>(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<ssize_t>(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<size_t>(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<unsigned>(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<unsigned>(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<size_t>(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<size_t>(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<size_t>(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<size_t>(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<size_t>(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<size_t>(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<size_t>(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<ssize_t>(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 |