# HG changeset patch # User John W. Eaton # Date 1633640924 14400 # Node ID a2936935c7c8881338a190a29c4634b8eb1ceaaa # Parent c14a536a41cd9a08d67fc68f3d9752d0d396dd92 attempt to limit possible const_cast damage * Array.h (Array::mex_get_data): Delete. * Sparse.h (Sparse::mex_get_data, Sparse::mex_get_ir, Sparse::mex_get_jc): Delete. * oct-inttypes.h (octave_int::mex_get_data): Delete. * mex.cc (mxArray_octave_value::get_data, mxArray_octave_value::get_ir, mxArray_octave_value::get_jc): Cast away const here, not in octave_value methods. * ov.h, ov.cc (octave_value::mex_get_data, octave_value::mex_get_ir, octave_value::mex_get_jc): Return const void pointer. * ov-base.h (octave_base_value::mex_get_data, octave_base_value::mex_get_ir, octave_base_value::mex_get_jc): Likewise. Change all derived classes. diff -r c14a536a41cd -r a2936935c7c8 libinterp/corefcn/mex.cc --- a/libinterp/corefcn/mex.cc Thu Oct 07 09:16:18 2021 -0700 +++ b/libinterp/corefcn/mex.cc Thu Oct 07 17:08:44 2021 -0400 @@ -631,16 +631,16 @@ if (m_val.issparse ()) { // For sparse arrays, return the first non-zero value. - void *m_data = m_val.mex_get_data (); + const void *m_data = m_val.mex_get_data (); if (m_data == nullptr) return 0.0; if (m_val.islogical ()) - return *static_cast (m_data); + return *static_cast (m_data); else if (m_val.isreal ()) - return *static_cast (m_data); + return *static_cast (m_data); else // Complex type, only return real part - return *static_cast (m_data); + return *static_cast (m_data); } else return m_val.scalar_value (true); @@ -648,7 +648,9 @@ void * get_data (void) const { - void *retval = m_val.mex_get_data (); + // Casting away const required for MEX interface. + + void *retval = const_cast (m_val.mex_get_data ()); if (retval && (m_val.isreal () || m_interleaved)) { @@ -663,7 +665,11 @@ template T * get_data (mxClassID class_id, mxComplexity complexity) const { - T *retval = static_cast (m_val.mex_get_data (class_id, complexity)); + // Casting away const required for MEX interface. + + void *ptr = const_cast (m_val.mex_get_data (class_id, complexity)); + + T *retval = static_cast (ptr); if (retval && (complexity == mxREAL || m_interleaved)) { @@ -763,12 +769,18 @@ mwIndex * get_ir (void) const { - return static_cast (maybe_mark_foreign (m_val.mex_get_ir ())); + // Casting away const required for MEX interface. + + octave_idx_type *ptr = const_cast (m_val.mex_get_ir ()); + return static_cast (maybe_mark_foreign (ptr)); } mwIndex * get_jc (void) const { - return static_cast (maybe_mark_foreign (m_val.mex_get_jc ())); + // Casting away const required for MEX interface. + + octave_idx_type *ptr = const_cast (m_val.mex_get_jc ()); + return static_cast (maybe_mark_foreign (ptr)); } mwSize get_nzmax (void) const { return m_val.nzmax (); } diff -r c14a536a41cd -r a2936935c7c8 libinterp/octave-value/ov-base-mat.h --- a/libinterp/octave-value/ov-base-mat.h Thu Oct 07 09:16:18 2021 -0700 +++ b/libinterp/octave-value/ov-base-mat.h Thu Oct 07 17:08:44 2021 -0400 @@ -198,9 +198,9 @@ OCTINTERP_API bool fast_elem_insert (octave_idx_type n, const octave_value& x); - // Unsafe. This function exists to support the MEX interface. + // This function exists to support the MEX interface. // You should not use it anywhere else. - void * mex_get_data (void) const { return matrix.mex_get_data (); } + const void * mex_get_data (void) const { return matrix.data (); } protected: diff -r c14a536a41cd -r a2936935c7c8 libinterp/octave-value/ov-base-scalar.h --- a/libinterp/octave-value/ov-base-scalar.h Thu Oct 07 09:16:18 2021 -0700 +++ b/libinterp/octave-value/ov-base-scalar.h Thu Oct 07 17:08:44 2021 -0400 @@ -157,9 +157,9 @@ edit_display (const float_display_format& fmt, octave_idx_type i, octave_idx_type j) const; - // Unsafe. This function exists to support the MEX interface. + // This function exists to support the MEX interface. // You should not use it anywhere else. - void * mex_get_data (void) const { return const_cast (&scalar); } + const void * mex_get_data (void) const { return &scalar; } const ST& scalar_ref (void) const { return scalar; } diff -r c14a536a41cd -r a2936935c7c8 libinterp/octave-value/ov-base-sparse.h --- a/libinterp/octave-value/ov-base-sparse.h Thu Oct 07 09:16:18 2021 -0700 +++ b/libinterp/octave-value/ov-base-sparse.h Thu Oct 07 17:08:44 2021 -0400 @@ -232,13 +232,13 @@ edit_display (const float_display_format& fmt, octave_idx_type i, octave_idx_type j) const; - // Unsafe. These functions exists to support the MEX interface. + // These functions exists to support the MEX interface. // You should not use them anywhere else. - void * mex_get_data (void) const { return matrix.mex_get_data (); } + const void * mex_get_data (void) const { return matrix.data (); } - octave_idx_type * mex_get_ir (void) const { return matrix.mex_get_ir (); } + const octave_idx_type * mex_get_ir (void) const { return matrix.ridx (); } - octave_idx_type * mex_get_jc (void) const { return matrix.mex_get_jc (); } + const octave_idx_type * mex_get_jc (void) const { return matrix.cidx (); } OCTINTERP_API octave_value fast_elem_extract (octave_idx_type n) const; diff -r c14a536a41cd -r a2936935c7c8 libinterp/octave-value/ov-base.h --- a/libinterp/octave-value/ov-base.h Thu Oct 07 09:16:18 2021 -0700 +++ b/libinterp/octave-value/ov-base.h Thu Oct 07 17:08:44 2021 -0400 @@ -739,11 +739,11 @@ oct_data_conv::data_type output_type, int skip, octave::mach_info::float_format flt_fmt) const; - virtual void * mex_get_data (void) const { return nullptr; } + virtual const void * mex_get_data (void) const { return nullptr; } - virtual octave_idx_type * mex_get_ir (void) const { return nullptr; } + virtual const octave_idx_type * mex_get_ir (void) const { return nullptr; } - virtual octave_idx_type * mex_get_jc (void) const { return nullptr; } + virtual const octave_idx_type * mex_get_jc (void) const { return nullptr; } virtual mxArray * as_mxArray (bool interleaved) const; diff -r c14a536a41cd -r a2936935c7c8 libinterp/octave-value/ov-cell.cc --- a/libinterp/octave-value/ov-cell.cc Thu Oct 07 09:16:18 2021 -0700 +++ b/libinterp/octave-value/ov-cell.cc Thu Oct 07 17:08:44 2021 -0400 @@ -994,11 +994,11 @@ return true; } -void * +const void * octave_cell::mex_get_data (void) const { clear_cellstr_cache (); - return matrix.mex_get_data (); + return matrix.data (); } bool diff -r c14a536a41cd -r a2936935c7c8 libinterp/octave-value/ov-cell.h --- a/libinterp/octave-value/ov-cell.h Thu Oct 07 09:16:18 2021 -0700 +++ b/libinterp/octave-value/ov-cell.h Thu Oct 07 17:08:44 2021 -0400 @@ -176,9 +176,9 @@ mxArray * as_mxArray (bool interleaved) const; - // Unsafe. This function exists to support the MEX interface. + // This function exists to support the MEX interface. // You should not use it anywhere else. - void * mex_get_data (void) const; + const void * mex_get_data (void) const; private: diff -r c14a536a41cd -r a2936935c7c8 libinterp/octave-value/ov-lazy-idx.h --- a/libinterp/octave-value/ov-lazy-idx.h Thu Oct 07 09:16:18 2021 -0700 +++ b/libinterp/octave-value/ov-lazy-idx.h Thu Oct 07 17:08:44 2021 -0400 @@ -235,9 +235,9 @@ return make_value ().write (os, block_size, output_type, skip, flt_fmt); } - // Unsafe. This function exists to support the MEX interface. + // This function exists to support the MEX interface. // You should not use it anywhere else. - void * mex_get_data (void) const + const void * mex_get_data (void) const { return make_value ().mex_get_data (); } diff -r c14a536a41cd -r a2936935c7c8 libinterp/octave-value/ov.cc --- a/libinterp/octave-value/ov.cc Thu Oct 07 09:16:18 2021 -0700 +++ b/libinterp/octave-value/ov.cc Thu Oct 07 17:08:44 2021 -0400 @@ -2335,7 +2335,7 @@ m_rep->print_info (os, prefix + ' '); } -void * +const void * octave_value::mex_get_data (mxClassID class_id, mxComplexity complexity) const { // If class_id is set to mxUNKNOWN_CLASS, return data for any type. diff -r c14a536a41cd -r a2936935c7c8 libinterp/octave-value/ov.h --- a/libinterp/octave-value/ov.h Thu Oct 07 09:16:18 2021 -0700 +++ b/libinterp/octave-value/ov.h Thu Oct 07 17:08:44 2021 -0400 @@ -1478,15 +1478,23 @@ octave_base_value * internal_rep (void) const { return m_rep; } - // Unsafe. These functions exist to support the MEX interface. + // These functions exist to support the MEX interface. // You should not use them anywhere else. - OCTINTERP_API void * + + OCTINTERP_API const void * mex_get_data (mxClassID class_id = mxUNKNOWN_CLASS, mxComplexity complexity = mxREAL) const; - octave_idx_type * mex_get_ir (void) const { return m_rep->mex_get_ir (); } + const octave_idx_type * mex_get_ir (void) const + { + return m_rep->mex_get_ir (); + } - octave_idx_type * mex_get_jc (void) const { return m_rep->mex_get_jc (); } + const octave_idx_type * + mex_get_jc (void) const + { + return m_rep->mex_get_jc (); + } mxArray * as_mxArray (bool interleaved = false) const { return m_rep->as_mxArray (interleaved); } diff -r c14a536a41cd -r a2936935c7c8 liboctave/array/Array.h --- a/liboctave/array/Array.h Thu Oct 07 09:16:18 2021 -0700 +++ b/liboctave/array/Array.h Thu Oct 07 17:08:44 2021 -0400 @@ -713,11 +713,6 @@ OCTARRAY_API void print_info (std::ostream& os, const std::string& prefix) const; - //! Give a pointer to the data in mex format. Unsafe. This function - //! exists to support the MEX interface. You should not use it - //! anywhere else. - void * mex_get_data (void) const { return const_cast (data ()); } - OCTARRAY_API Array sort (int dim = 0, sortmode mode = ASCENDING) const; OCTARRAY_API Array sort (Array &sidx, int dim = 0, sortmode mode = ASCENDING) const; diff -r c14a536a41cd -r a2936935c7c8 liboctave/array/Sparse.h --- a/liboctave/array/Sparse.h Thu Oct 07 09:16:18 2021 -0700 +++ b/liboctave/array/Sparse.h Thu Oct 07 17:08:44 2021 -0400 @@ -578,20 +578,6 @@ OCTAVE_API void print_info (std::ostream& os, const std::string& prefix) const; - // Unsafe. These functions exist to support the MEX interface. - // You should not use them anywhere else. - void * mex_get_data (void) const { return const_cast (data ()); } - - octave_idx_type * mex_get_ir (void) const - { - return const_cast (ridx ()); - } - - octave_idx_type * mex_get_jc (void) const - { - return const_cast (cidx ()); - } - OCTAVE_API Sparse sort (octave_idx_type dim = 0, sortmode mode = ASCENDING) const; OCTAVE_API Sparse diff -r c14a536a41cd -r a2936935c7c8 liboctave/util/oct-inttypes.h --- a/liboctave/util/oct-inttypes.h Thu Oct 07 09:16:18 2021 -0700 +++ b/liboctave/util/oct-inttypes.h Thu Oct 07 17:08:44 2021 -0400 @@ -912,10 +912,6 @@ // The following are provided for convenience. static const octave_int s_zero, s_one; - // Unsafe. This function exists to support the MEX interface. - // You should not use it anywhere else. - void * mex_get_data (void) const { return const_cast (&m_ival); } - private: T m_ival;