Fix the issue of returning a pointer to a local variable in data_products (#1593)

* Updated to use std::string instead for cases that ruturn const char* by referencing to a local variable.

* Updated c_str invovled check as it is never NULL and always return a null-terminated ('\0') string and also removed unnecessary conversion using c_str.

* Updated a data_products file missed last time to replace c_str check with string check.

* Updated missed parts to replace c_str check with string check.

* Updated to use C++ string comparison for std::string variables.
This commit is contained in:
Hong Chen 2023-10-26 10:39:02 -05:00 committed by GitHub
parent cc6fbc4ff2
commit 80f341f5e7
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
17 changed files with 82 additions and 103 deletions

View File

@ -200,19 +200,11 @@ PlotViewNode::PlotViewNode( Widget Toplevel, Widget Parent_form, DPC_plot* Plot
curves = new XYCurve[n_curves];
// X Axis label.
if ((const_temp_str = plot->getXLabel()) != NULL) {
snprintf( charbuf, sizeof(charbuf), "%s", const_temp_str );
} else {
charbuf[0] = '\0';
}
X_label = XmStringCreateLocalized( charbuf);
// Y Axis label.
if ((const_temp_str = plot->getYLabel()) != NULL) {
snprintf( charbuf, sizeof(charbuf), "%s", const_temp_str );
} else {
charbuf[0] = '\0';
}
Y_label = XmStringCreateLocalized( charbuf);
// ---------------------------

View File

@ -319,15 +319,14 @@ TableViewNode::TableViewNode( Widget Toplevel, DPC_table* Table, int Xpos, int Y
// Print out the column heading labels.
// ----------------------------------------------------------------
for (colix=0; colix < n_columns ; colix++) {
const char *column_label = table->getColumnLabel( colix);
if (!column_label) {
std::string column_label = table->getColumnLabel(colix);
if (column_label.empty()) {
snprintf( charbuf, sizeof(charbuf), "Column_%d", colix);
table_text_buf = twprint( table_text_buf, &table_buf_size, &table_insertion_pos,
column_heading_format[colix], charbuf);
} else {
table_text_buf = twprint( table_text_buf, &table_buf_size, &table_insertion_pos,
column_heading_format[colix], column_label);
column_heading_format[colix], column_label.c_str());
}
}
table_text_buf = twprint( table_text_buf, &table_buf_size, &table_insertion_pos, (char *)"\n");

View File

@ -124,11 +124,11 @@ void GPViewPlotNode::finalize() {
* So, check the return value before making the assignment.
*/
plot_title = (plot->getTitle()) ? plot->getTitle() : tmp_stream.str();
plot_x_label = (plot->getXLabel()) ? plot->getXLabel() : "";
plot_x_label = plot->getXLabel();
plot_x_scale = (plot->getAttribute("x_scale")) ? plot->getAttribute("x_scale") : "";
plot_x_min_rng = (plot->getAttribute("xmin")) ? plot->getAttribute("xmin") : "";
plot_x_max_rng = (plot->getAttribute("xmax")) ? plot->getAttribute("xmax") : "";
plot_y_label = (plot->getYLabel()) ? plot->getYLabel() : "";
plot_y_label = plot->getYLabel();
plot_y_format = (plot->getAttribute("format")) ? plot->getAttribute("format") : "";
plot_y_scale = (plot->getAttribute("y_scale")) ? plot->getAttribute("y_scale") : "";
plot_y_min_rng = (plot->getAttribute("ymin")) ? plot->getAttribute("ymin") : "";

View File

@ -12,7 +12,7 @@ DPC_UnitConvDataStream::DPC_UnitConvDataStream(DataStream* ds, const char *ToUni
ut_unit * to = NULL ;
ut_unit * from = NULL ;
const char * recorded_units = ds->getUnit().c_str();
std::string recorded_units = ds->getUnit();
source_ds = ds;
@ -24,7 +24,7 @@ DPC_UnitConvDataStream::DPC_UnitConvDataStream(DataStream* ds, const char *ToUni
// If the user has specified a units conversion and those units are valid ...
if ( to != NULL ) {
// If the recorded data file doesn't contain the units in which the data is recorded ...
if ((recorded_units == NULL) || (strcmp(recorded_units,"") == 0)) {
if (recorded_units.empty()) {
// If the user didn't give us a hint as to what the units are (using var@from_units) ...
if ((FromUnitsHint == NULL) || (strcmp(FromUnitsHint,"") == 0)) {
// set the from units to the same as the to units.
@ -47,7 +47,7 @@ DPC_UnitConvDataStream::DPC_UnitConvDataStream(DataStream* ds, const char *ToUni
}
} else { // the recorded data file does "know" the units in which the data was recorded,
// so those will be the units that we convert from.
from = ut_parse(u_system, recorded_units, UT_ASCII) ;
from = ut_parse(u_system, recorded_units.c_str(), UT_ASCII) ;
if ( !from ) {
std::cerr << "ERROR: Unable to to perform units conversion because the"
<< " units in the data recording file appear to be corrupt."
@ -72,7 +72,7 @@ DPC_UnitConvDataStream::DPC_UnitConvDataStream(DataStream* ds, const char *ToUni
}
} else { // The user has not specified a units conversion or the units were not valid.
// If the recorded data file doesn't contain the units in which the data is recorded ...
if ((recorded_units == NULL) || (strcmp(recorded_units,"") == 0)) {
if (recorded_units.empty()) {
// If the user didn't give us a hint as to what the units are (using var@from_units) ...
if ((FromUnitsHint == NULL) || (strcmp(FromUnitsHint,"") == 0)) {
cf = cv_get_trivial() ;

View File

@ -39,7 +39,7 @@ public:
/**
* Return the name of the X variable.
*/
const char *getXVarShortName() {
std::string getXVarShortName() {
return( x_var->getShortName() );
};
@ -53,7 +53,7 @@ public:
/**
* Return the name of the Y variable.
*/
const char *getYVarShortName() {
std::string getYVarShortName() {
return( y_var->getShortName() );
};

View File

@ -78,16 +78,16 @@ public:
}
/**
* Return the X-axis label of the plot, which may be NULL.
* Return the X-axis label of the plot, which may be empty.
*/
const char *getXLabel() {
std::string getXLabel() {
return( relation->getXAxisLabel());
}
/**
* Return the Y-axis label of the plot, which may be NULL.
* Return the Y-axis label of the plot, which may be empty.
*/
const char *getYLabel() {
std::string getYLabel() {
return( relation->getYAxisLabel());
}

View File

@ -106,9 +106,9 @@ DPC_std_curve::DPC_std_curve(
Time_constraints );
if (ds[0] != NULL) {
const char* ds_units = ds[0]->getUnit().c_str();
std::string ds_units = ds[0]->getUnit();
y_actual_units = strdup(ds_units);
y_actual_units = strdup(ds_units.c_str());
// Tell our DataStream to start at the beginning.
ds[0]->begin();

View File

@ -194,12 +194,12 @@ int DPC_table::getNumColumns() {
}
// MEMBER FUNCTION
const char *DPC_table::getColumnLabel(unsigned int index) {
std::string DPC_table::getColumnLabel(unsigned int index) {
DPM_column *column = table_spec->getColumn( index );
if ( column->getLabel() != NULL) {
return( column->getLabel());
} else {
return( column->getVar()->getShortName());
return( column->getVar()->getShortName() );
}
}

View File

@ -53,9 +53,9 @@ public:
int getNumColumns();
/**
* Return the Label (which may be NULL) of the of the indicated column.
* Return the Label (which may be empty) of the of the indicated column.
*/
const char *getColumnLabel(unsigned int index);
std::string getColumnLabel(unsigned int index);
/**
* Return the name of the variable for the indicated column.

View File

@ -231,57 +231,54 @@ const char * DPM_curve::getZVarName(unsigned int case_index) {
}
// MEMBER FUNCTION
const char * DPM_curve::getXCommonName() {
std::string DPM_curve::getXCommonName() {
std::string candidate_label;
const char *candidate_label;
const char *short_name;
int n_vars, i;
candidate_label = x_varcase_list[0]->getShortName();
n_vars = (int)x_varcase_list.size();
for (i=1; i<n_vars; i++) {
short_name = x_varcase_list[i]->getShortName();
if (strcmp( candidate_label, short_name) != 0 ) {
return (NULL);
if (candidate_label != x_varcase_list[i]->getShortName()) {
return ("");
}
}
return ( candidate_label);
return (candidate_label);
}
// MEMBER FUNCTION
const char * DPM_curve::getYCommonName() {
std::string DPM_curve::getYCommonName() {
std::string candidate_label;
const char *candidate_label;
const char *short_name;
int n_vars, i;
candidate_label = y_varcase_list[0]->getShortName();
n_vars = (int)y_varcase_list.size();
for (i=1; i<n_vars; i++) {
short_name = y_varcase_list[i]->getShortName();
if (strcmp( candidate_label, short_name) != 0 ) {
return (NULL);
if (candidate_label != y_varcase_list[i]->getShortName()) {
return ("");
}
}
return ( candidate_label);
}
// MEMBER FUNCTION
const char * DPM_curve::getZCommonName() {
std::string DPM_curve::getZCommonName() {
std::string candidate_label;
const char *candidate_label;
const char *short_name;
int n_vars, i;
candidate_label = z_varcase_list[0]->getShortName();
n_vars = (int)z_varcase_list.size();
for (i=1; i<n_vars; i++) {
short_name = z_varcase_list[i]->getShortName();
if (strcmp( candidate_label, short_name) != 0 ) {
return (NULL);
if (candidate_label != z_varcase_list[i]->getShortName()) {
return ("");
}
}
return ( candidate_label);
return (candidate_label);
}
// MEMBER FUNCTION

View File

@ -69,17 +69,17 @@ public:
/**
*
*/
const char * getXCommonName();
std::string getXCommonName();
/**
*
*/
const char * getYCommonName();
std::string getYCommonName();
/**
*
*/
const char * getZCommonName();
std::string getZCommonName();
/**
* Output an xml representation of DPM_curve.

View File

@ -148,29 +148,26 @@ int DPM_relation::NumberOfAxes() {
}
// MEMBER FUNCTION
const char * DPM_relation::getXAxisLabel() {
const char * candidate_label;
const char * short_name;
std::string DPM_relation::getXAxisLabel() {
std::string candidate_label;
int n_curves, i;
if (xaxis) {
candidate_label = xaxis->getLabel();
} else {
candidate_label = NULL;
candidate_label = "";
}
// If an Y-Axis label wasn't supplied, see if there is a common
// variable name that will serve as a label.
if (candidate_label == NULL) {
if (( candidate_label = curve_list[0]->getXCommonName() ) == NULL) {
return (NULL);
if (candidate_label == "") {
if (( candidate_label = curve_list[0]->getXCommonName() ) == "") {
return ("");
}
n_curves = (int)curve_list.size();
for (i=1; i<n_curves; i++) {
if (( short_name = curve_list[i]->getXCommonName() ) == NULL ) {
return (NULL);
}
if (strcmp( candidate_label, short_name) != 0 ) {
return (NULL);
if ( curve_list[i]->getXCommonName().empty() || candidate_label != curve_list[i]->getXCommonName()) {
return ("");
}
}
}
@ -178,29 +175,26 @@ const char * DPM_relation::getXAxisLabel() {
}
// MEMBER FUNCTION
const char * DPM_relation::getYAxisLabel() {
const char * candidate_label;
const char * short_name;
std::string DPM_relation::getYAxisLabel() {
std::string candidate_label;
int n_curves, i;
if (yaxis) {
candidate_label = yaxis->getLabel();
} else {
candidate_label = NULL;
candidate_label = "";
}
// If an Y-Axis label wasn't supplied, see if there is a common
// variable name that will serve as a label.
if (candidate_label == NULL) {
if (( candidate_label = curve_list[0]->getYCommonName() ) == NULL) {
return (NULL);
if (candidate_label == "") {
if (( candidate_label = curve_list[0]->getYCommonName() ) == "") {
return ("");
}
n_curves = (int)curve_list.size();
for (i=1; i<n_curves; i++) {
if (( short_name = curve_list[i]->getYCommonName() ) == NULL ) {
return (NULL);
}
if (strcmp( candidate_label, short_name) != 0 ) {
return (NULL);
if (curve_list[i]->getYCommonName().empty() || candidate_label != curve_list[i]->getYCommonName()) {
return ("");
}
}
}
@ -208,29 +202,26 @@ const char * DPM_relation::getYAxisLabel() {
}
// MEMBER FUNCTION
const char * DPM_relation::getZAxisLabel() {
const char * candidate_label;
const char * short_name;
std::string DPM_relation::getZAxisLabel() {
std::string candidate_label;
int n_curves, i;
if (zaxis) {
candidate_label = zaxis->getLabel();
} else {
candidate_label = NULL;
candidate_label = "";
}
// If an Z-Axis label wasn't supplied, see if there is a common
// variable name that will serve as a label.
if (candidate_label == NULL) {
if (( candidate_label = curve_list[0]->getZCommonName() ) == NULL) {
return (NULL);
if (candidate_label == "") {
if (( candidate_label = curve_list[0]->getZCommonName() ) == "") {
return ("");
}
n_curves = (int)curve_list.size();
for (i=1; i<n_curves; i++) {
if (( short_name = curve_list[i]->getZCommonName() ) == NULL ) {
return (NULL);
}
if (strcmp( candidate_label, short_name) != 0 ) {
return (NULL);
if (curve_list[i]->getZCommonName().empty() || candidate_label != curve_list[i]->getZCommonName()) {
return ("");
}
}
}

View File

@ -53,9 +53,9 @@ public:
/**
*
*/
const char * getXAxisLabel();
const char * getYAxisLabel();
const char * getZAxisLabel();
std::string getXAxisLabel();
std::string getYAxisLabel();
std::string getZAxisLabel();
/**
*

View File

@ -40,7 +40,7 @@ const char* DPM_var::getName() {
return ( (const char*)name );
}
const char* DPM_var::getShortName() {
std::string DPM_var::getShortName() {
char *p;
std::string::size_type idx ;
@ -103,7 +103,7 @@ const char* DPM_var::getShortName() {
} else {
combinedName += " - " + paramName2;
}
return combinedName.c_str();
return combinedName;
}
}

View File

@ -43,7 +43,7 @@ class DPM_var:public DPM_component {
* Return the part of the variable name right of the last
* period or the name if there are no periods.
*/
const char *getShortName();
std::string getShortName();
/**
* Output an xml representation of DPM_var.

View File

@ -93,7 +93,7 @@ DPV_pointer test_view::create_table_view( DPV_pointer parent_data,
cout << "Number of Columns : " << n_columns << endl;
for (colix=0; colix < n_columns ; colix++) {
const char *col_label = table->getColumnLabel( colix);
const char *col_label = table->getColumnLabel( colix).c_str();
const char *var_name = table->getColumnVarName( colix);
const char *col_units = table->getColumnUnits( colix);
const char *format = table->getColumnAttribute( colix,"format");

View File

@ -138,7 +138,7 @@ DPV_pointer Test_view::render_table( DPV_pointer parent_data, DPC_table *table)
s << "Number of Columns: " << n_columns << std::endl;
for (colix=0; colix < n_columns ; colix++) {
if ((temp_cstr = table->getColumnLabel(colix)) != NULL) {
if ((temp_cstr = table->getColumnLabel(colix).c_str())[0] != '\0') {
s << "Column Label [" << colix << "]: " << temp_cstr << std::endl;
}
if ((temp_cstr = table->getColumnVarName(colix)) != NULL) {