From 9afda9c1653c5a77a082941f13929c6b59430425 Mon Sep 17 00:00:00 2001 From: ClemensBuechner Date: Mon, 8 Jan 2024 16:38:40 +0100 Subject: [PATCH] Prevent returning nullpointer if data array is empty. --- src/search/lp/cplex_solver_interface.h | 40 ++++++++++++++++++-------- 1 file changed, 28 insertions(+), 12 deletions(-) diff --git a/src/search/lp/cplex_solver_interface.h b/src/search/lp/cplex_solver_interface.h index db8d7ad64e..fa1b6177cb 100644 --- a/src/search/lp/cplex_solver_interface.h +++ b/src/search/lp/cplex_solver_interface.h @@ -10,6 +10,22 @@ #include namespace lp { + +template +static T* to_cplex_array(std::vector &v) { + /* + CPLEX expects a non-nullptr even for empty arrays but the C++ standard + does not guarantee any particular value for data for empty vectors (see + issue1111). + */ + if (v.empty()) { + static T dummy; + return &dummy; + } else { + return v.data(); + } +} + class CplexSolverInterface : public SolverInterface { CPXENVptr env; CPXLPptr problem; @@ -89,10 +105,10 @@ class CplexSolverInterface : public SolverInterface { void assign_row_by_row( const named_vector::NamedVector &constraints); - double *get_coefficients() {return coefficients.data();} - int *get_indices() {return indices.data();} - int *get_starts() {return starts.data();} - int *get_counts() {return counts.data();} + double *get_coefficients() {return to_cplex_array(coefficients);} + int *get_indices() {return to_cplex_array(indices);} + int *get_starts() {return to_cplex_array(starts);} + int *get_counts() {return to_cplex_array(counts);} int get_num_nonzeros() {return coefficients.size();} }; @@ -107,10 +123,10 @@ class CplexSolverInterface : public SolverInterface { std::vector objective; public: void assign(const named_vector::NamedVector &variables); - double *get_lb() {return lb.data();} - double *get_ub() {return ub.data();} - char *get_type() {return type.data();} - double *get_objective() {return objective.data();} + double *get_lb() {return to_cplex_array(lb);} + double *get_ub() {return to_cplex_array(ub);} + char *get_type() {return to_cplex_array(type);} + double *get_objective() {return to_cplex_array(objective);} }; class CplexRowsInfo { @@ -130,10 +146,10 @@ class CplexSolverInterface : public SolverInterface { std::vector range_indices; public: void assign(const named_vector::NamedVector &constraints, int offset = 0, bool dense_range_values = true); - double *get_rhs() {return rhs.data();} - char *get_sense() {return sense.data();} - double *get_range_values() {return range_values.data();} - int *get_range_indices() {return range_indices.data();} + double *get_rhs() {return to_cplex_array(rhs);} + char *get_sense() {return to_cplex_array(sense);} + double *get_range_values() {return to_cplex_array(range_values);} + int *get_range_indices() {return to_cplex_array(range_indices);} int get_num_ranged_rows() {return range_indices.size();} };