Skip to content

Commit

Permalink
#1855 Replaced strncpy to m_strncpy and enhance m_strcpy & m_strncpy (#…
Browse files Browse the repository at this point in the history
…1901)

Co-authored-by: Howard Soh <hsoh@kiowa.rap.ucar.edu>
  • Loading branch information
hsoh-u and Howard Soh authored Sep 1, 2021
1 parent 3486de8 commit d054d81
Show file tree
Hide file tree
Showing 19 changed files with 109 additions and 73 deletions.
4 changes: 0 additions & 4 deletions met/src/basic/enum_to_string/my_enum_scanner.cc
Original file line number Diff line number Diff line change
Expand Up @@ -432,8 +432,6 @@ int do_id()

{

// column += m_strlen(yytext);

if ( enum_mode || last_was_enum || last_was_class ) {

strncpy(yylval.name, yytext, sizeof(yylval.name));
Expand All @@ -456,8 +454,6 @@ int do_int()

{

// column += m_strlen(yytext);

if ( !enum_mode ) return ( 0 );

yylval.ival = atoi(yytext);
Expand Down
25 changes: 14 additions & 11 deletions met/src/basic/vx_config/my_config_scanner.cc
Original file line number Diff line number Diff line change
Expand Up @@ -527,10 +527,14 @@ int do_id()
{

int j, k;
const char *method_name = "do_id() -> ";

Column += m_strlen(configtext);

if ( is_lhs ) { strncpy(configlval.text, configtext, max_id_length); return ( IDENTIFIER ); }
if ( is_lhs ) {
m_strncpy(configlval.text, configtext, max_id_length, method_name, "configlval.text1");
return ( IDENTIFIER );
}

//
// print?
Expand Down Expand Up @@ -642,7 +646,8 @@ for (j=0; j<n_perc_thresh_infos; ++j) {
// nope
//

strncpy(configlval.text, configtext, sizeof(configlval.text) - 1);
m_strncpy(configlval.text, configtext, sizeof(configlval.text) - 1,
method_name, "configlval.text2");

need_number = false;

Expand All @@ -658,8 +663,6 @@ int do_int()

{

// Column += m_strlen(configtext);

configlval.nval.i = atoi(configtext);

configlval.nval.is_int = true;
Expand All @@ -679,8 +682,6 @@ bool do_float()

{

// Column += m_strlen(configtext);

configlval.nval.d = atof(configtext);

configlval.nval.is_int = false;
Expand Down Expand Up @@ -724,6 +725,7 @@ clear_lexeme();
int n;
int c;
char * line = (char *) lexeme;
const char *method_name = "do_quoted_string() -> ";

clear_lexeme();

Expand Down Expand Up @@ -765,7 +767,7 @@ while ( n < max_id_length ) {

if ( (n + 1) >= max_id_length ) {

mlog << Error << "\ndo_quoted_string() -> "
mlog << Error << "\n" << method_name
<< "string too long! ... c = \"" << c << "\"\n\n";

exit ( 1 );
Expand All @@ -787,7 +789,7 @@ while ( replace_env(s) ) {

if ( s.length() >= max_id_length ) {

mlog << Error << "\ndo_quoted_string() -> "
mlog << Error << "\n" << method_name
<< "string \"" << s << "\" too long!\n\n";

exit ( 1 );
Expand All @@ -798,9 +800,9 @@ clear_lexeme();

if ( s.nonempty() ) {

strncpy((char *) lexeme, s.c_str(), max_id_length);
m_strncpy((char *) lexeme, s.c_str(), max_id_length, method_name, "lexeme");

strncpy(configlval.text, line, max_id_length);
m_strncpy(configlval.text, line, max_id_length, method_name, "configlval.text");

} else {

Expand Down Expand Up @@ -1150,7 +1152,8 @@ int do_fort_thresh()

{

strncpy(configlval.text, configtext, sizeof(configlval.text));
const char *method_name = "do_fort_thresh() -> ";
m_strncpy(configlval.text, configtext, sizeof(configlval.text), method_name);

return ( FORTRAN_THRESHOLD );

Expand Down
3 changes: 2 additions & 1 deletion met/src/basic/vx_util/ascii_table.cc
Original file line number Diff line number Diff line change
Expand Up @@ -981,6 +981,7 @@ void AsciiTable::set_entry(const int r, const int c, double x)
{

ConcatString str;
const char *method_name = "AsciiTable::set_entry() -> ";

if ( fabs(x - BadDataValue) < 0.0001 ) str = BadDataStr;
else {
Expand All @@ -994,7 +995,7 @@ fix_float(str);

if ( DoCommaString ) {
char junk[256];
strncpy(junk, str.c_str(), str.length());
m_strncpy(junk, str.c_str(), str.length(), method_name);
char * p = (char *) 0;
long X;
ConcatString s;
Expand Down
23 changes: 20 additions & 3 deletions met/src/basic/vx_util/string_fxns.cc
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ using namespace std;
#include "vx_log.h"


const bool ENHANCE_STR_APIS = false;
const bool ENHANCE_STR_APIS = true;

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

Expand Down Expand Up @@ -412,7 +412,11 @@ int m_strlen(const char *str) {
void m_strcpy(char *to_str, const char *from_str, const char *method_name,
const char *extra_msg) {

strcpy(to_str, from_str);
if (ENHANCE_STR_APIS) {
int str_len = sizeof to_str;
m_strncpy(to_str, from_str, str_len, method_name, extra_msg);
}
else strcpy(to_str, from_str);

}

Expand Down Expand Up @@ -463,7 +467,20 @@ void m_strncpy(char *to_str, const char *from_str, const int buf_len,
if (str_len > buf_len) str_len = buf_len;

memset(to_str, 0, str_len);
strncpy(to_str, from_str, str_len);
if (ENHANCE_STR_APIS) {
string temp_str = from_str;
temp_str.copy(to_str, str_len);
to_str[str_len] = 0;

// Kludge: The sizeof from_str is 8 when the filenames come from a python script
if (strcmp(from_str, to_str)) {
str_len = m_strlen(from_str);
if (str_len > buf_len) str_len = buf_len;
temp_str.copy(to_str, str_len);
to_str[str_len] = 0;
}
}
else strncpy(to_str, from_str, str_len);

if (strcmp(from_str, to_str)) {
mlog << Warning << "\n" << method_name
Expand Down
9 changes: 7 additions & 2 deletions met/src/libcode/vx_color/my_color_scanner.cc
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ using namespace std;
#include <cmath>


#include "string_fxns.h"

#include "color_parser.h" // this must be included before color_parser_yacc.h
#include "color.h"

Expand Down Expand Up @@ -516,6 +518,8 @@ int do_id()

{

const char *method_name = "is_id() -> ";

if ( strcmp(colortext, "blend" ) == 0 ) return ( BLEND );
if ( strcmp(colortext, "hsv" ) == 0 ) return ( HSV );
if ( strcmp(colortext, "cmyk" ) == 0 ) return ( CMYK );
Expand All @@ -532,7 +536,7 @@ if ( clist.has_name(colortext, index) ) {

}

strncpy(colorlval.text, colortext, sizeof(colorlval.text) - 1);
m_strncpy(colorlval.text, colortext, sizeof(colorlval.text) - 1, method_name);



Expand Down Expand Up @@ -681,6 +685,7 @@ void do_quoted_string()
int n;
char c;
char line[max_lexeme_size + 1];
const char *method_name = "do_quoted_string() -> ";

memset(line, 0, sizeof(line));

Expand Down Expand Up @@ -728,7 +733,7 @@ while ( n < max_lexeme_size ) {

line[n] = (char) 0;

strncpy(colorlval.text, line, sizeof(colorlval.text));
m_strncpy(colorlval.text, line, sizeof(colorlval.text), method_name);

colorlval.text[ sizeof(colorlval.text) - 1 ] = (char) 0;

Expand Down
12 changes: 7 additions & 5 deletions met/src/libcode/vx_data2d_grib/data2d_grib_utils.cc
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ using namespace std;
#include "data2d_grib_utils.h"
#include "angles.h"
#include "is_bad_data.h"
#include "string_fxns.h"
#include "vx_log.h"
#include "vx_math.h"

Expand All @@ -41,6 +42,7 @@ bool is_prelim_match( VarInfoGrib & vinfo, const GribRecord & g)
int ens_application, ens_type, ens_number, vinfo_ens_type;
int vinfo_ens_number;
unixtime ut, init_ut, valid_ut;
const char *method_name = "is_prelim_match() -> ";

int p_code, code_for_lookup= vinfo.field_rec ();
double p_thresh_lo, p_thresh_hi;
Expand Down Expand Up @@ -110,8 +112,8 @@ bool is_prelim_match( VarInfoGrib & vinfo, const GribRecord & g)
}

char *ens_number_str = new char[vinfo_ens.length()];
strncpy(ens_number_str, vinfo_ens.text()+1,
(size_t) vinfo_ens.length());
m_strncpy(ens_number_str, vinfo_ens.text()+1,
(size_t) vinfo_ens.length(), method_name);
ens_number_str[vinfo_ens.length()-1] = (char) 0;

// if the string is numeric
Expand All @@ -123,7 +125,7 @@ bool is_prelim_match( VarInfoGrib & vinfo, const GribRecord & g)
// if one of the parameters was not set - error
if( is_bad_data(vinfo_ens_number) ||
is_bad_data(vinfo_ens_type) ) {
mlog << Error << "\nis_prelim_match() -> "
mlog << Error << "\n" << method_name
<< "unrecognized GRIB_ens value '" << vinfo_ens
<< "' should be '" << conf_key_grib_ens_hi_res_ctl
<< "' or '" << conf_key_grib_ens_low_res_ctl
Expand Down Expand Up @@ -179,7 +181,7 @@ bool is_prelim_match( VarInfoGrib & vinfo, const GribRecord & g)

// if either the field name or the indices are specified, bail
if( bad_data_int == vinfo_ptv || bad_data_int == code_for_lookup ) {
mlog << Error << "\nis_prelim_match() -> "
mlog << Error << "\n" << method_name
<< "either name or GRIB1_ptv and GRIB1_code must be "
<< "specified in field information\n\n";
exit(1);
Expand All @@ -190,7 +192,7 @@ bool is_prelim_match( VarInfoGrib & vinfo, const GribRecord & g)
//if did not find with params from the header - try default
if( !GribTable.lookup_grib1(code_for_lookup, default_grib1_ptv, default_grib1_center, default_grib1_subcenter, tab) )
{
mlog << Error << "\nis_prelim_match() -> "
mlog << Error << "\n" << method_name
<< "no parameter found with matching GRIB1_ptv ("
<< vinfo_ptv << ") " << "GRIB1_code ("
<< vinfo.field_rec() << "). Use the MET_GRIB_TABLES "
Expand Down
5 changes: 4 additions & 1 deletion met/src/libcode/vx_data2d_grib2/data2d_grib2.cc
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ using namespace std;

#include "data2d_grib2.h"
#include "grib_strings.h"
#include "string_fxns.h"
#include "vx_data2d.h"
#include "vx_math.h"
#include "vx_log.h"
Expand Down Expand Up @@ -363,6 +364,8 @@ void MetGrib2DataFile::find_record_matches( VarInfoGrib2* vinfo,
vector<Grib2Record*> &listMatchRange
){

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

// clear the contents of the result vectors
listMatchExact.clear();
listMatchRange.clear();
Expand All @@ -389,7 +392,7 @@ void MetGrib2DataFile::find_record_matches( VarInfoGrib2* vinfo,
vinfo_ens_type = 2;
}
char* ens_number_str = new char[vinfo_ens.length() ];
strncpy(ens_number_str, vinfo_ens.text()+1, (size_t) vinfo_ens.length());
m_strncpy(ens_number_str, vinfo_ens.text()+1, (size_t) vinfo_ens.length(), method_name);
ens_number_str[vinfo_ens.length()-1] = (char) 0;

// if the string is numeric
Expand Down
8 changes: 4 additions & 4 deletions met/src/libcode/vx_data2d_nc_pinterp/pinterp_file.cc
Original file line number Diff line number Diff line change
Expand Up @@ -184,12 +184,12 @@ int month, day, year, hour, minute, second, str_len;
char time_str[max_str_len];
string c;
NcVar v;

const char *method_name = "PinterpFile::open() -> ";

close();

Nc = open_ncfile(filename);
mlog << Debug(5) << "\nPinterpFile::open() -> "
mlog << Debug(5) << "\n" << method_name
<< "opend \"" << filename << "\".\n\n";

if ( IS_INVALID_NC_P(Nc) ) { close(); return ( false ); }
Expand Down Expand Up @@ -245,7 +245,7 @@ if ( has_var(Nc, times_var_name) ) {
for (j=0; j<Ntimes; ++j) {
ConcatString tmp_time_str;
get_string_val(&v, j, str_len, tmp_time_str);
strncpy ( time_str, tmp_time_str.c_str(), str_len );
m_strncpy ( time_str, tmp_time_str.c_str(), str_len, method_name );
time_str[str_len] = '\0';

// Check for leading blank
Expand All @@ -256,7 +256,7 @@ if ( has_var(Nc, times_var_name) ) {

if(sscanf(time_str, "%4d-%2d-%2d_%2d:%2d:%2d",
&year, &month, &day, &hour, &minute, &second) != 6) {
mlog << Error << "\nPinterpFile::open() -> "
mlog << Error << "\n" << method_name
<< "error parsing time string \"" << time_str << "\".\n\n";
return ( false );
}
Expand Down
2 changes: 1 addition & 1 deletion met/src/libcode/vx_data2d_nccf/nccf_file.cc
Original file line number Diff line number Diff line change
Expand Up @@ -2577,7 +2577,7 @@ void NcCfFile::get_grid_mapping_geostationary(
ConcatString scene_id;
if (get_global_att(_ncFile, (string)"scene_id", scene_id)) {
char* scene_id_str = new char[scene_id.length()+1];
strncpy(scene_id_str, scene_id.text(), scene_id.length());
m_strncpy(scene_id_str, scene_id.text(), scene_id.length(), method_name.c_str());
scene_id_str[scene_id.length()] = 0;
data.scene_id = scene_id_str;
}
Expand Down
2 changes: 1 addition & 1 deletion met/src/libcode/vx_nc_obs/nc_obs_util.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1396,7 +1396,7 @@ int write_nc_string_array (NcVar *ncVar, StringArray &strArray, const int str_le
len_n = string_data.length();
len_p = strnlen(data_buf[buf_index], str_len);
if (len_n > str_len) len_n = str_len;
strncpy(data_buf[buf_index], string_data.c_str(), len_n);
m_strncpy(data_buf[buf_index], string_data.c_str(), len_n, method_name.c_str());
for (int idx=len_n; idx<len_p; idx++)
data_buf[buf_index][idx] = 0; // erase previous data

Expand Down
3 changes: 2 additions & 1 deletion met/src/libcode/vx_python3_utils/wchar_argv.cc
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,7 @@ int len;
char * s = 0;
char ** av = 0;
ConcatString c;
const char *method_name = "Wchar_Argv::set() -> ";


len = 0;
Expand Down Expand Up @@ -172,7 +173,7 @@ for (j=0; j<(a.n()); ++j) {

len = c.length();

strncpy(s + k, c.text(), len);
m_strncpy(s + k, c.text(), len, method_name);

k += (len + 1);

Expand Down
3 changes: 2 additions & 1 deletion met/src/tools/dev_utils/gribtab.dat_to_flat.cc
Original file line number Diff line number Diff line change
Expand Up @@ -92,8 +92,9 @@ const char s_delim [] = ",\"";
const char * c = (const char *) 0;
char line2[512];
char * s = line2;
const char *method_name = "parse_line() -> ";

strncpy(line2, line, sizeof(line2));
m_strncpy(line2, line, sizeof(line2), method_name);

//
// get first 6 integers
Expand Down
Loading

0 comments on commit d054d81

Please sign in to comment.