From 10f1e6acf8916a429f2cc925f49518cfc0015b6c Mon Sep 17 00:00:00 2001 From: Binh-Minh Ribler Date: Tue, 30 Sep 2014 13:20:36 -0500 Subject: [svn-r25640] Purpose: Fixed HDFFV-4259 Description: - Used H5I_INVALID_HID instead of 0 to initialized member "id" in classes that represent HDF5 objects. For PropList, H5P_DEFAULT has to be used instead of H5I_INVALID_HID. - Added try/catch block to some dynamically allocating memory code and re-throw the bad_alloc exception with a message informing the location of the failure. Platforms tested: Linux/ppc64 (ostrich) Linux/32 2.6 (jam) SunOS 5.11 (emu) --- c++/src/H5ArrayType.cpp | 55 ++++++++++++++++++++++++++++++----------------- c++/src/H5Attribute.cpp | 12 +++++------ c++/src/H5CommonFG.cpp | 3 +++ c++/src/H5DataSet.cpp | 8 +++---- c++/src/H5DataSpace.cpp | 2 +- c++/src/H5DataSpace.h | 12 +++++------ c++/src/H5DataType.cpp | 8 +++---- c++/src/H5File.cpp | 8 +++---- c++/src/H5Group.cpp | 8 +++---- c++/src/H5IdComponent.cpp | 4 ++-- c++/src/H5Location.cpp | 3 +++ c++/src/H5PropList.cpp | 6 +++--- 12 files changed, 75 insertions(+), 54 deletions(-) diff --git a/c++/src/H5ArrayType.cpp b/c++/src/H5ArrayType.cpp index 852658b..5a3f860 100644 --- a/c++/src/H5ArrayType.cpp +++ b/c++/src/H5ArrayType.cpp @@ -54,15 +54,18 @@ ArrayType::ArrayType( const hid_t existing_id ) : DataType( existing_id ) rank = H5Tget_array_ndims(existing_id); if (rank < 0) { - throw DataTypeIException("ArrayType overloaded constructor", "H5Tget_array_ndims failed"); + throw DataTypeIException("ArrayType constructor (existing id)", "H5Tget_array_ndims failed"); } - // Get the dimensions of the existing array and store it in this array - dimensions = new hsize_t[rank]; - //hsize_t rdims2[H5S_MAX_RANK]; - int ret_value = H5Tget_array_dims2(id, dimensions); - if (ret_value < 0) - throw DataTypeIException("ArrayType::getArrayDims", "H5Tget_array_dims2 failed"); + // Get the dimensions of the existing array and store it in this array + try { + dimensions = new hsize_t[rank]; + } catch (const std::bad_alloc&) { + throw DataTypeIException("ArrayType constructor (existing id)", "Memory allocation failed"); + } + int ret_value = H5Tget_array_dims2(id, dimensions); + if (ret_value < 0) + throw DataTypeIException("ArrayType constructor (existing id)", "H5Tget_array_dims2 failed"); } //-------------------------------------------------------------------------- @@ -72,10 +75,14 @@ ArrayType::ArrayType( const hid_t existing_id ) : DataType( existing_id ) //-------------------------------------------------------------------------- ArrayType::ArrayType( const ArrayType& original ) : DataType( original ) { - rank = original.rank; - dimensions = new hsize_t[rank]; - for (int i = 0; i < rank; i++) - dimensions[i] = original.dimensions[i]; + rank = original.rank; + try { + dimensions = new hsize_t[rank]; + } catch (const std::bad_alloc&) { + throw DataTypeIException("ArrayType constructor (existing id)", "Memory allocation failed"); + } + for (int i = 0; i < rank; i++) + dimensions[i] = original.dimensions[i]; } //-------------------------------------------------------------------------- @@ -90,14 +97,18 @@ ArrayType::ArrayType( const ArrayType& original ) : DataType( original ) //-------------------------------------------------------------------------- ArrayType::ArrayType(const DataType& base_type, int ndims, const hsize_t* dims) : DataType() { - hid_t new_type_id = H5Tarray_create2(base_type.getId(), ndims, dims); - if (new_type_id < 0) - throw DataTypeIException("ArrayType constructor", "H5Tarray_create2 failed"); - id = new_type_id; - rank = ndims; - dimensions = new hsize_t[rank]; - for (int i = 0; i < rank; i++) - dimensions[i] = dims[i]; + hid_t new_type_id = H5Tarray_create2(base_type.getId(), ndims, dims); + if (new_type_id < 0) + throw DataTypeIException("ArrayType constructor", "H5Tarray_create2 failed"); + id = new_type_id; + rank = ndims; + try { + dimensions = new hsize_t[rank]; + } catch (const std::bad_alloc&) { + throw DataTypeIException("ArrayType constructor (existing id)", "Memory allocation failed"); + } + for (int i = 0; i < rank; i++) + dimensions[i] = dims[i]; } //-------------------------------------------------------------------------- @@ -140,7 +151,11 @@ int ArrayType::getArrayDims(hsize_t* dims) throw DataTypeIException("ArrayType::getArrayDims", "H5Tget_array_dims2 failed"); // store the array's info in memory rank = ndims; - dimensions = new hsize_t[rank]; + try { + dimensions = new hsize_t[rank]; + } catch (const std::bad_alloc&) { + throw DataTypeIException("ArrayType constructor (existing id)", "Memory allocation failed"); + } for (int i = 0; i < rank; i++) dimensions[i] = dims[i]; } diff --git a/c++/src/H5Attribute.cpp b/c++/src/H5Attribute.cpp index bdd0ac0..a9e928d 100644 --- a/c++/src/H5Attribute.cpp +++ b/c++/src/H5Attribute.cpp @@ -50,7 +50,7 @@ class H5_DLLCPP H5Object; // forward declaration for UserData4Aiterate ///\brief Default constructor: Creates a stub attribute // Programmer Binh-Minh Ribler - May, 2004 //-------------------------------------------------------------------------- -Attribute::Attribute() : AbstractDs(), IdComponent(), id(0) {} +Attribute::Attribute() : AbstractDs(), IdComponent(), id(H5I_INVALID_HID) {} //-------------------------------------------------------------------------- // Function: Attribute copy constructor @@ -162,13 +162,13 @@ void Attribute::read( const DataType& mem_type, void *buf ) const // Mar 2008 // Corrected a misunderstanding that H5Aread would allocate // space for the buffer. Obtained the attribute size and -// allocated memory properly. - BMR +// allocated memory properly. -BMR // Apr 2009 -// Used getInMemDataSize to get attribute data size. - BMR +// Used getInMemDataSize to get attribute data size. -BMR // Jul 2009 // Divided into specific private functions for fixed- and // variable-len string data: p_read_fixed_len and -// p_read_variable_len. This should improve readability. +// p_read_variable_len. This should improve readability. -BMR //-------------------------------------------------------------------------- void Attribute::read(const DataType& mem_type, H5std_string& strg) const { @@ -586,7 +586,7 @@ void Attribute::p_read_fixed_len(const DataType& mem_type, H5std_string& strg) c // Modification // Jul 2009 // Separated the variable length case from the original -// Attribute::read +// Attribute::read. -BMR //-------------------------------------------------------------------------- void Attribute::p_read_variable_len(const DataType& mem_type, H5std_string& strg) const { @@ -650,7 +650,7 @@ void Attribute::close() throw AttributeIException("Attribute::close", "H5Aclose failed"); } // reset the id - id = 0; + id = H5I_INVALID_HID; } } diff --git a/c++/src/H5CommonFG.cpp b/c++/src/H5CommonFG.cpp index d296db4..8ee88d6 100644 --- a/c++/src/H5CommonFG.cpp +++ b/c++/src/H5CommonFG.cpp @@ -33,6 +33,9 @@ #include "H5Alltypes.h" #include "H5private.h" // for HDstrcpy +#include +using namespace std; + // There are a few comments that are common to most of the functions // defined in this file so they are listed here. // - getLocId is called by all functions, that call a C API, to get diff --git a/c++/src/H5DataSet.cpp b/c++/src/H5DataSet.cpp index c34769e..6728264 100644 --- a/c++/src/H5DataSet.cpp +++ b/c++/src/H5DataSet.cpp @@ -52,7 +52,7 @@ namespace H5 { ///\brief Default constructor: creates a stub DataSet. // Programmer Binh-Minh Ribler - 2000 //-------------------------------------------------------------------------- -DataSet::DataSet() : AbstractDs(), H5Object(), id(0) {} +DataSet::DataSet() : AbstractDs(), H5Object(), id(H5I_INVALID_HID) {} //-------------------------------------------------------------------------- // Function: DataSet overloaded constructor @@ -95,7 +95,7 @@ DataSet::DataSet(const DataSet& original) : AbstractDs(original), H5Object(origi // Jul, 2008 // Added for application convenience. //-------------------------------------------------------------------------- -DataSet::DataSet(const H5Location& loc, const void* ref, H5R_type_t ref_type, const PropList& plist) : AbstractDs(), H5Object(), id(0) +DataSet::DataSet(const H5Location& loc, const void* ref, H5R_type_t ref_type, const PropList& plist) : AbstractDs(), H5Object(), id(H5I_INVALID_HID) { id = H5Location::p_dereference(loc.getId(), ref, ref_type, plist, "constructor - by dereferenced"); } @@ -114,7 +114,7 @@ DataSet::DataSet(const H5Location& loc, const void* ref, H5R_type_t ref_type, co // Jul, 2008 // Added for application convenience. //-------------------------------------------------------------------------- -DataSet::DataSet(const Attribute& attr, const void* ref, H5R_type_t ref_type, const PropList& plist) : AbstractDs(), H5Object(), id(0) +DataSet::DataSet(const Attribute& attr, const void* ref, H5R_type_t ref_type, const PropList& plist) : AbstractDs(), H5Object(), id(H5I_INVALID_HID) { id = H5Location::p_dereference(attr.getId(), ref, ref_type, plist, "constructor - by dereference"); } @@ -788,7 +788,7 @@ void DataSet::close() throw DataSetIException("DataSet::close", "H5Dclose failed"); } // reset the id - id = 0; + id = H5I_INVALID_HID; } } diff --git a/c++/src/H5DataSpace.cpp b/c++/src/H5DataSpace.cpp index 6ad8c8d..20b4e5e 100644 --- a/c++/src/H5DataSpace.cpp +++ b/c++/src/H5DataSpace.cpp @@ -630,7 +630,7 @@ void DataSpace::close() throw DataSpaceIException("DataSpace::close", "H5Sclose failed"); } // reset the id - id = 0; + id = H5I_INVALID_HID; } } diff --git a/c++/src/H5DataSpace.h b/c++/src/H5DataSpace.h index 9efecdf..b007fd0 100644 --- a/c++/src/H5DataSpace.h +++ b/c++/src/H5DataSpace.h @@ -33,6 +33,12 @@ class H5_DLLCPP DataSpace : public IdComponent { // Creates a simple dataspace DataSpace(int rank, const hsize_t * dims, const hsize_t * maxdims = NULL); + // Creates a DataSpace object using an existing dataspace id. + DataSpace(const hid_t space_id); + + // Copy constructor: makes a copy of the original DataSpace object. + DataSpace(const DataSpace& original); + // Assignment operator DataSpace& operator=( const DataSpace& rhs ); @@ -109,12 +115,6 @@ class H5_DLLCPP DataSpace : public IdComponent { ///\brief Returns this class name. virtual H5std_string fromClass () const { return("DataSpace"); } - // Creates a DataSpace object using an existing dataspace id. - DataSpace(const hid_t space_id); - - // Copy constructor: makes a copy of the original DataSpace object. - DataSpace(const DataSpace& original); - // Gets the dataspace id. virtual hid_t getId() const; diff --git a/c++/src/H5DataType.cpp b/c++/src/H5DataType.cpp index afc5678..a435b4e 100644 --- a/c++/src/H5DataType.cpp +++ b/c++/src/H5DataType.cpp @@ -53,7 +53,7 @@ namespace H5 { ///\brief Default constructor: Creates a stub datatype // Programmer Binh-Minh Ribler - 2000 //-------------------------------------------------------------------------- -DataType::DataType() : H5Object(), id(0) {} +DataType::DataType() : H5Object(), id(H5I_INVALID_HID) {} //-------------------------------------------------------------------------- // Function: DataType overloaded constructor @@ -105,7 +105,7 @@ DataType::DataType( const H5T_class_t type_class, size_t size ) : H5Object() // Jul, 2008 // Added for application convenience. //-------------------------------------------------------------------------- -DataType::DataType(const H5Location& loc, const void* ref, H5R_type_t ref_type, const PropList& plist) : H5Object(), id(0) +DataType::DataType(const H5Location& loc, const void* ref, H5R_type_t ref_type, const PropList& plist) : H5Object(), id(H5I_INVALID_HID) { id = H5Location::p_dereference(loc.getId(), ref, ref_type, plist, "constructor - by dereference"); } @@ -124,7 +124,7 @@ DataType::DataType(const H5Location& loc, const void* ref, H5R_type_t ref_type, // Jul, 2008 // Added for application convenience. //-------------------------------------------------------------------------- -DataType::DataType(const Attribute& attr, const void* ref, H5R_type_t ref_type, const PropList& plist) : H5Object(), id(0) +DataType::DataType(const Attribute& attr, const void* ref, H5R_type_t ref_type, const PropList& plist) : H5Object(), id(H5I_INVALID_HID) { id = H5Location::p_dereference(attr.getId(), ref, ref_type, plist, "constructor - by dereference"); } @@ -710,7 +710,7 @@ void DataType::close() throw DataTypeIException(inMemFunc("close"), "H5Tclose failed"); } // reset the id - id = 0; + id = H5I_INVALID_HID; } } diff --git a/c++/src/H5File.cpp b/c++/src/H5File.cpp index ff00e3b..45e08b2 100644 --- a/c++/src/H5File.cpp +++ b/c++/src/H5File.cpp @@ -50,7 +50,7 @@ namespace H5 { ///\brief Default constructor: creates a stub H5File object. // Programmer Binh-Minh Ribler - 2000 //-------------------------------------------------------------------------- -H5File::H5File() : H5Location(), id(0) {} +H5File::H5File() : H5Location(), id(H5I_INVALID_HID) {} //-------------------------------------------------------------------------- // Function: H5File overloaded constructor @@ -86,7 +86,7 @@ H5File::H5File() : H5Location(), id(0) {} // to catch then re-throw it. -BMR 2013/03/21 // Programmer Binh-Minh Ribler - 2000 //-------------------------------------------------------------------------- -H5File::H5File( const char* name, unsigned int flags, const FileCreatPropList& create_plist, const FileAccPropList& access_plist ) : H5Location(), id(0) +H5File::H5File( const char* name, unsigned int flags, const FileCreatPropList& create_plist, const FileAccPropList& access_plist ) : H5Location(), id(H5I_INVALID_HID) { try { p_get_file(name, flags, create_plist, access_plist); @@ -111,7 +111,7 @@ H5File::H5File( const char* name, unsigned int flags, const FileCreatPropList& c // to catch then re-throw it. -BMR 2013/03/21 // Programmer Binh-Minh Ribler - 2000 //-------------------------------------------------------------------------- -H5File::H5File( const H5std_string& name, unsigned int flags, const FileCreatPropList& create_plist, const FileAccPropList& access_plist ) : H5Location(), id(0) +H5File::H5File( const H5std_string& name, unsigned int flags, const FileCreatPropList& create_plist, const FileAccPropList& access_plist ) : H5Location(), id(H5I_INVALID_HID) { try { p_get_file(name.c_str(), flags, create_plist, access_plist); @@ -604,7 +604,7 @@ void H5File::close() throw FileIException("H5File::close", "H5Fclose failed"); } // reset the id - id = 0; + id = H5I_INVALID_HID; } } diff --git a/c++/src/H5Group.cpp b/c++/src/H5Group.cpp index 7679315..cad5e29 100644 --- a/c++/src/H5Group.cpp +++ b/c++/src/H5Group.cpp @@ -51,7 +51,7 @@ namespace H5 { ///\brief Default constructor: creates a stub Group. // Programmer Binh-Minh Ribler - 2000 //-------------------------------------------------------------------------- -Group::Group() : H5Object(), id(0) {} +Group::Group() : H5Object(), id(H5I_INVALID_HID) {} //-------------------------------------------------------------------------- // Function: Group copy constructor @@ -100,7 +100,7 @@ Group::Group(const hid_t existing_id) : H5Object() /// is a datatype that has been named by DataType::commit. // Programmer Binh-Minh Ribler - Oct, 2006 //-------------------------------------------------------------------------- -Group::Group(const H5Location& loc, const void* ref, H5R_type_t ref_type, const PropList& plist) : H5Object(), id(0) +Group::Group(const H5Location& loc, const void* ref, H5R_type_t ref_type, const PropList& plist) : H5Object(), id(H5I_INVALID_HID) { id = H5Location::p_dereference(loc.getId(), ref, ref_type, plist, "constructor - by dereference"); } @@ -115,7 +115,7 @@ Group::Group(const H5Location& loc, const void* ref, H5R_type_t ref_type, const ///\exception H5::ReferenceException // Programmer Binh-Minh Ribler - Oct, 2006 //-------------------------------------------------------------------------- -Group::Group(const Attribute& attr, const void* ref, H5R_type_t ref_type, const PropList& plist) : H5Object(), id(0) +Group::Group(const Attribute& attr, const void* ref, H5R_type_t ref_type, const PropList& plist) : H5Object(), id(H5I_INVALID_HID) { id = H5Location::p_dereference(attr.getId(), ref, ref_type, plist, "constructor - by dereference"); } @@ -181,7 +181,7 @@ void Group::close() throw GroupIException("Group::close", "H5Gclose failed"); } // reset the id - id = 0; + id = H5I_INVALID_HID; } } diff --git a/c++/src/H5IdComponent.cpp b/c++/src/H5IdComponent.cpp index 99a8dc4..64297ee 100644 --- a/c++/src/H5IdComponent.cpp +++ b/c++/src/H5IdComponent.cpp @@ -145,7 +145,7 @@ int IdComponent::getCounter() const //-------------------------------------------------------------------------- H5I_type_t IdComponent::getHDFObjType(const hid_t obj_id) { - if (obj_id == 0) + if (obj_id <= 0) return H5I_BADID; // invalid H5I_type_t id_type = H5Iget_type(obj_id); if (id_type <= H5I_BADID || id_type >= H5I_NTYPES) @@ -339,7 +339,7 @@ H5std_string IdComponent::p_get_file_name() const //-------------------------------------------------------------------------- bool IdComponent::p_valid_id(const hid_t obj_id) { - if (obj_id == 0) + if (obj_id <= 0) return false; H5I_type_t id_type = H5Iget_type(obj_id); diff --git a/c++/src/H5Location.cpp b/c++/src/H5Location.cpp index 9d3d7bf..89f10b5 100644 --- a/c++/src/H5Location.cpp +++ b/c++/src/H5Location.cpp @@ -33,6 +33,9 @@ #include "H5DataSet.h" #include "H5Attribute.h" #include "H5private.h" // for HDmemset +#include +using namespace std; + #ifndef H5_NO_NAMESPACE namespace H5 { diff --git a/c++/src/H5PropList.cpp b/c++/src/H5PropList.cpp index 0b740d8..56743f7 100644 --- a/c++/src/H5PropList.cpp +++ b/c++/src/H5PropList.cpp @@ -46,7 +46,7 @@ const PropList PropList::DEFAULT; ///\brief Default constructor: creates a stub property list object. // Programmer Binh-Minh Ribler - 2000 //-------------------------------------------------------------------------- -PropList::PropList() : IdComponent(), id(0) {} +PropList::PropList() : IdComponent(), id(H5P_DEFAULT) {} //-------------------------------------------------------------------------- // Function: PropList copy constructor @@ -74,7 +74,7 @@ PropList::PropList(const PropList& original) : IdComponent(original) //-------------------------------------------------------------------------- PropList::PropList( const hid_t plist_id ) : IdComponent() { - if (plist_id == 0) + if (plist_id <= 0) id = H5P_DEFAULT; H5I_type_t id_type = H5Iget_type(plist_id); @@ -277,7 +277,7 @@ void PropList::close() throw PropListIException(inMemFunc("close"), "H5Pclose failed"); } // reset the id - id = 0; + id = H5I_INVALID_HID; } } -- cgit v0.12