Skip to content

Commit

Permalink
Fix the issue of returning a pointer to a local variable in data_prod…
Browse files Browse the repository at this point in the history
…ucts (#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.
  • Loading branch information
hchen99 committed Oct 26, 2023
1 parent cc6fbc4 commit 80f341f
Show file tree
Hide file tree
Showing 17 changed files with 82 additions and 103 deletions.
12 changes: 2 additions & 10 deletions trick_source/data_products/DPX/APPS/FXPLOT/plot_view_node.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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';
}
snprintf( charbuf, sizeof(charbuf), "%s", const_temp_str );
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';
}
snprintf( charbuf, sizeof(charbuf), "%s", const_temp_str );
Y_label = XmStringCreateLocalized( charbuf);

// ---------------------------
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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") : "";
Expand Down
10 changes: 5 additions & 5 deletions trick_source/data_products/DPX/DPC/DPC_UnitConvDataStream.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ 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;

Expand All @@ -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.
Expand All @@ -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."
Expand All @@ -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() ;
Expand Down
4 changes: 2 additions & 2 deletions trick_source/data_products/DPX/DPC/DPC_curve.hh
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public:
/**
* Return the name of the X variable.
*/
const char *getXVarShortName() {
std::string getXVarShortName() {
return( x_var->getShortName() );
};

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

Expand Down
8 changes: 4 additions & 4 deletions trick_source/data_products/DPX/DPC/DPC_plot.hh
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}

Expand Down
4 changes: 2 additions & 2 deletions trick_source/data_products/DPX/DPC/DPC_std_curve.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
4 changes: 2 additions & 2 deletions trick_source/data_products/DPX/DPC/DPC_table.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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() );
}
}

Expand Down
4 changes: 2 additions & 2 deletions trick_source/data_products/DPX/DPC/DPC_table.hh
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
37 changes: 17 additions & 20 deletions trick_source/data_products/DPX/DPM/DPM_curve.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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() {

const char *candidate_label;
const char *short_name;
std::string candidate_label;

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() {

const char *candidate_label;
const char *short_name;
std::string candidate_label;

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() {

const char *candidate_label;
const char *short_name;
std::string candidate_label;

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
Expand Down
6 changes: 3 additions & 3 deletions trick_source/data_products/DPX/DPM/DPM_curve.hh
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
69 changes: 30 additions & 39 deletions trick_source/data_products/DPX/DPM/DPM_relation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -148,90 +148,81 @@ 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 ("");
}
}
}
return (candidate_label);
}

// 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 ("");
}
}
}
return (candidate_label);
}

// 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 ("");
}
}
}
return (candidate_label);
Expand Down
Loading

0 comments on commit 80f341f

Please sign in to comment.