changeset 30232:a2936935c7c8

attempt to limit possible const_cast damage * Array.h (Array<T>::mex_get_data): Delete. * Sparse.h (Sparse<T>::mex_get_data, Sparse<T>::mex_get_ir, Sparse<T>::mex_get_jc): Delete. * oct-inttypes.h (octave_int<T>::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.
author John W. Eaton <jwe@octave.org>
date Thu, 07 Oct 2021 17:08:44 -0400
parents c14a536a41cd
children 76393baa188d
files libinterp/corefcn/mex.cc libinterp/octave-value/ov-base-mat.h libinterp/octave-value/ov-base-scalar.h libinterp/octave-value/ov-base-sparse.h libinterp/octave-value/ov-base.h libinterp/octave-value/ov-cell.cc libinterp/octave-value/ov-cell.h libinterp/octave-value/ov-lazy-idx.h libinterp/octave-value/ov.cc libinterp/octave-value/ov.h liboctave/array/Array.h liboctave/array/Sparse.h liboctave/util/oct-inttypes.h
diffstat 13 files changed, 50 insertions(+), 53 deletions(-) [+]
line wrap: on
line diff
--- 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<bool *> (m_data);
+          return *static_cast<const bool *> (m_data);
         else if (m_val.isreal ())
-          return *static_cast<double *> (m_data);
+          return *static_cast<const double *> (m_data);
         else  // Complex type, only return real part
-          return *static_cast<double *> (m_data);
+          return *static_cast<const double *> (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<void *> (m_val.mex_get_data ());
 
     if (retval && (m_val.isreal () || m_interleaved))
       {
@@ -663,7 +665,11 @@
   template <typename T>
   T * get_data (mxClassID class_id, mxComplexity complexity) const
   {
-    T *retval = static_cast<T *> (m_val.mex_get_data (class_id, complexity));
+    // Casting away const required for MEX interface.
+
+    void *ptr = const_cast<void *> (m_val.mex_get_data (class_id, complexity));
+
+    T *retval = static_cast<T *> (ptr);
 
     if (retval && (complexity == mxREAL || m_interleaved))
       {
@@ -763,12 +769,18 @@
 
   mwIndex * get_ir (void) const
   {
-    return static_cast<mwIndex *> (maybe_mark_foreign (m_val.mex_get_ir ()));
+    // Casting away const required for MEX interface.
+
+    octave_idx_type *ptr = const_cast<octave_idx_type *> (m_val.mex_get_ir ());
+    return static_cast<mwIndex *> (maybe_mark_foreign (ptr));
   }
 
   mwIndex * get_jc (void) const
   {
-    return static_cast<mwIndex *> (maybe_mark_foreign (m_val.mex_get_jc ()));
+    // Casting away const required for MEX interface.
+
+    octave_idx_type *ptr = const_cast<octave_idx_type *> (m_val.mex_get_jc ());
+    return static_cast<mwIndex *> (maybe_mark_foreign (ptr));
   }
 
   mwSize get_nzmax (void) const { return m_val.nzmax (); }
--- 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:
 
--- 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<ST *> (&scalar); }
+  const void * mex_get_data (void) const { return &scalar; }
 
   const ST& scalar_ref (void) const { return scalar; }
 
--- 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;
 
--- 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;
 
--- 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
--- 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:
 
--- 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 ();
   }
--- 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.
--- 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); }
--- 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<T *> (data ()); }
-
   OCTARRAY_API Array<T> sort (int dim = 0, sortmode mode = ASCENDING) const;
   OCTARRAY_API Array<T> sort (Array<octave_idx_type> &sidx, int dim = 0,
                  sortmode mode = ASCENDING) const;
--- 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<T *> (data ()); }
-
-  octave_idx_type * mex_get_ir (void) const
-  {
-    return const_cast<octave_idx_type *> (ridx ());
-  }
-
-  octave_idx_type * mex_get_jc (void) const
-  {
-    return const_cast<octave_idx_type *> (cidx ());
-  }
-
   OCTAVE_API Sparse<T>
   sort (octave_idx_type dim = 0, sortmode mode = ASCENDING) const;
   OCTAVE_API Sparse<T>
--- 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<T *> (&m_ival); }
-
 private:
 
   T m_ival;