diff libinterp/corefcn/sparse.cc @ 32059:bade9602c5a1

Validate M and N dimension inputs to sparse(). * sparse.cc (Fsparse): Use xidx_type_value() to extract an octave_idx_type for M and N inputs or throw an error. Remove call to get_dimensions(). Remove FIXME note about unreachable code. Update error messages to be specific about what is wrong with M or N inputs. * mk-sparse-tst.sh: Remove FIXME notes. Update BIST tests for M and N inputs.
author Rik <rik@octave.org>
date Wed, 26 Apr 2023 14:40:37 -0700
parents e242124f1240
children 2e484f9f1f18
line wrap: on
line diff
--- a/libinterp/corefcn/sparse.cc	Wed Apr 26 10:09:09 2023 -0700
+++ b/libinterp/corefcn/sparse.cc	Wed Apr 26 14:40:37 2023 -0700
@@ -199,15 +199,11 @@
     }
   else if (nargin == 2)
     {
-      octave_idx_type m = 0;
-      octave_idx_type n = 0;
-
-      get_dimensions (args(0), args(1), "sparse", m, n);
+      octave_idx_type m = args(0).xidx_type_value ("sparse: M must be a non-negative integer");
+      octave_idx_type n = args(1).xidx_type_value ("sparse: N must be a non-negative integer");
 
-      // FIXME: this code is never active because get_dimensions()
-      //        replaces negative dimensions with 0.
       if (m < 0 || n < 0)
-        error ("sparse: dimensions must be non-negative");
+        error ("sparse: dimensions M and N must be non-negative");
 
       retval = SparseMatrix (m, n);
     }
@@ -237,15 +233,14 @@
 
       if (nargin == 5)
         {
-          get_dimensions (args(3), args(4), "sparse", m, n);
+          m = args(3).xidx_type_value ("sparse: M must be a non-negative integer");
+          n = args(4).xidx_type_value ("sparse: N must be a non-negative integer");
 
-          // FIXME: this code is never active because get_dimensions()
-          //        replaces negative dimensions with 0.
           if (m < 0 || n < 0)
-            error ("sparse: dimensions must be non-negative");
+            error ("sparse: dimensions M and N must be non-negative");
         }
 
-      int argidx = 0;    // index we're checking when index_vector throws
+      int argidx = 0;    // index being checked when index_vector throws
       try
         {
           idx_vector i = args(0).index_vector ();