From e35b42b84fa26fbb3cc368af838ed1fe7d476c04 Mon Sep 17 00:00:00 2001 From: John Halley Gotway Date: Tue, 6 Feb 2024 16:55:11 +0000 Subject: [PATCH] Per #2809, update the LaeaData struct to store the spheroid_name as a 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. --- src/libcode/vx_data2d_grib2/data2d_grib2.cc | 10 ++++++---- src/libcode/vx_grid/find_grid_by_name.cc | 19 +++++++++++++------ src/libcode/vx_grid/grid_base.cc | 1 - src/libcode/vx_grid/laea_grid.cc | 9 ++++++--- src/libcode/vx_grid/laea_grid_defs.h | 4 ++-- 5 files changed, 27 insertions(+), 16 deletions(-) diff --git a/src/libcode/vx_data2d_grib2/data2d_grib2.cc b/src/libcode/vx_data2d_grib2/data2d_grib2.cc index e86ef7ca2c..440f55de87 100644 --- a/src/libcode/vx_data2d_grib2/data2d_grib2.cc +++ b/src/libcode/vx_data2d_grib2/data2d_grib2.cc @@ -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; @@ -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); } @@ -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 @@ -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); } @@ -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); diff --git a/src/libcode/vx_grid/find_grid_by_name.cc b/src/libcode/vx_grid/find_grid_by_name.cc index 8197bbec49..5354304a3d 100644 --- a/src/libcode/vx_grid/find_grid_by_name.cc +++ b/src/libcode/vx_grid/find_grid_by_name.cc @@ -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 ); diff --git a/src/libcode/vx_grid/grid_base.cc b/src/libcode/vx_grid/grid_base.cc index 9ed499be1e..9d86afa9ed 100644 --- a/src/libcode/vx_grid/grid_base.cc +++ b/src/libcode/vx_grid/grid_base.cc @@ -870,7 +870,6 @@ assign(g); } - //////////////////////////////////////////////////////////////////////// diff --git a/src/libcode/vx_grid/laea_grid.cc b/src/libcode/vx_grid/laea_grid.cc index 067e52f048..6d59f25f6e 100644 --- a/src/libcode/vx_grid/laea_grid.cc +++ b/src/libcode/vx_grid/laea_grid.cc @@ -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); @@ -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; @@ -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); diff --git a/src/libcode/vx_grid/laea_grid_defs.h b/src/libcode/vx_grid/laea_grid_defs.h index 87dcbcfef4..ab02d713b4 100644 --- a/src/libcode/vx_grid/laea_grid_defs.h +++ b/src/libcode/vx_grid/laea_grid_defs.h @@ -17,7 +17,7 @@ //////////////////////////////////////////////////////////////////////// -#include "string.h" +#include "vx_log.h" //////////////////////////////////////////////////////////////////////// @@ -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