comparison 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
comparison
equal deleted inserted replaced
32058:e242124f1240 32059:bade9602c5a1
197 else 197 else
198 err_wrong_type_arg ("sparse", arg); 198 err_wrong_type_arg ("sparse", arg);
199 } 199 }
200 else if (nargin == 2) 200 else if (nargin == 2)
201 { 201 {
202 octave_idx_type m = 0; 202 octave_idx_type m = args(0).xidx_type_value ("sparse: M must be a non-negative integer");
203 octave_idx_type n = 0; 203 octave_idx_type n = args(1).xidx_type_value ("sparse: N must be a non-negative integer");
204 204
205 get_dimensions (args(0), args(1), "sparse", m, n);
206
207 // FIXME: this code is never active because get_dimensions()
208 // replaces negative dimensions with 0.
209 if (m < 0 || n < 0) 205 if (m < 0 || n < 0)
210 error ("sparse: dimensions must be non-negative"); 206 error ("sparse: dimensions M and N must be non-negative");
211 207
212 retval = SparseMatrix (m, n); 208 retval = SparseMatrix (m, n);
213 } 209 }
214 else if (nargin >= 3) 210 else if (nargin >= 3)
215 { 211 {
235 nargin--; 231 nargin--;
236 } 232 }
237 233
238 if (nargin == 5) 234 if (nargin == 5)
239 { 235 {
240 get_dimensions (args(3), args(4), "sparse", m, n); 236 m = args(3).xidx_type_value ("sparse: M must be a non-negative integer");
241 237 n = args(4).xidx_type_value ("sparse: N must be a non-negative integer");
242 // FIXME: this code is never active because get_dimensions() 238
243 // replaces negative dimensions with 0.
244 if (m < 0 || n < 0) 239 if (m < 0 || n < 0)
245 error ("sparse: dimensions must be non-negative"); 240 error ("sparse: dimensions M and N must be non-negative");
246 } 241 }
247 242
248 int argidx = 0; // index we're checking when index_vector throws 243 int argidx = 0; // index being checked when index_vector throws
249 try 244 try
250 { 245 {
251 idx_vector i = args(0).index_vector (); 246 idx_vector i = args(0).index_vector ();
252 argidx = 1; 247 argidx = 1;
253 idx_vector j = args(1).index_vector (); 248 idx_vector j = args(1).index_vector ();