diff --git a/met/src/basic/enum_to_string/Makefile.am b/met/src/basic/enum_to_string/Makefile.am index 1f03b2ccef..4877eb5147 100644 --- a/met/src/basic/enum_to_string/Makefile.am +++ b/met/src/basic/enum_to_string/Makefile.am @@ -16,7 +16,7 @@ enum_to_string_SOURCES = enum_parser.yy \ code.cc code.h \ scope.cc scope.h \ my_enum_scanner.cc \ - enum_to_string.cc + enum_to_string.cc enum_to_string.h enum_to_string_CPPFLAGS = -DCOPYRIGHT_DIR="\"${abs_top_srcdir}/data\"" ${MET_CPPFLAGS} enum_to_string_LDFLAGS = ${MET_LDFLAGS} enum_to_string_LDADD = -lvx_cal \ diff --git a/met/src/basic/enum_to_string/code.cc b/met/src/basic/enum_to_string/code.cc index ed79e593e5..e72238eecf 100644 --- a/met/src/basic/enum_to_string/code.cc +++ b/met/src/basic/enum_to_string/code.cc @@ -10,16 +10,6 @@ //////////////////////////////////////////////////////////////////////// -static const bool do_name_len_decl = true; - -static const bool echo_pound_define_after_endif = true; - -static const char copyright_filename [] = "copyright_notice.txt"; // relative to MET_BASE_DIR - - -//////////////////////////////////////////////////////////////////////// - - using namespace std; @@ -33,43 +23,22 @@ using namespace std; #include #include "code.h" +#include "enum_to_string.h" +#include "str_wrappers.h" #include "vx_cal.h" //////////////////////////////////////////////////////////////////////// -static const char * sep = "////////////////////////////////////////////////////////////////////////\n"; - - -//////////////////////////////////////////////////////////////////////// - - - // - // these have external linkage - // - - -extern bool do_prefix; - -extern const char * header_suffix; - -extern bool do_angle_brackets; - -extern bool do_array; - -extern bool do_reverse; - -extern bool do_concat_string; - -extern bool verbose; +static const bool do_name_len_decl = true; -extern unixtime generation_gmt; +static const bool echo_pound_define_after_endif = true; -extern const char * header_filename; +static const char copyright_filename [] = "copyright_notice.txt"; // relative to MET_BASE_DIR -extern const char * program_name; +static const char * sep = "////////////////////////////////////////////////////////////////////////\n"; //////////////////////////////////////////////////////////////////////// @@ -111,7 +80,7 @@ char upper[256]; char pound_define[256]; char junk[256]; int len, scope_len, max_len; -char * len_name = (char *) 0; +char * len_name = (char *) NULL; // if ( e.scope() ) snprintf(full_id, sizeof(full_id), "%s::%s", e.scope(), e.id(j)); @@ -121,12 +90,12 @@ max_len = 0; -if ( e.scope() ) scope_len = strlen(e.scope()) + 2; // includes "::" +if ( e.scope() ) scope_len = m_strlen(e.scope()) + 2; // includes "::" else scope_len = 0; for (j=0; j<(e.n_ids()); ++j) { - len = strlen(e.id(j)); + len = m_strlen(e.id(j)); len += scope_len; @@ -265,11 +234,6 @@ if ( do_array ) { } -// f << "#endif // " << pound_define << "\n" -// << "\n\n" -// << sep -// << "\n\n"; - f << "#endif"; if ( echo_pound_define_after_endif ) f << " /* " << pound_define << " */\n"; @@ -285,7 +249,7 @@ f << "\n\n" f.close(); -if ( len_name ) { delete [] len_name; len_name = (char *) 0; } +if ( len_name ) { delete [] len_name; len_name = (char *) NULL; } return; @@ -301,7 +265,6 @@ void write_cs_header(const EnumInfo & e) if ( e.n_ids() == 0 ) return; -int j; ofstream f; char filename[256]; char lower[256]; @@ -309,7 +272,7 @@ char upper[256]; char pound_define[256]; char junk[256]; int len, scope_len, max_len; -char * len_name = (char *) 0; +char * len_name = (char *) NULL; // if ( e.scope() ) snprintf(full_id, sizeof(full_id), "%s::%s", e.scope(), e.id(j)); @@ -319,12 +282,12 @@ max_len = 0; -if ( e.scope() ) scope_len = strlen(e.scope()) + 2; // includes "::" +if ( e.scope() ) scope_len = m_strlen(e.scope()) + 2; // includes "::" else scope_len = 0; -for (j=0; j<(e.n_ids()); ++j) { +for (int j=0; j<(e.n_ids()); ++j) { - len = strlen(e.id(j)); + len = m_strlen(e.id(j)); len += scope_len; @@ -486,7 +449,7 @@ f << "\n\n" f.close(); -if ( len_name ) { delete [] len_name; len_name = (char *) 0; } +if ( len_name ) { delete [] len_name; len_name = (char *) NULL; } return; @@ -766,7 +729,7 @@ for (j=0; j<(e.n_ids()); ++j) { if ( e.scope() ) snprintf(full_id, sizeof(full_id), "%s::%s", e.scope(), e.id(j)); else snprintf(full_id, sizeof(full_id), "%s", e.id(j)); - k = strlen(e.id(j)); + k = m_strlen(e.id(j)); out << " " << "case " << full_id << ": "; @@ -850,7 +813,7 @@ for (j=0; j<(e.n_ids()); ++j) { if ( e.scope() ) snprintf(full_id, sizeof(full_id), "%s::%s", e.scope(), e.id(j)); else snprintf(full_id, sizeof(full_id), "%s", e.id(j)); - k = strlen(e.id(j)); + k = m_strlen(e.id(j)); out << " " << "case " << full_id << ": "; @@ -927,7 +890,7 @@ n = e.n_ids(); for (j=0; j= 0) && (header_filename[j] != '/') ) --j; @@ -1118,10 +1081,11 @@ void patch_name(char * len_name) int j, n; int pos; -char * new_name = (char *) 0; +char * new_name = (char *) NULL; char c; +const char *method_name = "patch_name() -> "; -n = strlen(len_name); +n = m_strlen(len_name); new_name = new char [n + 1]; @@ -1151,14 +1115,14 @@ for (j=0; j= 0) && (argv[0][j] != '/') ) --j; @@ -395,7 +395,7 @@ void yyerror(const char * s) int c; -c = (int) (column - strlen(yytext)); +c = (int) (column - m_strlen(yytext)); cout << "\n\n " << program_name << " -> " << "syntax error in file \"" << header_filename << "\" at line " << LineNumber diff --git a/met/src/basic/enum_to_string/enum_to_string.h b/met/src/basic/enum_to_string/enum_to_string.h new file mode 100644 index 0000000000..0d2baf1c68 --- /dev/null +++ b/met/src/basic/enum_to_string/enum_to_string.h @@ -0,0 +1,55 @@ +// *=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=* +// ** Copyright UCAR (c) 1992 - 2021 +// ** University Corporation for Atmospheric Research (UCAR) +// ** National Center for Atmospheric Research (NCAR) +// ** Research Applications Lab (RAL) +// ** P.O.Box 3000, Boulder, Colorado, 80307-3000, USA +// *=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=* + + +//////////////////////////////////////////////////////////////////////// + + +#ifndef __ENUM_TO_STRING_H__ +#define __ENUM_TO_STRING_H__ + + +//////////////////////////////////////////////////////////////////////// + + +#include "vx_cal.h" + + +//////////////////////////////////////////////////////////////////////// + + +extern bool do_prefix; + +extern const char * header_suffix; + +extern bool do_angle_brackets; + +extern bool do_array; + +extern bool do_reverse; + +extern bool do_concat_string; + +extern bool verbose; + +extern unixtime generation_gmt; + +extern const char * header_filename; + +extern const char * program_name; + + +//////////////////////////////////////////////////////////////////////// + + +#endif /* __ENUM_TO_STRING_H__ */ + + +//////////////////////////////////////////////////////////////////////// + + diff --git a/met/src/basic/enum_to_string/info.cc b/met/src/basic/enum_to_string/info.cc index ba716a3ce1..52c5ab8471 100644 --- a/met/src/basic/enum_to_string/info.cc +++ b/met/src/basic/enum_to_string/info.cc @@ -20,6 +20,7 @@ using namespace std; #include #include "info.h" +#include "str_wrappers.h" //////////////////////////////////////////////////////////////////////// @@ -93,17 +94,17 @@ void EnumInfo::init_from_scratch() { -s = (char **) 0; +s = (char **) NULL; -Name = (char *) 0; +Name = (char *) NULL; -LowerCaseName = (char *) 0; +LowerCaseName = (char *) NULL; -Scope = (char *) 0; +Scope = (char *) NULL; -U_Scope = (char *) 0; +U_Scope = (char *) NULL; -Header = (char *) 0; +Header = (char *) NULL; Nalloc = Nids = 0; @@ -126,21 +127,21 @@ int j; for (j=0; j max_len ) max_len = k; @@ -287,14 +288,15 @@ void EnumInfo::add_id(const char * text) { int k; +const char *method_name = "EnumInfo::add_id() -> "; extend(Nids + 1); -k = strlen(text); +k = m_strlen(text); s[Nids] = new char [1 + k]; -strncpy(s[Nids], text, k); +m_strncpy(s[Nids], text, k, method_name); s[Nids][k] = (char) 0; // just to make sure @@ -311,26 +313,27 @@ return; void EnumInfo::set_name(const char * text) { +const char *method_name = "EnumInfo::set_name() -> "; -if ( Name ) { delete [] Name; Name = (char *) 0; } +if ( Name ) { delete [] Name; Name = (char *) NULL; } -if ( LowerCaseName ) { delete [] LowerCaseName; LowerCaseName = (char *) 0; } +if ( LowerCaseName ) { delete [] LowerCaseName; LowerCaseName = (char *) NULL; } if ( !text ) return; int j, k; -k = strlen(text); +k = m_strlen(text); Name = new char [1 + k]; LowerCaseName = new char [1 + k]; -strncpy(Name, text, k); +m_strncpy(Name, text, k, method_name, "Name"); Name[k] = (char) 0; // just to make sure -strncpy(LowerCaseName, text, k); +m_strncpy(LowerCaseName, text, k, method_name, "LowerCaseName"); LowerCaseName[k] = (char) 0; // just to make sure @@ -349,12 +352,12 @@ return; void EnumInfo::set_scope(const char * text) - { +const char *method_name = "EnumInfo::set_scope() -> "; -if ( Scope ) { delete [] Scope; Scope = (char *) 0; } +if ( Scope ) { delete [] Scope; Scope = (char *) NULL; } -if ( U_Scope ) { delete [] U_Scope; U_Scope = (char *) 0; } +if ( U_Scope ) { delete [] U_Scope; U_Scope = (char *) NULL; } if ( !text ) return; @@ -362,11 +365,11 @@ int j, k, m; char c; -k = strlen(text); +k = m_strlen(text); Scope = new char [1 + k]; -strncpy(Scope, text, k); +m_strncpy(Scope, text, k, method_name); Scope[k] = (char) 0; // just to make sure @@ -399,16 +402,17 @@ return; void EnumInfo::set_header(const char * text) { +const char *method_name = "EnumInfo::set_header() -> "; -if ( Header ) { delete [] Header; Header = (char *) 0; } +if ( Header ) { delete [] Header; Header = (char *) NULL; } int k; -k = strlen(text); +k = m_strlen(text); Header = new char [1 + k]; -strncpy(Header, text, k); +m_strncpy(Header, text, k, method_name); Header[k] = (char) 0; // just to make sure diff --git a/met/src/basic/enum_to_string/my_enum_scanner.cc b/met/src/basic/enum_to_string/my_enum_scanner.cc index 65b89273ab..fd03afccf8 100644 --- a/met/src/basic/enum_to_string/my_enum_scanner.cc +++ b/met/src/basic/enum_to_string/my_enum_scanner.cc @@ -20,6 +20,7 @@ using namespace std; #include "scope.h" +#include "str_wrappers.h" //////////////////////////////////////////////////////////////////////// @@ -431,10 +432,11 @@ return; int do_id() { +const char *method_name = "do_id()"; if ( enum_mode || last_was_enum || last_was_class ) { - strncpy(yylval.name, yytext, sizeof(yylval.name)); + m_strncpy(yylval.name, yytext, sizeof(yylval.name), method_name); return ( 1 ); diff --git a/met/src/basic/enum_to_string/scope.cc b/met/src/basic/enum_to_string/scope.cc index 703d3b5215..86d094d3b0 100644 --- a/met/src/basic/enum_to_string/scope.cc +++ b/met/src/basic/enum_to_string/scope.cc @@ -20,6 +20,8 @@ using namespace std; #include #include "scope.h" +#include "str_wrappers.h" + //////////////////////////////////////////////////////////////////////// @@ -129,7 +131,7 @@ return; void ScopeStackElement::set_name(const char * text) { - +const char *method_name = "void ScopeStackElement::set_name(const char *) -> "; if ( Name ) { delete [] Name; Name = (const char *) 0; } if ( !text ) return; @@ -137,19 +139,19 @@ if ( !text ) return; int k; char * c = (char *) 0; -k = strlen(text); +k = m_strlen(text); c = new char [1 + k]; if ( !c ) { - cerr << "\n\n void ScopeStackElement::set_name(const char *) -> memory allocation error\n\n"; + cerr << "\n\n " << method_name << "memory allocation error\n\n"; exit ( 1 ); } -strncpy(c, text, k); +m_strncpy(c, text, k, method_name); c[k] = (char) 0; // just to make sure diff --git a/met/src/basic/vx_cal/is_dst.cc b/met/src/basic/vx_cal/is_dst.cc index 57b9b2dfb0..94fb1be5a4 100644 --- a/met/src/basic/vx_cal/is_dst.cc +++ b/met/src/basic/vx_cal/is_dst.cc @@ -36,7 +36,7 @@ struct DstInfo { }; -static DstInfo dst_info[] = { +const static DstInfo dst_info[] = { { 4, 2, 10, 29 }, // 0 { 4, 1, 10, 28 }, // 1 diff --git a/met/src/basic/vx_cal/time_array.cc b/met/src/basic/vx_cal/time_array.cc index cbbf8aa8eb..2dab0d0d9c 100644 --- a/met/src/basic/vx_cal/time_array.cc +++ b/met/src/basic/vx_cal/time_array.cc @@ -98,7 +98,7 @@ void TimeArray::init_from_scratch() { -e = (unixtime*) 0; +e = (unixtime*) NULL; clear(); @@ -114,7 +114,7 @@ void TimeArray::clear() { -if ( e ) { delete [] e; e = (unixtime *) 0; } +if ( e ) { delete [] e; e = (unixtime *) NULL; } Nelements = Nalloc = 0; @@ -194,7 +194,7 @@ if ( ! exact ) { } -unixtime * u = (unixtime *) 0; +unixtime * u = (unixtime *) NULL; u = new unixtime [n]; @@ -219,11 +219,11 @@ if ( e ) { } - delete [] e; e = (unixtime *) 0; + delete [] e; e = (unixtime *) NULL; } -e = u; u = (unixtime *) 0; +e = u; u = (unixtime *) NULL; Nalloc = n; diff --git a/met/src/basic/vx_cal/time_strings.cc b/met/src/basic/vx_cal/time_strings.cc index d15dea5280..51aec9d8a2 100644 --- a/met/src/basic/vx_cal/time_strings.cc +++ b/met/src/basic/vx_cal/time_strings.cc @@ -532,7 +532,7 @@ if ( !text ) { exit ( 1 ); } -else if ( strlen(text) == 0 ) { +else if ( m_strlen(text) == 0 ) { mlog << Error << "\ntimestring_to_unix(const char *) -> " << "empty time string!\n\n"; @@ -707,7 +707,7 @@ if ( !text ) { exit ( 1 ); } -else if ( strlen(text) == 0 ) { +else if ( m_strlen(text) == 0 ) { mlog << Error << "\ntimestring_to_sec(const char *) -> " << "empty time string!\n\n"; diff --git a/met/src/basic/vx_config/config_util.cc b/met/src/basic/vx_config/config_util.cc index 70f718a90d..094e429bb7 100644 --- a/met/src/basic/vx_config/config_util.cc +++ b/met/src/basic/vx_config/config_util.cc @@ -18,7 +18,6 @@ using namespace std; #include "vx_math.h" #include "vx_util.h" -#include "string_fxns.h" #include "GridTemplate.h" diff --git a/met/src/basic/vx_config/icode.cc b/met/src/basic/vx_config/icode.cc index 6ce8b1f903..b17aac432f 100644 --- a/met/src/basic/vx_config/icode.cc +++ b/met/src/basic/vx_config/icode.cc @@ -1,5 +1,3 @@ - - //////////////////////////////////////////////////////////////////////// @@ -29,7 +27,6 @@ using namespace std; #include "indent.h" #include "icode.h" #include "dictionary.h" -#include "string_fxns.h" #include "celltype_to_string.h" @@ -173,17 +170,13 @@ type = icc.type; if ( type == identifier ) { - name = new char [1 + m_strlen(icc.name)]; - - m_strcpy(name, icc.name, method_name, "name"); + name = m_strcpy2(icc.name, method_name, "name"); } if ( type == character_string ) { - text = new char [1 + m_strlen(icc.text)]; - - m_strcpy(text, icc.text, method_name, "text"); + text = m_strcpy2(icc.text, method_name, "text"); } @@ -422,9 +415,7 @@ clear(); type = identifier; -name = new char [1 + m_strlen(Text)]; - -m_strcpy(name, Text, method_name); +name = m_strcpy2(Text, method_name); return; @@ -443,15 +434,9 @@ const char *method_name = "IcodeCell::set_string() -> "; clear(); -const int n = m_strlen(Text); - type = character_string; -text = new char [1 + n]; - -m_strcpy(text, Text, method_name); - -text[n] = 0; +text = m_strcpy2(Text, method_name); return; diff --git a/met/src/basic/vx_config/my_config_scanner.cc b/met/src/basic/vx_config/my_config_scanner.cc index 376fa10f5f..72c7e06e72 100644 --- a/met/src/basic/vx_config/my_config_scanner.cc +++ b/met/src/basic/vx_config/my_config_scanner.cc @@ -26,7 +26,6 @@ using namespace std; #include "math_constants.h" #include "util_constants.h" #include "is_number.h" -#include "string_fxns.h" #include "dictionary.h" #include "builtin.h" @@ -1028,7 +1027,11 @@ if ( c == '$' ) { memset(env_name, 0, sizeof(env_name)); - while ( (env_pos < max_id_length) && ((c = fgetc(configin)) != R_curly) ) env_name[env_pos++] = (char) c; + // SonarQube: to avoid side effect by || operator + while (env_pos < max_id_length) { + if ((c = fgetc(configin)) == R_curly) break; + env_name[env_pos++] = (char) c; + } e = getenv ( env_name ); diff --git a/met/src/basic/vx_log/Makefile.am b/met/src/basic/vx_log/Makefile.am index 710c1c5843..9e4ba32cf2 100644 --- a/met/src/basic/vx_log/Makefile.am +++ b/met/src/basic/vx_log/Makefile.am @@ -16,6 +16,7 @@ libvx_log_a_SOURCES = concat_string.cc concat_string.h \ indent.cc indent.h \ logger.cc logger.h \ string_array.cc string_array.h \ + str_wrappers.cc str_wrappers.h \ vx_log.h # Build the library when making a distribution so that diff --git a/met/src/basic/vx_log/concat_string.cc b/met/src/basic/vx_log/concat_string.cc index d35497884c..2f4e6550dd 100644 --- a/met/src/basic/vx_log/concat_string.cc +++ b/met/src/basic/vx_log/concat_string.cc @@ -22,6 +22,7 @@ using namespace std; #include "concat_string.h" #include "logger.h" +#include "str_wrappers.h" //////////////////////////////////////////////////////////////////////// @@ -76,7 +77,7 @@ ConcatString::~ConcatString() { clear(); -delete s; +if (s) delete s; } @@ -333,7 +334,7 @@ void ConcatString::chomp(const char c) void ConcatString::chomp(const char * suffix) { - size_t limit = length() - strlen(suffix); + size_t limit = length() - m_strlen(suffix); size_t pos = s->find(suffix, limit); if (pos != string::npos) s->erase(pos); } @@ -400,7 +401,7 @@ void ConcatString::elim_trailing_whitespace() bool ConcatString::startswith(const char * Text) const { - size_t pos = s->rfind(Text, strlen(Text)); + size_t pos = s->rfind(Text, m_strlen(Text)); return (pos != string::npos); } @@ -410,7 +411,7 @@ bool ConcatString::startswith(const char * Text) const bool ConcatString::endswith(const char * Text) const { - size_t pos = s->find(Text, s->length() - strlen(Text)); + size_t pos = s->find(Text, s->length() - m_strlen(Text)); return (pos != string::npos); } @@ -539,7 +540,7 @@ int ConcatString::format(const char *fmt, ...) { va_list vl; int status = -1; - char *tmp; + char *tmp = NULL; va_start(vl, fmt); status = vasprintf(&tmp, fmt, vl); @@ -587,7 +588,7 @@ void ConcatString::replace(const char * target, const char * replacement, size_t pos; while ((pos = s->find(target)) != string::npos) { - s->replace(pos, strlen(target), replacement); + s->replace(pos, m_strlen(target), replacement); } } @@ -1189,7 +1190,7 @@ bool is_empty(const char * text) { -return ( (text == NULL) || (*text == 0) || (strlen(text) == 0)); +return ( (text == NULL) || (*text == 0) || (m_strlen(text) == 0)); } @@ -1208,10 +1209,9 @@ static const char *method_name = "get_env() "; // Initialize env_value.clear(); -if (str.find('/') != string::npos || - (ptr = getenv(env_name)) == NULL) { - return(false); -} +// SonarQube: two ifs to avoid the side effect by the logical || operator +if (str.find('/') != string::npos) return(false); +if ((ptr = getenv(env_name)) == NULL) return(false); env_value = ptr; str = env_value; diff --git a/met/src/basic/vx_log/str_wrappers.cc b/met/src/basic/vx_log/str_wrappers.cc new file mode 100644 index 0000000000..81d707c5a7 --- /dev/null +++ b/met/src/basic/vx_log/str_wrappers.cc @@ -0,0 +1,102 @@ +// *=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=* +// ** Copyright UCAR (c) 1992 - 2021 +// ** University Corporation for Atmospheric Research (UCAR) +// ** National Center for Atmospheric Research (NCAR) +// ** Research Applications Lab (RAL) +// ** P.O.Box 3000, Boulder, Colorado, 80307-3000, USA +// *=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=* + + + +//////////////////////////////////////////////////////////////////////// + + +using namespace std; + +#include "logger.h" +#include "str_wrappers.h" + + +//////////////////////////////////////////////////////////////////////// + +int m_strlen(const char *str) { + int str_len = -1; + if (str) str_len = strlen(str); + + return str_len; +} + +//////////////////////////////////////////////////////////////////////// +// to_str should be allocated before calling this. + +void m_strcpy(char *to_str, const char *from_str, const char *method_name, + const char *extra_msg) { + + // Note: recommend to use m_strncpy because there are some cases that + // sizeof(to_str) returns 8 which is not the allocated buffer size. + int str_len = m_strlen(from_str); + m_strncpy(to_str, from_str, str_len, method_name, extra_msg); + +} + +//////////////////////////////////////////////////////////////////////// +// to_string should not allocated. This allocates and return to_str after copying + +char *m_strcpy2(const char *from_str, const char *method_name, const char *extra_msg) { + char *to_str = (char *) 0; + if (from_str) { + int str_len = m_strlen(from_str); + + to_str = new char[str_len + 1]; + + if(!to_str) { + mlog << Error << "\n" << method_name + << "memory allocation error (m_strcpy)" + << (extra_msg == 0 ? "" : extra_msg) << "\n\n"; + exit(1); + } + + m_strncpy(to_str, from_str, str_len, method_name, extra_msg); + } + else { + mlog << Error << "\n" << method_name + << " Do not copy the string because a from_string is NULL. " + << (extra_msg == 0 ? "" : extra_msg) << "\n\n"; + } + + return to_str; +} + +//////////////////////////////////////////////////////////////////////// + +void m_strncpy(char *to_str, const char *from_str, const int buf_len, + const char *method_name, const char *extra_msg, bool truncate) { + if (!from_str){ + mlog << Warning << "\n" << method_name + << " Do not copy the string because a from_string is NULL. " + << (extra_msg == 0 ? "" : extra_msg) << "\n\n"; + } + else if (!to_str){ + mlog << Warning << "\n" << method_name + << " Do not copy the string because a to_string is NULL. " + << (extra_msg == 0 ? "" : extra_msg) << "\n\n"; + } + else { // (from_str && to_str) + int str_len = m_strlen(from_str); + if (str_len > buf_len) str_len = buf_len; + + memset(to_str, 0, str_len); + string temp_str = from_str; + temp_str.copy(to_str, str_len); + to_str[str_len] = 0; + + if (!truncate && strcmp(from_str, to_str)) { + mlog << Warning << "\n" << method_name + << " truncated a string " << (extra_msg == 0 ? " " : extra_msg) + << " from \"" << from_str << "\" to \"" << to_str << "\"\n\n"; + } + } + +} + + diff --git a/met/src/basic/vx_log/str_wrappers.h b/met/src/basic/vx_log/str_wrappers.h new file mode 100644 index 0000000000..5d74eef860 --- /dev/null +++ b/met/src/basic/vx_log/str_wrappers.h @@ -0,0 +1,40 @@ +// *=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=* +// ** Copyright UCAR (c) 1992 - 2021 +// ** University Corporation for Atmospheric Research (UCAR) +// ** National Center for Atmospheric Research (NCAR) +// ** Research Applications Lab (RAL) +// ** P.O.Box 3000, Boulder, Colorado, 80307-3000, USA +// *=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=* + + + +//////////////////////////////////////////////////////////////////////// + + +#ifndef __STR_WRAPPERS_H__ +#define __STR_WRAPPERS_H__ + + +//////////////////////////////////////////////////////////////////////// + +extern int m_strlen(const char *str); + +extern void m_strcpy(char *to_str, const char *from_str, const char *method_name, + const char *extra_msg=(char *)0); +extern char *m_strcpy2(const char *from_str, const char *method_name, + const char *extra_msg=(char *)0); +extern void m_strncpy(char *to_str, const char *from_str, const int buf_len, + const char *method_name, const char *extra_msg=(char *)0, + bool truncate=false); + +//////////////////////////////////////////////////////////////////////// + + +#endif // __STR_WRAPPERS_H__ + + +//////////////////////////////////////////////////////////////////////// + + + + diff --git a/met/src/basic/vx_log/vx_log.h b/met/src/basic/vx_log/vx_log.h index 3dfa05f868..7ffb9d3ab8 100644 --- a/met/src/basic/vx_log/vx_log.h +++ b/met/src/basic/vx_log/vx_log.h @@ -24,7 +24,7 @@ using namespace std; #include "string_array.h" #include "file_fxns.h" #include "logger.h" - +#include "str_wrappers.h" //////////////////////////////////////////////////////////////////////// diff --git a/met/src/basic/vx_util/ascii_table.cc b/met/src/basic/vx_util/ascii_table.cc index 8e45eed53d..28fb9004d6 100644 --- a/met/src/basic/vx_util/ascii_table.cc +++ b/met/src/basic/vx_util/ascii_table.cc @@ -26,7 +26,6 @@ using namespace std; #include "ascii_table.h" #include "comma_string.h" #include "fix_float.h" -#include "string_fxns.h" #include "util_constants.h" //////////////////////////////////////////////////////////////////////// diff --git a/met/src/basic/vx_util/filename_suffix.cc b/met/src/basic/vx_util/filename_suffix.cc index 0a050964cc..1c20b292bf 100644 --- a/met/src/basic/vx_util/filename_suffix.cc +++ b/met/src/basic/vx_util/filename_suffix.cc @@ -19,6 +19,7 @@ using namespace std; #include #include +#include "str_wrappers.h" #include "string_fxns.h" #include "filename_suffix.h" #include "concat_string.h" diff --git a/met/src/basic/vx_util/get_filenames.cc b/met/src/basic/vx_util/get_filenames.cc index b777c78455..d0067fa437 100644 --- a/met/src/basic/vx_util/get_filenames.cc +++ b/met/src/basic/vx_util/get_filenames.cc @@ -29,7 +29,6 @@ using namespace std; #include "vx_log.h" #include "get_filenames.h" -#include "string_fxns.h" //////////////////////////////////////////////////////////////////////// diff --git a/met/src/basic/vx_util/is_number.cc b/met/src/basic/vx_util/is_number.cc index 4cdebd6d9a..f7bb865cb3 100644 --- a/met/src/basic/vx_util/is_number.cc +++ b/met/src/basic/vx_util/is_number.cc @@ -21,7 +21,7 @@ using namespace std; #include #include "is_number.h" -#include "string_fxns.h" +#include "str_wrappers.h" #include "substring.h" diff --git a/met/src/basic/vx_util/make_path.cc b/met/src/basic/vx_util/make_path.cc index 9fd81b7b5b..53dcb632ca 100644 --- a/met/src/basic/vx_util/make_path.cc +++ b/met/src/basic/vx_util/make_path.cc @@ -26,7 +26,6 @@ using namespace std; #include "make_path.h" #include "vx_log.h" -#include "string_fxns.h" //////////////////////////////////////////////////////////////////////// diff --git a/met/src/basic/vx_util/ordinal.cc b/met/src/basic/vx_util/ordinal.cc index fde21f237d..87ec74679e 100644 --- a/met/src/basic/vx_util/ordinal.cc +++ b/met/src/basic/vx_util/ordinal.cc @@ -21,7 +21,6 @@ using namespace std; #include #include "ordinal.h" -#include "string_fxns.h" #include "vx_log.h" diff --git a/met/src/basic/vx_util/roman_numeral.cc b/met/src/basic/vx_util/roman_numeral.cc index 83b90b8cf4..02bc520282 100644 --- a/met/src/basic/vx_util/roman_numeral.cc +++ b/met/src/basic/vx_util/roman_numeral.cc @@ -21,7 +21,6 @@ using namespace std; #include #include "roman_numeral.h" -#include "string_fxns.h" #include "vx_log.h" diff --git a/met/src/basic/vx_util/string_fxns.cc b/met/src/basic/vx_util/string_fxns.cc index 8e477d073c..8817880053 100644 --- a/met/src/basic/vx_util/string_fxns.cc +++ b/met/src/basic/vx_util/string_fxns.cc @@ -31,8 +31,6 @@ using namespace std; #include "vx_log.h" -const bool ENHANCE_STR_APIS = false; - //////////////////////////////////////////////////////////////////////// @@ -114,7 +112,7 @@ const char * get_short_name(const char * path) { -const char * short_name = (const char *) 0; +const char * short_name = (const char *) NULL; if ( path ) { int j; @@ -140,7 +138,7 @@ return ( short_name ); void append_char(char *str, const char c) { - char *ptr = (char *) 0; + char *ptr = (char *) NULL; // // If the specified characater does not already exist at the @@ -166,7 +164,7 @@ void append_char(char *str, const char c) void strip_char(char *str, const char c) { - char *ptr = (char *) 0; + char *ptr = (char *) NULL; // // If the specified character exists at the end of the string, @@ -188,9 +186,9 @@ void strip_char(char *str, const char c) int num_tokens(const char *test_str, const char *separator) { - int n; - char *temp_str = (char *) 0; - char *c = (char *) 0; + int n = 0; + char *temp_str = (char *) NULL; + char *c = (char *) NULL; const char *method_name = "num_tokens() -> "; // @@ -216,18 +214,19 @@ int num_tokens(const char *test_str, const char *separator) // // Check for an empty string // - if(!c) { delete [] temp_str; temp_str = 0; return(0); } - else n = 1; - - // - // Parse remaining tokens - // - // - while((c = strtok(0, separator)) != NULL) n++; + if(c) { + n = 1; + + // + // Parse remaining tokens + // + // + while((c = strtok(0, separator)) != NULL) n++; + } } - if(temp_str) { delete [] temp_str; temp_str = (char *) 0; } + if(temp_str) { delete [] temp_str; temp_str = (char *) NULL; } return(n); } @@ -298,8 +297,8 @@ int regex_apply(const char* pat, int num_mat, const char* str, char** &mat) for(int i=0; i < num_act; i++){ int mat_len = pmatch[i].rm_eo - pmatch[i].rm_so; mat[i] = new char[mat_len + 1]; - m_strcpy(mat[i], str_dat.substr(pmatch[i].rm_so, mat_len).data(), - method_name, "mat[i]"); + m_strncpy(mat[i], str_dat.substr(pmatch[i].rm_so, mat_len).data(), + mat_len, method_name, "mat[i]"); } mat[num_act] = NULL; @@ -309,7 +308,7 @@ int regex_apply(const char* pat, int num_mat, const char* str, char** &mat) } regfree(re); - if( re ) { delete re; re = 0; } + if( re ) { delete re; re = NULL; } return num_act; } @@ -384,7 +383,7 @@ ConcatString str_trim(const ConcatString str){ int parse_thresh_index(const char *col_name) { int i = 0; - const char *ptr = (const char *) 0; + const char *ptr = (const char *) NULL; if((ptr = strrchr(col_name, '_')) != NULL) i = atoi(++ptr); else { @@ -397,102 +396,4 @@ int parse_thresh_index(const char *col_name) { return(i); } -//////////////////////////////////////////////////////////////////////// - -int m_strlen(const char *str) { - int str_len = 0; - if (str) str_len = strlen(str); // or use sizeof str; - - return str_len; -} - -//////////////////////////////////////////////////////////////////////// -// to_string is allocated. - -void m_strcpy(char *to_str, const char *from_str, const char *method_name, - const char *extra_msg) { - - 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); - -} - -//////////////////////////////////////////////////////////////////////// -// to_string is not allocated. Allocate to_string and return the to_string after copying - -char *m_strcpy2(const char *from_str, const char *method_name, const char *extra_msg) { - char *to_str = (char *) 0; - if (from_str) { - int str_len = m_strlen(from_str); - - to_str = new char[str_len + 1]; - - if(!to_str) { - mlog << Error << "\n" << method_name - << "memory allocation error (m_strcpy)" - << (extra_msg == 0 ? "" : extra_msg) << "\n\n"; - exit(1); - } - - m_strcpy(to_str, from_str, method_name, extra_msg); - } - else { - mlog << Error << "\n" << method_name - << " Do not copy the string because a from_string is NULL. " - << (extra_msg == 0 ? "" : extra_msg) << "\n\n"; - } - - return to_str; -} - -//////////////////////////////////////////////////////////////////////// - -void m_strncpy(char *to_str, const char *from_str, const int buf_len, - const char *method_name, const char *extra_msg) { - if (!from_str){ - mlog << Warning << "\n" << method_name - << " Do not copy the string because a from_string is NULL. " - << (extra_msg == 0 ? "" : extra_msg) << "\n\n"; - } - else if (!to_str){ - mlog << Warning << "\n" << method_name - << " Do not copy the string because a to_string is NULL. " - << (extra_msg == 0 ? "" : extra_msg) << "\n\n"; - } - else { // (from_str && to_str) - int str_len = m_strlen(from_str); - if (str_len > buf_len) str_len = buf_len; - - memset(to_str, 0, 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); - to_str[str_len] = 0; - } - - if (strcmp(from_str, to_str)) { - mlog << Warning << "\n" << method_name - << " truncated a string " << (extra_msg == 0 ? "" : extra_msg) - << " from \"" << from_str << "\" to \"" << to_str << "\"\n\n"; - } - } - -} - - //////////////////////////////////////////////////////////////////////// diff --git a/met/src/basic/vx_util/string_fxns.h b/met/src/basic/vx_util/string_fxns.h index b64edbbb86..84db62c4b1 100644 --- a/met/src/basic/vx_util/string_fxns.h +++ b/met/src/basic/vx_util/string_fxns.h @@ -59,15 +59,6 @@ extern ConcatString str_trim(const ConcatString str); extern int parse_thresh_index(const char *str); -extern int m_strlen(const char *str); - -extern void m_strcpy(char *to_str, const char *from_str, const char *method_name, - const char *extra_msg=(char *)0); -extern char *m_strcpy2(const char *from_str, const char *method_name, - const char *extra_msg=(char *)0); -extern void m_strncpy(char *to_str, const char *from_str, const int buf_len, - const char *method_name, const char *extra_msg=(char *)0); - //////////////////////////////////////////////////////////////////////// diff --git a/met/src/basic/vx_util/thresh_array.cc b/met/src/basic/vx_util/thresh_array.cc index 2dd8b1fd9d..75ea29b4ff 100644 --- a/met/src/basic/vx_util/thresh_array.cc +++ b/met/src/basic/vx_util/thresh_array.cc @@ -18,7 +18,6 @@ using namespace std; #include "thresh_array.h" #include "vx_math.h" #include "vx_log.h" -#include "string_fxns.h" //////////////////////////////////////////////////////////////////////// diff --git a/met/src/libcode/vx_afm/afm.cc b/met/src/libcode/vx_afm/afm.cc index 57c168ca4a..d8355916ef 100644 --- a/met/src/libcode/vx_afm/afm.cc +++ b/met/src/libcode/vx_afm/afm.cc @@ -26,7 +26,6 @@ using namespace std; #include "afm_token.h" #include "vx_log.h" -#include "string_fxns.h" //////////////////////////////////////////////////////////////////////// @@ -2284,11 +2283,10 @@ if ( s ) { delete [] s; s = (char *) 0; } if ( !text ) return; -int buf_len = m_strlen(text); -s = new char [1 + buf_len]; -const char *method_name = "set_string()"; +const char *method_name = "afm set_string()"; + +s = m_strcpy2(text, method_name); -m_strcpy(s, text, method_name); return; diff --git a/met/src/libcode/vx_analysis_util/analysis_utils.cc b/met/src/libcode/vx_analysis_util/analysis_utils.cc index 1282aceedf..313b082236 100644 --- a/met/src/libcode/vx_analysis_util/analysis_utils.cc +++ b/met/src/libcode/vx_analysis_util/analysis_utils.cc @@ -29,7 +29,6 @@ StringArray parse_line(const char * line) { -int n; StringArray a; const char * delim = " "; char * c = (char *) 0; @@ -40,13 +39,7 @@ const char *method_name = "parse_line() -> "; // copy the line // -n = m_strlen(line); - -L = new char [1 + n]; - -memset(L, 0, 1 + n); - -m_strcpy(L, line, method_name); +L = m_strcpy2(line, method_name); // // tokenize the line diff --git a/met/src/libcode/vx_color/color.cc b/met/src/libcode/vx_color/color.cc index ffb72de3ad..a15eef78b0 100644 --- a/met/src/libcode/vx_color/color.cc +++ b/met/src/libcode/vx_color/color.cc @@ -454,7 +454,9 @@ void dhsv_to_drgb(double h, double s, double v, double & r, double & g, double & { -if ( is_eq(s, 0.0) ) { r = g = b = v; return; }; +r = g = b = v; // initialze, default is missing at switch statement below + +if ( is_eq(s, 0.0) ) return; int i; double f, p, q, t; diff --git a/met/src/libcode/vx_color/my_color_scanner.cc b/met/src/libcode/vx_color/my_color_scanner.cc index c845be959d..96500f4d37 100644 --- a/met/src/libcode/vx_color/my_color_scanner.cc +++ b/met/src/libcode/vx_color/my_color_scanner.cc @@ -16,7 +16,7 @@ using namespace std; #include -#include "string_fxns.h" +#include "str_wrappers.h" #include "color_parser.h" // this must be included before color_parser_yacc.h #include "color.h" diff --git a/met/src/libcode/vx_data2d/table_lookup.cc b/met/src/libcode/vx_data2d/table_lookup.cc index 7f5264d1ee..ea3385f323 100644 --- a/met/src/libcode/vx_data2d/table_lookup.cc +++ b/met/src/libcode/vx_data2d/table_lookup.cc @@ -843,10 +843,12 @@ bool TableFlatFile::read(const char * filename) ifstream in; ConcatString line; int n_lines; +bool status = false; +const char *method_name = "TableFlatFile::read(const char *) -> "; if ( empty(filename) ) { - mlog << Error << "\nTableFlatFile::read(const char *) ->" + mlog << Error << "\n" << method_name << "empty filename!\n\n"; exit ( 1 ); @@ -863,7 +865,7 @@ met_open(in, filename); if ( !in ) { - mlog << Error << "\nTableFlatFile::read(const char *) -> " + mlog << Error << "\n" << method_name << "unable to open input file \"" << filename << "\"\n\n"; exit ( 1 ); @@ -880,11 +882,11 @@ line.chomp('\n'); line.ws_strip(); - if ( line == "GRIB1" ) { return ( read_grib1(in, filename, n_lines - 1) ); } -else if ( line == "GRIB2" ) { return ( read_grib2(in, filename, n_lines - 1) ); } + if ( line == "GRIB1" ) status = read_grib1(in, filename, n_lines - 1); +else if ( line == "GRIB2" ) status = read_grib2(in, filename, n_lines - 1); else { - mlog << Error << "\nTableFlatFile::read(const char *) -> " + mlog << Error << "\n" << method_name << "unable unrecognized format spec \"" << line << "\" in file \"" << filename << "\"\n\n"; @@ -898,7 +900,7 @@ else { in.close(); -return ( false ); +return ( status ); } diff --git a/met/src/libcode/vx_data2d_factory/is_bufr_file.cc b/met/src/libcode/vx_data2d_factory/is_bufr_file.cc index 7ecfd1d5b5..78773558d7 100644 --- a/met/src/libcode/vx_data2d_factory/is_bufr_file.cc +++ b/met/src/libcode/vx_data2d_factory/is_bufr_file.cc @@ -1,5 +1,3 @@ - - // *=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=* // ** Copyright UCAR (c) 1992 - 2021 // ** University Corporation for Atmospheric Research (UCAR) @@ -27,7 +25,6 @@ using namespace std; #include "is_bufr_file.h" #include "vx_log.h" -#include "string_fxns.h" //////////////////////////////////////////////////////////////////////// diff --git a/met/src/libcode/vx_data2d_factory/parse_file_list.cc b/met/src/libcode/vx_data2d_factory/parse_file_list.cc index b8cd615fd2..cd01087acc 100644 --- a/met/src/libcode/vx_data2d_factory/parse_file_list.cc +++ b/met/src/libcode/vx_data2d_factory/parse_file_list.cc @@ -30,7 +30,6 @@ using namespace std; #include "parse_file_list.h" #include "data2d_factory_utils.h" -#include "string_fxns.h" //////////////////////////////////////////////////////////////////////// diff --git a/met/src/libcode/vx_data2d_grib/data2d_grib_utils.cc b/met/src/libcode/vx_data2d_grib/data2d_grib_utils.cc index 4ef896f158..a1f01334a8 100644 --- a/met/src/libcode/vx_data2d_grib/data2d_grib_utils.cc +++ b/met/src/libcode/vx_data2d_grib/data2d_grib_utils.cc @@ -21,7 +21,6 @@ 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" diff --git a/met/src/libcode/vx_data2d_grib/grib_classes.cc b/met/src/libcode/vx_data2d_grib/grib_classes.cc index 51485d38c2..e76e9d2631 100644 --- a/met/src/libcode/vx_data2d_grib/grib_classes.cc +++ b/met/src/libcode/vx_data2d_grib/grib_classes.cc @@ -1358,9 +1358,13 @@ void GribFile::close() { -if ( rep && (--rep->referenceCount == 0) ) delete rep; +if ( rep ) { -rep = (GribFileRep *) 0; + if (--rep->referenceCount == 0) delete rep; + + rep = (GribFileRep *) NULL; + +} return; diff --git a/met/src/libcode/vx_data2d_grib/grib_strings.cc b/met/src/libcode/vx_data2d_grib/grib_strings.cc index 52ab29462e..d45177e832 100644 --- a/met/src/libcode/vx_data2d_grib/grib_strings.cc +++ b/met/src/libcode/vx_data2d_grib/grib_strings.cc @@ -275,7 +275,7 @@ int str_to_grib_code(const char *c, int &pcode, // PROB(string>lo) // PROB(string') n_gt++; } - m_strcpy(tmp_str, c, method_name); + m_strncpy(tmp_str, c, buf_len, method_name); // Single inequality if(n_lt + n_gt == 1) { diff --git a/met/src/libcode/vx_data2d_grib2/data2d_grib2.cc b/met/src/libcode/vx_data2d_grib2/data2d_grib2.cc index 82168ac52b..20dc3681d2 100644 --- a/met/src/libcode/vx_data2d_grib2/data2d_grib2.cc +++ b/met/src/libcode/vx_data2d_grib2/data2d_grib2.cc @@ -27,7 +27,6 @@ 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" diff --git a/met/src/libcode/vx_data2d_nc_pinterp/pinterp_file.cc b/met/src/libcode/vx_data2d_nc_pinterp/pinterp_file.cc index a84210752f..03653ec6af 100644 --- a/met/src/libcode/vx_data2d_nc_pinterp/pinterp_file.cc +++ b/met/src/libcode/vx_data2d_nc_pinterp/pinterp_file.cc @@ -1,5 +1,3 @@ - - // *=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=* // ** Copyright UCAR (c) 1992 - 2021 // ** University Corporation for Atmospheric Research (UCAR) diff --git a/met/src/libcode/vx_data2d_nccf/nccf_file.cc b/met/src/libcode/vx_data2d_nccf/nccf_file.cc index 5bc0fea1d1..80ae51e415 100644 --- a/met/src/libcode/vx_data2d_nccf/nccf_file.cc +++ b/met/src/libcode/vx_data2d_nccf/nccf_file.cc @@ -740,7 +740,7 @@ void NcCfFile::dump(ostream & out, int depth) const if (AccumTime > 0) { unix_to_mdyhms(AccumTime, month, day, year, hour, minute, second); snprintf(junk, sizeof(junk), "%2d:%02d:%02d (%d seconds)", - hour, minute, second, AccumTime); + hour, minute, second, (int)AccumTime); out << prefix << "Accum Time = "; out << junk << "\n"; out << prefix << "\n"; @@ -1124,7 +1124,6 @@ bool NcCfFile::getData(NcVar * v, const LongArray & a, DataPlane & plane) const << " bad type [" << GET_NC_TYPE_NAME_P(v) << "] for variable \"" << (GET_NC_NAME_P(v)) << "\"\n\n"; exit ( 1 ); - break; } // switch diff --git a/met/src/libcode/vx_data2d_python/data2d_python.cc b/met/src/libcode/vx_data2d_python/data2d_python.cc index 56998a706f..e246dc8ca2 100644 --- a/met/src/libcode/vx_data2d_python/data2d_python.cc +++ b/met/src/libcode/vx_data2d_python/data2d_python.cc @@ -180,7 +180,7 @@ if ( file_argc > 0 ) { for ( i=0; i #include "vx_log.h" -#include "string_fxns.h" #include "ps_text.h" @@ -323,7 +322,7 @@ Nalloc = n; // copy the old values, if any // -if ( Text ) m_strcpy(u, Text, method_name, "Text"); +if ( Text ) m_strncpy(u, Text, (n-1), method_name, "Text"); // // toss old, grab new @@ -358,7 +357,7 @@ Nchars = m_strlen(s); extend(Nchars + 2); -if (Text) m_strcpy(Text, s, method_name, "Text"); +if (Text) m_strncpy(Text, s, Nalloc, method_name, "Text"); return; diff --git a/met/src/libcode/vx_pxm/pbm.cc b/met/src/libcode/vx_pxm/pbm.cc index d38539a330..7271fa570e 100644 --- a/met/src/libcode/vx_pxm/pbm.cc +++ b/met/src/libcode/vx_pxm/pbm.cc @@ -25,7 +25,6 @@ using namespace std; #include #include "vx_log.h" -#include "string_fxns.h" #include "pbm.h" #include "pxm_utils.h" diff --git a/met/src/libcode/vx_pxm/pcm.cc b/met/src/libcode/vx_pxm/pcm.cc index 6b96bf5ae4..299c931211 100644 --- a/met/src/libcode/vx_pxm/pcm.cc +++ b/met/src/libcode/vx_pxm/pcm.cc @@ -29,7 +29,6 @@ using namespace std; #include "vx_log.h" #include "vx_math.h" -#include "string_fxns.h" //////////////////////////////////////////////////////////////////////// diff --git a/met/src/libcode/vx_pxm/pgm.cc b/met/src/libcode/vx_pxm/pgm.cc index 9b73d3b4df..17ae16aefb 100644 --- a/met/src/libcode/vx_pxm/pgm.cc +++ b/met/src/libcode/vx_pxm/pgm.cc @@ -28,7 +28,6 @@ using namespace std; #include "vx_log.h" #include "vx_math.h" -#include "string_fxns.h" //////////////////////////////////////////////////////////////////////// diff --git a/met/src/libcode/vx_pxm/ppm.cc b/met/src/libcode/vx_pxm/ppm.cc index a5df76dc77..9a33b4b28a 100644 --- a/met/src/libcode/vx_pxm/ppm.cc +++ b/met/src/libcode/vx_pxm/ppm.cc @@ -29,7 +29,6 @@ using namespace std; #include "vx_log.h" #include "vx_math.h" -#include "string_fxns.h" //////////////////////////////////////////////////////////////////////// diff --git a/met/src/libcode/vx_pxm/pxm_base.cc b/met/src/libcode/vx_pxm/pxm_base.cc index 1946f1d289..2a668bee88 100644 --- a/met/src/libcode/vx_pxm/pxm_base.cc +++ b/met/src/libcode/vx_pxm/pxm_base.cc @@ -23,7 +23,6 @@ using namespace std; #include "pxm_base.h" #include "vx_log.h" -#include "string_fxns.h" #include "check_endian.h" diff --git a/met/src/libcode/vx_python3_utils/python3_script.cc b/met/src/libcode/vx_python3_utils/python3_script.cc index d9ac7f41e5..e34fb3ed7a 100644 --- a/met/src/libcode/vx_python3_utils/python3_script.cc +++ b/met/src/libcode/vx_python3_utils/python3_script.cc @@ -14,7 +14,6 @@ using namespace std; #include "vx_log.h" #include "empty_string.h" -#include "string_fxns.h" #include "python3_util.h" #include "python3_script.h" @@ -245,10 +244,10 @@ void Python3_Script::import_read_tmp_ascii_py(void) { -ConcatString module; +ConcatString py_module; -module << cs_erase - << replace_path(read_tmp_ascii_py); +py_module << cs_erase + << replace_path(read_tmp_ascii_py); ConcatString command; @@ -256,14 +255,14 @@ run_python_string("import sys"); command << cs_erase << "sys.path.append(\"" - << module.dirname().c_str() + << py_module.dirname().c_str() << "\")"; mlog << Debug(3) << command << "\n"; run_python_string(command.text()); -mlog << Debug(2) << "Importing " << module << "\n"; +mlog << Debug(2) << "Importing " << py_module << "\n"; ConcatString path = "read_tmp_ascii"; diff --git a/met/src/libcode/vx_python3_utils/wchar_argv.cc b/met/src/libcode/vx_python3_utils/wchar_argv.cc index b876073860..9a52363c5b 100644 --- a/met/src/libcode/vx_python3_utils/wchar_argv.cc +++ b/met/src/libcode/vx_python3_utils/wchar_argv.cc @@ -1,5 +1,3 @@ - - //////////////////////////////////////////////////////////////////////// @@ -25,7 +23,6 @@ using namespace std; #include "wchar_argv.h" #include "concat_string.h" -#include "string_fxns.h" #include "vx_log.h" diff --git a/met/src/libcode/vx_statistics/met_stats.cc b/met/src/libcode/vx_statistics/met_stats.cc index 0657e60239..2975b03432 100644 --- a/met/src/libcode/vx_statistics/met_stats.cc +++ b/met/src/libcode/vx_statistics/met_stats.cc @@ -3051,7 +3051,7 @@ int parse_message_type(const char *msg_typ_str, char **&msg_typ_arr) { msg_typ_arr = new char * [n]; // Initialize the temp string for use in tokenizing - m_strcpy(tmp_str, msg_typ_str, method_name); + m_strncpy(tmp_str, msg_typ_str, max_str_len, method_name); // Tokenize the string and store the double values c = strtok(tmp_str, " "); diff --git a/met/src/libcode/vx_statistics/pair_data_ensemble.cc b/met/src/libcode/vx_statistics/pair_data_ensemble.cc index d6869221fc..a305fa64ea 100644 --- a/met/src/libcode/vx_statistics/pair_data_ensemble.cc +++ b/met/src/libcode/vx_statistics/pair_data_ensemble.cc @@ -1793,7 +1793,7 @@ void VxPairDataEnsemble::set_skip_const(bool tf) { double compute_crps_emp(double obs, const NumArray &ens_na) { int i; - double fcst; + double fcst = 0.0; NumArray evals; // Store valid ensemble member values diff --git a/met/src/libcode/vx_tc_util/track_info.cc b/met/src/libcode/vx_tc_util/track_info.cc index 3d95a084e1..3566077b9b 100644 --- a/met/src/libcode/vx_tc_util/track_info.cc +++ b/met/src/libcode/vx_tc_util/track_info.cc @@ -606,11 +606,12 @@ bool TrackInfo::is_match(const TrackInfo &t) const { bool TrackInfo::is_interp() const { const char *s = Technique.c_str(); - - s += (m_strlen(Technique.c_str()) - 1); + int offset = (m_strlen(Technique.c_str()) - 1); // Return true if the last character of the model name is 'I' - return(*s == 'I'); + if (offset < 0) return false; + else return(s[offset] == 'I'); + } //////////////////////////////////////////////////////////////////////// diff --git a/met/src/tools/core/mode/page_1.cc b/met/src/tools/core/mode/page_1.cc index 9b9dc54f63..09ae0ca2da 100644 --- a/met/src/tools/core/mode/page_1.cc +++ b/met/src/tools/core/mode/page_1.cc @@ -413,12 +413,12 @@ text_y -= 2.0*TextSep; // Accumulation time // -m_strcpy(junk, sec_to_hhmmss_colon(eng.fcst_raw->data.accum()).c_str(), - method_name, "fcst_raw->data.accum()"); +m_strncpy(junk, sec_to_hhmmss_colon(eng.fcst_raw->data.accum()).c_str(), + buf_len, method_name, "fcst_raw->data.accum()"); t1.write_xy1_to_cell(9, 1, dx, dy, 0.0, 0.0, junk); -m_strcpy(junk, sec_to_hhmmss_colon(eng.obs_raw->data.accum()).c_str(), - method_name, "obs_raw->data.accum()"); +m_strncpy(junk, sec_to_hhmmss_colon(eng.obs_raw->data.accum()).c_str(), + buf_len, method_name, "obs_raw->data.accum()"); t1.write_xy1_to_cell(9, 2, dx, dy, 0.0, 0.0, junk); nextline(); diff --git a/met/src/tools/dev_utils/insitu_nc_to_ascii.cc b/met/src/tools/dev_utils/insitu_nc_to_ascii.cc index 04efec517e..efc0b22707 100644 --- a/met/src/tools/dev_utils/insitu_nc_to_ascii.cc +++ b/met/src/tools/dev_utils/insitu_nc_to_ascii.cc @@ -69,8 +69,6 @@ int main(int argc, char * argv []) FILE *output_file; if ((output_file = met_fopen(output_filepath, "w")) == 0) { -// mlog << Error << "\n" << method_name << " -> " -// << "error opening output file" << output_filepath << endl; cerr << "Error opening output file: " << output_filepath << endl; exit(1); @@ -89,8 +87,8 @@ int main(int argc, char * argv []) double max_edr; while (input_file.getNextRecord(aircraft_id, time_obs, - latitude, longitude, altitude, - qc_confidence, med_edr, max_edr)) + latitude, longitude, altitude, + qc_confidence, med_edr, max_edr)) { // Construct the time string @@ -98,7 +96,7 @@ int main(int argc, char * argv []) if (time_struct == 0) { // mlog << Error << "\n" << method_name << " -> " -// << "error converting time value to time structure" << endl; +// << "error converting time value to time structure" << endl; cerr << "Error converting time value to time structure" << endl; fclose(output_file); exit(1); @@ -108,23 +106,23 @@ int main(int argc, char * argv []) snprintf(time_obs_string, sizeof(time_obs_string), "%04d%02d%02d_%02d%02d%02d", - time_struct->tm_year + 1900, time_struct->tm_mon + 1, - time_struct->tm_mday, - time_struct->tm_hour, time_struct->tm_min, time_struct->tm_sec); + time_struct->tm_year + 1900, time_struct->tm_mon + 1, + time_struct->tm_mday, + time_struct->tm_hour, time_struct->tm_min, time_struct->tm_sec); // Write the observations. fprintf(output_file, "MEDEDR %10s %s %10.4f %10.4f %8.2f %3d %8.2f %8.2f %8.2f %8.2f\n", - aircraft_id.c_str(), time_obs_string, - latitude, longitude, altitude * FEET_TO_M, - MED_EDR_GRIB_CODE, MISSING_DATA_VALUE, altitude, - qc_confidence, med_edr); + aircraft_id.c_str(), time_obs_string, + latitude, longitude, altitude * FEET_TO_M, + MED_EDR_GRIB_CODE, MISSING_DATA_VALUE, altitude, + qc_confidence, med_edr); fprintf(output_file, "MAXEDR %10s %s %10.4f %10.4f %8.2f %3d %8.2f %8.2f %8.2f %8.2f\n", - aircraft_id.c_str(), time_obs_string, - latitude, longitude, altitude * FEET_TO_M, - MAX_EDR_GRIB_CODE, MISSING_DATA_VALUE, altitude, - qc_confidence, max_edr); + aircraft_id.c_str(), time_obs_string, + latitude, longitude, altitude * FEET_TO_M, + MAX_EDR_GRIB_CODE, MISSING_DATA_VALUE, altitude, + qc_confidence, max_edr); } fclose(output_file); diff --git a/met/src/tools/other/ascii2nc/aeronet_handler.cc b/met/src/tools/other/ascii2nc/aeronet_handler.cc index 1a1fe249a1..ef7025bec9 100644 --- a/met/src/tools/other/ascii2nc/aeronet_handler.cc +++ b/met/src/tools/other/ascii2nc/aeronet_handler.cc @@ -643,7 +643,6 @@ double AeronetHandler::extract_height(string hdr_field) { string height_str = ""; if (string::npos == hdr_field.find("Empty")) { - size_t offset; int token_count = 0; bool with_unit = false; string tmp_height; @@ -652,7 +651,7 @@ double AeronetHandler::extract_height(string hdr_field) { hdr_names.parse_delim(hdr_field.c_str(), "_"); // AOD_1640nm-Total,AOD_1640nm-AOD,AOD_1640nm-Rayleigh,AOD_1640nm-O3, // AOD_1640nm-NO2,AOD_1640nm-CO2,AOD_1640nm-CH4,AOD_1640nm-WaterVapor - if (0 == (offset = hdr_field.find(AOD_NAME))) { + if (0 == hdr_field.find(AOD_NAME)) { with_unit = true; tmp_height = hdr_names[hdr_names.n_elements()-1]; hdr_names.clear(); @@ -660,27 +659,27 @@ double AeronetHandler::extract_height(string hdr_field) { height_str = hdr_names[0]; } // Exact_Wavelengths_for_Input_AOD(um) - else if (0 == (offset = hdr_field.find(WAVELENGTHS_INPUT_AOD_NAME))) { + else if (0 == hdr_field.find(WAVELENGTHS_INPUT_AOD_NAME)) { StringArray tmp_hdr_names; tmp_hdr_names.parse_delim(WAVELENGTHS_INPUT_AOD_NAME.c_str(), "_"); token_count = tmp_hdr_names.n_elements(); } // 870nm_Input_AOD - else if (string::npos != (offset = hdr_field.find(INPUT_AOD_NAME))) { + else if (string::npos != hdr_field.find(INPUT_AOD_NAME)) { with_unit = true; height_str = hdr_names[0]; } // Exact_Wavelengths_of_AOD(um)_865nm // Exact_Wavelengths_of_PW(um)_935nm - else if (string::npos != (offset = hdr_field.find(WAVELENGTHS_AOD_NAME)) - || string::npos != (offset = hdr_field.find(WAVELENGTHS_PW_NAME)) ) { + else if (string::npos != hdr_field.find(WAVELENGTHS_AOD_NAME) + || string::npos != hdr_field.find(WAVELENGTHS_PW_NAME) ) { with_unit = true; StringArray tmp_hdr_names; tmp_hdr_names.parse_delim(WAVELENGTHS_AOD_NAME.c_str(), "_"); token_count = tmp_hdr_names.n_elements(); } // Exact_Wavelengths_of_INPUT_AOD(um) - else if (string::npos != (offset = hdr_field.find(WAVELENGTHS_INPUT_AOD_NAME)) ) { + else if (string::npos != hdr_field.find(WAVELENGTHS_INPUT_AOD_NAME) ) { StringArray tmp_hdr_names; tmp_hdr_names.parse_delim(WAVELENGTHS_INPUT_AOD_NAME.c_str(), "_"); token_count = tmp_hdr_names.n_elements(); @@ -720,29 +719,28 @@ int AeronetHandler::get_header_count_v3(StringArray hdr_tokens) { string AeronetHandler::make_var_name_from_header(string hdr_field) { string var_name = hdr_field; if (format_version == 3) { - int offset; bool found = true; StringArray hdr_names; // AOD_1640nm-Total,AOD_1640nm-AOD,AOD_1640nm-Rayleigh,AOD_1640nm-O3, // AOD_1640nm-NO2,AOD_1640nm-CO2,AOD_1640nm-CH4,AOD_1640nm-WaterVapor - if (0 == (offset = hdr_field.find(AOD_NAME))) { + if (0 == hdr_field.find(AOD_NAME)) { hdr_names.parse_delim(hdr_field.c_str(), "_"); var_name = hdr_names[0]; } // Exact_Wavelengths_for_Input_AOD(um) - else if (0 == (offset = hdr_field.find(WAVELENGTHS_INPUT_AOD_NAME))) { + else if (0 == hdr_field.find(WAVELENGTHS_INPUT_AOD_NAME)) { var_name = WAVELENGTHS_INPUT_AOD_NAME; } // const string Input_AOD_NAME = "Input_AOD"; // 870nm_Input_AOD - else if ((int) string::npos != (offset = hdr_field.find(INPUT_AOD_NAME))) { + else if ((int) string::npos != hdr_field.find(INPUT_AOD_NAME)) { var_name = INPUT_AOD_NAME; } // Exact_Wavelengths_of_AOD(um)_865nm - else if ((int) string::npos != (offset = hdr_field.find(WAVELENGTHS_AOD_NAME))) { + else if ((int) string::npos != hdr_field.find(WAVELENGTHS_AOD_NAME)) { var_name = WAVELENGTHS_AOD_NAME; } // Exact_Wavelengths_of_PW(um)_935nm - else if ((int) string::npos != (offset = hdr_field.find(WAVELENGTHS_PW_NAME))) { + else if ((int) string::npos != hdr_field.find(WAVELENGTHS_PW_NAME)) { var_name = WAVELENGTHS_PW_NAME; } else if ((hdr_field == OPTICAL_AIR_MASS_NAME)) { diff --git a/met/src/tools/other/lidar2nc/lidar2nc.cc b/met/src/tools/other/lidar2nc/lidar2nc.cc index 807de07a50..c02dfd2771 100644 --- a/met/src/tools/other/lidar2nc/lidar2nc.cc +++ b/met/src/tools/other/lidar2nc/lidar2nc.cc @@ -395,17 +395,6 @@ nc_point_obs.add_header_strings(hdr_typ_string, na_str); // // populate the obs_qty variable // -/* -memset(buf, 0, buf_size); - -for (j=0; j=0; idx--) { if (' ' != var_name[idx] ) break; @@ -681,7 +682,8 @@ void get_variable_info(const char* tbl_filename) { if (0 == m_strlen(var_name)) continue; var_count1++; - m_strncpy(var_desc, (line+BUFR_DESCRIPTION_START), BUFR_DESCRIPTION_LEN, method_name, "var_desc"); + m_strncpy(var_desc, (line+BUFR_DESCRIPTION_START), BUFR_DESCRIPTION_LEN, + method_name, "var_desc", true); var_desc[BUFR_DESCRIPTION_LEN] = '\0'; for (int idx=(BUFR_DESCRIPTION_LEN-1); idx>=0; idx--) { if (' ' != var_desc[idx] && '|' != var_desc[idx]) { @@ -700,7 +702,8 @@ void get_variable_info(const char* tbl_filename) { if (NULL != strstr(line,"MNEMONIC")) break; if (NULL == strstr(line,"EVENT")) continue; - m_strncpy(var_name, (line+BUFR_NAME_START), BUFR_NAME_LEN, method_name, "var_name2"); + m_strncpy(var_name, (line+BUFR_NAME_START), BUFR_NAME_LEN, + method_name, "var_name2", true); var_name[BUFR_NAME_LEN] = '\0'; for (int idx=(BUFR_NAME_LEN-1); idx >=0; idx--) { if (' ' != var_name[idx] ) break; @@ -708,7 +711,8 @@ void get_variable_info(const char* tbl_filename) { } //if (NULL == strstr(var_name,"EVENT")) continue; - m_strncpy(var_desc, (line+BUFR_SEQUENCE_START), BUFR_SEQUENCE_LEN, method_name, "var_desc2"); + m_strncpy(var_desc, (line+BUFR_SEQUENCE_START), BUFR_SEQUENCE_LEN, + method_name, "var_desc2", true); var_desc[BUFR_SEQUENCE_LEN] = '\0'; for (int idx=(BUFR_SEQUENCE_LEN-1); idx>=0; idx--) { if (' ' != var_desc[idx] && '|' != var_desc[idx]) { @@ -728,7 +732,8 @@ void get_variable_info(const char* tbl_filename) { if (' ' == line[BUFR_NAME_START]) continue; if ('-' == line[BUFR_NAME_START]) break; - m_strncpy(var_name, (line+BUFR_NAME_START), BUFR_NAME_LEN, method_name, "var_name3"); + m_strncpy(var_name, (line+BUFR_NAME_START), BUFR_NAME_LEN, + method_name, "var_name3", true); var_name[BUFR_NAME_LEN] = '\0'; for (int idx=(BUFR_NAME_LEN-1); idx >=0; idx--) { if (' ' != var_name[idx] ) break; @@ -737,10 +742,12 @@ void get_variable_info(const char* tbl_filename) { if (NULL != strstr(line,"CCITT IA5")) { ascii_vars.add(var_name); - m_strncpy(var_unit_str, "CCITT IA5", sizeof(var_unit_str), method_name, "var_unit_str1"); + m_strncpy(var_unit_str, "CCITT IA5", sizeof(var_unit_str), + method_name, "var_unit_str1", true); } else { - m_strncpy(var_unit_str, (line+BUFR_UNIT_START), BUFR_UNIT_LEN, method_name, "var_unit_str2"); + m_strncpy(var_unit_str, (line+BUFR_UNIT_START), BUFR_UNIT_LEN, + method_name, "var_unit_str2", true); var_unit_str[BUFR_UNIT_LEN] = '\0'; for (int idx=(BUFR_UNIT_LEN-1); idx>=0; idx--) { if (' ' != var_unit_str[idx] && '|' != var_unit_str[idx]) { diff --git a/met/src/tools/other/wwmca_tool/wwmca_plot.cc b/met/src/tools/other/wwmca_tool/wwmca_plot.cc index 42ec608cb3..b8705dfc84 100644 --- a/met/src/tools/other/wwmca_tool/wwmca_plot.cc +++ b/met/src/tools/other/wwmca_tool/wwmca_plot.cc @@ -400,7 +400,7 @@ void set_pixel_time_filename(const char * cp_name, char * pt_name) // now create the new pixel time filename which consists of the pt_name_start and the // rest of the cp_name after removing its start (WWMCA_TOTAL_CLOUD_PCT) - m_strcpy(pt_name + i, pt_name_start, method_name); + m_strncpy((char *)(pt_name + i), pt_name_start, m_strlen(pt_name_start), method_name); for (j = i + m_strlen(pt_name_start); j < cp_length; j++) pt_name[j] = cp_name[j]; diff --git a/met/src/tools/tc_utils/tc_dland/tc_poly.cc b/met/src/tools/tc_utils/tc_dland/tc_poly.cc index 16560e110b..650e2197b8 100644 --- a/met/src/tools/tc_utils/tc_dland/tc_poly.cc +++ b/met/src/tools/tc_utils/tc_dland/tc_poly.cc @@ -353,7 +353,7 @@ double TCPolyArray::min_dist(double lat, double lon, int &imin) const { bool operator>>(istream & in, TCPoly & p) { int i, n; double x, y; - const char *method_name = "operator>> -> "; + const char *method_name = "TCPolyArray::operator>> -> "; p.clear(); @@ -371,7 +371,7 @@ bool operator>>(istream & in, TCPoly & p) { // NNN is the 3-digit number of points // AAAAAAAA is the 8-character name of the region n = atoi(a[0].c_str()); - m_strncpy(name, line.c_str()+4, 8, method_name); + m_strncpy(name, line.c_str()+4, 8, method_name, "name", true); name[8] = '\0'; name_cs = name; name_cs.ws_strip(); diff --git a/met/src/tools/tc_utils/tc_stat/tc_stat_job.cc b/met/src/tools/tc_utils/tc_stat/tc_stat_job.cc index ed0e22757c..ab99501d9f 100644 --- a/met/src/tools/tc_utils/tc_stat/tc_stat_job.cc +++ b/met/src/tools/tc_utils/tc_stat/tc_stat_job.cc @@ -4034,6 +4034,7 @@ ConcatString build_map_key(const char *prefix, const TCStatLine &l, const StringArray &case_cols) { int i; int lead = 0; + bool not_converted; ConcatString key, cur; // Initialize the map key with prefix @@ -4042,7 +4043,7 @@ ConcatString build_map_key(const char *prefix, const TCStatLine &l, // Build case information for the map key for(i=0; i