Skip to content

Commit

Permalink
Per #2809, update the LaeaData struct to store the spheroid_name as a…
Browse files Browse the repository at this point in the history
… character array. I tried very hard to store this as a string instead but ran into lots of problems with segfaults when deleting the allocated structs. Using a fixed length character array while making calls to the m_strncpy() and m_strlen() utility functions enables the code to work properly while hopefully minimizing SonarQube findings.
  • Loading branch information
JohnHalleyGotway committed Feb 6, 2024
1 parent 2aa6f50 commit e35b42b
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 16 deletions.
10 changes: 6 additions & 4 deletions src/libcode/vx_data2d_grib2/data2d_grib2.cc
Original file line number Diff line number Diff line change
Expand Up @@ -986,6 +986,8 @@ void MetGrib2DataFile::read_grib2_record_list() {

void MetGrib2DataFile::read_grib2_grid(gribfield *gfld) {

const char * method_name = "MetGrib2DataFile::read_grib2_grid() -> ";

double d, r_km;
int ResCompFlag;
char hem = 0;
Expand Down Expand Up @@ -1040,7 +1042,7 @@ void MetGrib2DataFile::read_grib2_grid(gribfield *gfld) {

// check for thinned lat/lon grid
if( data.Nlon == -1 ){
mlog << Error << "\nMetGrib2DataFile::read_grib2_grid() -> "
mlog << Error << "\n" << method_name
<< "Thinned Lat/Lon grids are not supported for GRIB version 2.\n\n";
exit(1);
}
Expand Down Expand Up @@ -1329,7 +1331,7 @@ void MetGrib2DataFile::read_grib2_grid(gribfield *gfld) {
// build an LaeaData struct with the projection information
LaeaData laea;
laea.name = laea_proj_type;
laea.spheroid_name = "Grib template";
m_strncpy(laea.spheroid_name, "Grib template",m_strlen("Grib template"), method_name);

// earth shape
// Reference: https://www.nco.ncep.noaa.gov/pmb/docs/grib2/grib2_doc/grib2_table3-2.shtml
Expand Down Expand Up @@ -1385,7 +1387,7 @@ void MetGrib2DataFile::read_grib2_grid(gribfield *gfld) {
break;

default:
mlog << Error << "\nMetGrib2DataFile::read_grib2_grid() -> "
mlog << Error << "\n" << method_name
<< "unsupported earth shape value of " << earth_shape_int << "!\n\n";
exit(1);
}
Expand Down Expand Up @@ -1433,7 +1435,7 @@ void MetGrib2DataFile::read_grib2_grid(gribfield *gfld) {
// unrecognized grid
else {

mlog << Error << "\nMetGrib2DataFile::read_grib2_grid() -> "
mlog << Error << "\n" << method_name
<< "found unrecognized grid definition (" << gfld->igdtnum << ")\n\n";
exit(1);

Expand Down
19 changes: 13 additions & 6 deletions src/libcode/vx_grid/find_grid_by_name.cc
Original file line number Diff line number Diff line change
Expand Up @@ -55,12 +55,19 @@ if ( !status || !(i.ok()) ) return ( false );

status = false;

if ( i.lc ) { g.set( *(i.lc) ); status = true; }
if ( i.st ) { g.set( *(i.st) ); status = true; }
if ( i.ll ) { g.set( *(i.ll) ); status = true; }
if ( i.m ) { g.set( *(i.m) ); status = true; }
if ( i.g ) { g.set( *(i.g) ); status = true; }
if ( i.la ) { g.set( *(i.la) ); status = true; }
if ( i.lc ) { g.set( *(i.lc) ); status = true; }
if ( i.st ) { g.set( *(i.st) ); status = true; }
if ( i.ll ) { g.set( *(i.ll) ); status = true; }
if ( i.rll ) { g.set( *(i.rll) ); status = true; }
if ( i.m ) { g.set( *(i.m) ); status = true; }
if ( i.g ) { g.set( *(i.g) ); status = true; }
if ( i.gi ) { g.set( *(i.gi) ); status = true; }
if ( i.la ) { g.set( *(i.la) ); status = true; }
if ( i.tc ) { g.set( *(i.tc) ); status = true; }
if ( i.sl ) { g.set( *(i.sl) ); status = true; }
#ifdef WITH_UGRID
if ( i.us ) { g.set( *(i.us) ); status = true; }
#endif

return ( status );

Expand Down
1 change: 0 additions & 1 deletion src/libcode/vx_grid/grid_base.cc
Original file line number Diff line number Diff line change
Expand Up @@ -870,7 +870,6 @@ assign(g);
}



////////////////////////////////////////////////////////////////////////


Expand Down
9 changes: 6 additions & 3 deletions src/libcode/vx_grid/laea_grid.cc
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,8 @@ Ny = data.ny;

geoid.set_ab(data.equatorial_radius_km, data.polar_radius_km);

geoid.set_name(data.spheroid_name.c_str());
string s = data.spheroid_name;
geoid.set_name(s.c_str());

aff.set_mb(1.0/(data.dx_km), 0.0, 0.0, 1.0/(data.dy_km), 0.0, 0.0);

Expand All @@ -119,6 +120,8 @@ LaeaGrid::LaeaGrid(const LaeaNetcdfData & nc)

{

const char * method_name = "LaeaGrid::LaeaGrid(const LaeaNetcdfData &) -> ";

double u, v, lat, lon;
const double tol = 1.0e-5;

Expand Down Expand Up @@ -161,9 +164,9 @@ if ( fabs((nc.semi_major_axis_km - nc.semi_minor_axis_km)/(nc.semi_major_axis_km

geoid.set_ab(Data.equatorial_radius_km, Data.polar_radius_km);

Data.spheroid_name = "Undefined";
m_strncpy(Data.spheroid_name, "Undefined", m_strlen("Undefined"), method_name);

geoid.set_name(Data.spheroid_name.c_str());
geoid.set_name("Undefined");

aff.set_mb(1.0/(Data.dx_km), 0.0, 0.0, 1.0/(Data.dy_km), 0.0, 0.0);

Expand Down
4 changes: 2 additions & 2 deletions src/libcode/vx_grid/laea_grid_defs.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
////////////////////////////////////////////////////////////////////////


#include "string.h"
#include "vx_log.h"


////////////////////////////////////////////////////////////////////////
Expand Down Expand Up @@ -67,7 +67,7 @@ struct LaeaData {

const char * name; // not allocated

std::string spheroid_name;
char spheroid_name[max_str_len];

double radius_km; // for spherical Earth

Expand Down

0 comments on commit e35b42b

Please sign in to comment.