Unidata / netcdf-cxx4

Official GitHub repository for netCDF-C++ libraries and utilities.
Other
124 stars 49 forks source link

Inconsistent behavior of getAtt() function #51

Open sairum opened 6 years ago

sairum commented 6 years ago

In examples/sfc_pres_temp_rd.cpp the following code does not run as expected

105 att = latVar.getAtt("units"); 106 if(att.isNull()) return NC_ERR;

If the attribute "units" is nonexistent, the NC_ERR is never returned, because att.isNull() is also never evaluated. If an attribute of a variable is not defined the function getAtt() throws an exception and the program is stopped!

Check for its behavior in cxx4/ncVar.cpp

// Gets attribute by name.
NcVarAtt NcVar::getAtt(const string& name) const
{
  map<string,NcVarAtt> attributeList = getAtts();
  map<string,NcVarAtt>::iterator myIter;
  myIter = attributeList.find(name);
  if(myIter == attributeList.end()){
    string msg("Attribute '"+name+"' not found");
    throw NcException(msg.c_str(),__FILE__,__LINE__);
  }
  return NcVarAtt(myIter->second);
}

Shouldn't getAtt() return a NcVarAtt with isNull() == true?

Antonio

auschok commented 6 years ago

I agree.

It is inconsistent to the examples and to an old docu from https://www.unidata.ucar.edu/software/netcdf/netcdf-4/newdocs/netcdf-cxx.html#Class-NcVar:

NcAtt get_att( NcToken attname ) const NcAtt get_att( int n ) const The first member function returns a variable attribute by name. The second returns the n-th (starting at 0) attribute of the variable. In either case, if no such attribute has been attached to the variable, zero is returned. Attributes returned in this way belong to the caller, and hence should eventually be deleted by the caller to avoid memory leaks.