Skip to content

Commit

Permalink
#1355 Make the string null terminated
Browse files Browse the repository at this point in the history
  • Loading branch information
hsoh-u committed Nov 18, 2020
1 parent af7f2b7 commit f5439af
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 30 deletions.
7 changes: 3 additions & 4 deletions met/src/libcode/vx_nc_obs/nc_obs_util.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1282,7 +1282,7 @@ int write_nc_string_array (NcVar *ncVar, StringArray &strArray, const int str_le
// Initialize data_buf
for (int indexX=0; indexX<buf_size; indexX++)
for (int indexY=0; indexY<str_len; indexY++)
data_buf[indexX][indexY] = bad_data_char;
data_buf[indexX][indexY] = NULL;

int buf_index = 0;
int processed_count = 0;
Expand All @@ -1293,10 +1293,9 @@ int write_nc_string_array (NcVar *ncVar, StringArray &strArray, const int str_le
processed_count++;
len = string_data.length();
len2 = strnlen(data_buf[buf_index], str_len);
if (len2 < len) len2 = len;
strncpy(data_buf[buf_index], string_data.c_str(), len);
// Make sure NULL terminated string
if(len < str_len) data_buf[buf_index][len] = bad_data_char;
//Cleanup garbage characters
if (len < str_len) data_buf[buf_index][len] = NULL;
for (int idx=len; idx<len2; idx++)
data_buf[buf_index][idx] = bad_data_char;

Expand Down
14 changes: 9 additions & 5 deletions met/src/tools/other/ioda2nc/ioda2nc.cc
Original file line number Diff line number Diff line change
Expand Up @@ -396,8 +396,8 @@ void process_ioda_file(int i_pb) {
// Initialize
filtered_times.clear();
min_msg_ut = max_msg_ut = (unixtime) 0;
min_time_str[0] = bad_data_char;
max_time_str[0] = bad_data_char;
min_time_str[0] = NULL;
max_time_str[0] = NULL;

// Set the file name for the IODA file
file_name << ioda_files[i_pb];
Expand Down Expand Up @@ -429,8 +429,10 @@ void process_ioda_file(int i_pb) {
mlog << Debug(6) << method_name << "ObsValue var: " << obs_value_vars[idx] << "\n";
}

ConcatString msg_type_name = find_meta_name(metadata_vars, conf_info.metadata_map[conf_key_message_type]);
ConcatString station_id_name = find_meta_name(metadata_vars, conf_info.metadata_map[conf_key_station_id]);
ConcatString msg_type_name = find_meta_name(metadata_vars,
conf_info.metadata_map[conf_key_message_type]);
ConcatString station_id_name = find_meta_name(metadata_vars,
conf_info.metadata_map[conf_key_station_id]);

bool has_msg_type = 0 < msg_type_name.length();
bool has_station_id = 0 < station_id_name.length();
Expand Down Expand Up @@ -656,7 +658,7 @@ void process_ioda_file(int i_pb) {
if(has_msg_type) {
int buf_len = sizeof(modified_hdr_typ);
strncpy(hdr_typ, hdr_msg_types+(i_read*nstring), nstring);
hdr_typ[nstring] = 0;
hdr_typ[nstring] = NULL;
// Null terminate the message type string
cleanup_hdr_buf(hdr_typ, nstring);

Expand All @@ -679,11 +681,13 @@ void process_ioda_file(int i_pb) {
else {
strncpy(modified_hdr_typ, hdr_typ, buf_len);
}
modified_hdr_typ[buf_len-1] = NULL;
}

if(has_station_id) {
char tmp_sid[nstring+1];
strncpy(tmp_sid, hdr_station_ids+(i_read*nstring), nstring);
tmp_sid[nstring] = NULL;
cleanup_hdr_buf(tmp_sid, nstring);
hdr_sid = tmp_sid;
}
Expand Down
47 changes: 26 additions & 21 deletions met/src/tools/other/pb2nc/pb2nc.cc
Original file line number Diff line number Diff line change
Expand Up @@ -554,6 +554,7 @@ void process_command_line(int argc, char **argv) {
cline.add(set_nmsg, "-nmsg", 1);
cline.add(set_dump_path, "-dump", 1);
cline.add(set_collect_metadata, "-index", 0);
cline.add(set_target_variables, "-obs_var", 1);
cline.add(set_target_variables, "-vars", 1);
cline.add(set_logfile, "-log", 1);
cline.add(set_verbosity, "-v", 1);
Expand Down Expand Up @@ -677,21 +678,21 @@ void get_variable_info(const char* tbl_filename) {
if ('0' != line[BUFR_NUMBER_START]) continue;

strncpy(var_name, (line+BUFR_NAME_START), BUFR_NAME_LEN);
var_name[BUFR_NAME_LEN] = bad_data_char;
var_name[BUFR_NAME_LEN] = NULL;
for (int idx=(BUFR_NAME_LEN-1); idx >=0; idx--) {
if (' ' != var_name[idx] ) break;
var_name[idx] = '\0';
var_name[idx] = NULL;
}
if (0 == strlen(var_name)) continue;

var_count1++;
strncpy(var_desc, (line+BUFR_DESCRIPTION_START), BUFR_DESCRIPTION_LEN);
var_desc[BUFR_DESCRIPTION_LEN] = bad_data_char;
var_desc[BUFR_DESCRIPTION_LEN] = NULL;
for (int idx=(BUFR_DESCRIPTION_LEN-1); idx>=0; idx--) {
if (' ' != var_desc[idx] && '|' != var_desc[idx]) {
break;
}
var_desc[idx] = '\0';
var_desc[idx] = NULL;
}
mlog << Debug(10) << method_name << " sec. 1 var: ["
<< var_name << "] \tdesc: [" << var_desc << "]\n";
Expand All @@ -705,20 +706,20 @@ void get_variable_info(const char* tbl_filename) {
if (NULL == strstr(line,"EVENT")) continue;

strncpy(var_name, (line+BUFR_NAME_START), BUFR_NAME_LEN);
var_name[BUFR_NAME_LEN] = bad_data_char;
var_name[BUFR_NAME_LEN] = NULL;
for (int idx=(BUFR_NAME_LEN-1); idx >=0; idx--) {
if (' ' != var_name[idx] ) break;
var_name[idx] = '\0';
var_name[idx] = NULL;
}
//if (NULL == strstr(var_name,"EVENT")) continue;

strncpy(var_desc, (line+BUFR_SEQUENCE_START), BUFR_SEQUENCE_LEN);
var_desc[BUFR_SEQUENCE_LEN] = bad_data_char;
var_desc[BUFR_SEQUENCE_LEN] = NULL;
for (int idx=(BUFR_SEQUENCE_LEN-1); idx>=0; idx--) {
if (' ' != var_desc[idx] && '|' != var_desc[idx]) {
break;
}
var_desc[idx] = '\0';
var_desc[idx] = NULL;
}
mlog << Debug(10) << method_name << " event: ["
<< var_name << "] \tdesc: [" << var_desc << "]\n";
Expand All @@ -733,10 +734,10 @@ void get_variable_info(const char* tbl_filename) {
if ('-' == line[BUFR_NAME_START]) break;

strncpy(var_name, (line+BUFR_NAME_START), BUFR_NAME_LEN);
var_name[BUFR_NAME_LEN] = bad_data_char;
var_name[BUFR_NAME_LEN] = NULL;
for (int idx=(BUFR_NAME_LEN-1); idx >=0; idx--) {
if (' ' != var_name[idx] ) break;
var_name[idx] = '\0';
var_name[idx] = NULL;
}

if (NULL != strstr(line,"CCITT IA5")) {
Expand All @@ -745,12 +746,12 @@ void get_variable_info(const char* tbl_filename) {
}
else {
strncpy(var_unit_str, (line+BUFR_UNIT_START), BUFR_UNIT_LEN);
var_unit_str[BUFR_UNIT_LEN] = bad_data_char;
var_unit_str[BUFR_UNIT_LEN] = NULL;
for (int idx=(BUFR_UNIT_LEN-1); idx>=0; idx--) {
if (' ' != var_unit_str[idx] && '|' != var_unit_str[idx]) {
break;
}
var_unit_str[idx] = '\0';
var_unit_str[idx] = NULL;
}
var_names.add(var_name);
var_units.add(var_unit_str);
Expand Down Expand Up @@ -862,8 +863,8 @@ void process_pbfile(int i_pb) {
// Initialize
filtered_times.clear();
min_msg_ut = max_msg_ut = (unixtime) 0;
min_time_str[0] = bad_data_char;
max_time_str[0] = bad_data_char;
min_time_str[0] = NULL;
max_time_str[0] = NULL;

// Set the file name for the PrepBufr file
file_name << pbfile[i_pb];
Expand Down Expand Up @@ -1048,6 +1049,7 @@ void process_pbfile(int i_pb) {
// Get the next PrepBufr message (header only)
ireadns_(&unit, hdr_typ, &i_date);
readpb_hdr_(&unit, &i_ret, hdr);
hdr_typ[max_str_len-1] = 0;

snprintf(time_str, sizeof(time_str), "%.10i", i_date);
msg_ut = yyyymmddhh_to_unix(time_str);
Expand Down Expand Up @@ -1280,6 +1282,7 @@ void process_pbfile(int i_pb) {
// Get the next PrepBufr message
readpb_(&unit, &i_ret, &nlev, hdr, evns, &nlev_max_req);

int max_buf = sizeof(modified_hdr_typ);
// Special handling for "AIRNOW" and "ANOWPM"
bool is_airnow = (0 == strcmp("AIRNOW", hdr_typ) ||
0 == strcmp("ANOWPM", hdr_typ));
Expand All @@ -1289,16 +1292,14 @@ void process_pbfile(int i_pb) {
mlog << Debug(6) << "\n" << method_name << " -> "
<< "Switching report type \"" << hdr_typ
<< "\" to message type \"" << mappedMessageType << "\".\n";
if (mappedMessageType.length() < HEADER_STR_LEN) {
strncpy(modified_hdr_typ, mappedMessageType.c_str(), sizeof(modified_hdr_typ));
}
else {
strncpy(modified_hdr_typ, mappedMessageType.c_str(), HEADER_STR_LEN);
}
if (mappedMessageType.length() > HEADER_STR_LEN) max_buf = HEADER_STR_LEN;
strncpy(modified_hdr_typ, mappedMessageType.c_str(), max_buf);
}
else {
strncpy(modified_hdr_typ, hdr_typ, sizeof(modified_hdr_typ));
}
if (max_buf >= max_str_len) max_buf--;
modified_hdr_typ[max_buf] = NULL;

// Search through the observation values and store them as:
// HDR_ID GC LVL HGT OB
Expand Down Expand Up @@ -3444,6 +3445,7 @@ void usage() {
<< "\t[-nmsg n]\n"
<< "\t[-index]\n"
<< "\t[-dump path]\n"
<< "\t[-obs_var var]\n"
<< "\t[-log file]\n"
<< "\t[-v level]\n"
<< "\t[-compress level]\n\n"
Expand Down Expand Up @@ -3475,9 +3477,12 @@ void usage() {
<< "\"prepbufr_file\" should also be dumped to text files "
<< "in the directory specified (optional).\n"

<< "\t\t\"-obs_var var1,...\" or multiple \"-obs_var var\" sets the variable list to be saved"
<< " from input BUFR files (optional, default: all).\n"

<< "\t\t\"-index\" indicates that the meta data (available variables and headers)"
<< " is extracted from \"prepbufr_file\" (optional). "
<< "No NetCDF outputs. \"-all\" or \"-vars\" is ignored.\n"
<< "No NetCDF outputs. \"-obs_var\" is ignored.\n"

<< "\t\t\"-log file\" outputs log messages to the specified "
<< "file (optional).\n"
Expand Down

0 comments on commit f5439af

Please sign in to comment.