Mercurial > octave
comparison libinterp/corefcn/sparse.cc @ 33365:fb44a9db1bc6
Replace `xint_value` and related functions with stricter versions (bug #65538)
The conversion function `xint_value`, `xbool_value`, and `xidx_type_value`
were being used to convert input arguments to integers, booleans, or index
values respectively, and to provide a meaningful error message if the
conversion failed. The problem, as described in bug #65538, is that this
allows user code like `all (true (3), 2.718)` to be silently converted to
`all (true (3), 2)` instead of giving an error that the dimension needs to
be an integer. The same happens with `cell (e, pi)`, which silently creates
a 2x3 cell array. In both cases, this permissive conversion is a Matlab
incompatibility.
This patch provides an analogue of `xint_value` called `yint_value` that uses a
stricter conversion to integer, and also does the same for `bool` and
`idx_type` conversions. With this patch, passing non-integer sizes now gives
an error, and passing non-boolean values to boolean arguments now give a warning.
* libinterp/octave-value/ov.h: Define new functions `strict_int_value` and
`strict_bool_value` and declare new function `strict_idx_type_value`.
Declare corresponding new wrapper functions `yint_value`, `ybool_value`,
and `yidx_type_value`.
* libinterp/octave-value/ov.cc: Define `strict_idx_type_value`.
Define `yint_value`, `ybool_value`, and `yidx_type_value`
using the existing XVALUE_EXTRACTOR macro.
* Replace the corresponding function calls in: __eigs__.cc, besselj.cc,
bitfcns.cc, data.cc, file-io.cc, interpreter.cc, jsondecode.cc, load-path.cc,
oct-stream.cc, ov-cell.cc, psi.cc, pt-eval.cc, sparse.cc, strfns.cc,
syscalls.cc, time.cc, utils.cc, variables.cc, __glpk__.cc, __ode15__.cc,
pt-eval.cc
* NEWS.10.md: Add note about the Matlab compatibility of this patch.
Incidentally, the new conversion functions allow a FIXME note inside `fcntl`
to be addressed and removed.
author | Arun Giridhar <arungiridhar@gmail.com> |
---|---|
date | Mon, 08 Apr 2024 21:43:05 -0400 |
parents | 2e484f9f1f18 |
children | 5fded8395daa |
comparison
equal
deleted
inserted
replaced
33363:e95dabfeadb5 | 33365:fb44a9db1bc6 |
---|---|
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 = args(0).xidx_type_value ("sparse: M must be a non-negative integer"); | 202 octave_idx_type m = args(0).yidx_type_value ("sparse: M must be a non-negative integer"); |
203 octave_idx_type n = args(1).xidx_type_value ("sparse: N must be a non-negative integer"); | 203 octave_idx_type n = args(1).yidx_type_value ("sparse: N must be a non-negative integer"); |
204 | 204 |
205 if (m < 0 || n < 0) | 205 if (m < 0 || n < 0) |
206 error ("sparse: dimensions M and N must be non-negative"); | 206 error ("sparse: dimensions M and N must be non-negative"); |
207 | 207 |
208 retval = SparseMatrix (m, n); | 208 retval = SparseMatrix (m, n); |
231 nargin--; | 231 nargin--; |
232 } | 232 } |
233 | 233 |
234 if (nargin == 5) | 234 if (nargin == 5) |
235 { | 235 { |
236 m = args(3).xidx_type_value ("sparse: M must be a non-negative integer"); | 236 m = args(3).yidx_type_value ("sparse: M must be a non-negative integer"); |
237 n = args(4).xidx_type_value ("sparse: N must be a non-negative integer"); | 237 n = args(4).yidx_type_value ("sparse: N must be a non-negative integer"); |
238 | 238 |
239 if (m < 0 || n < 0) | 239 if (m < 0 || n < 0) |
240 error ("sparse: dimensions M and N must be non-negative"); | 240 error ("sparse: dimensions M and N must be non-negative"); |
241 } | 241 } |
242 | 242 |