comparison libinterp/octave-value/ov.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 56d234504c01
children fc12e1f3cd5d 5fded8395daa
comparison
equal deleted inserted replaced
33363:e95dabfeadb5 33365:fb44a9db1bc6
1702 #else 1702 #else
1703 return int_value (req_int, frc_str_conv); 1703 return int_value (req_int, frc_str_conv);
1704 #endif 1704 #endif
1705 } 1705 }
1706 1706
1707 octave_idx_type
1708 octave_value::strict_idx_type_value (bool frc_str_conv) const
1709 {
1710 #if defined (OCTAVE_ENABLE_64)
1711 return int64_value (true, frc_str_conv);
1712 #else
1713 return int_value (true, frc_str_conv);
1714 #endif
1715 }
1716
1707 Cell 1717 Cell
1708 octave_value::cell_value () const 1718 octave_value::cell_value () const
1709 { 1719 {
1710 return m_rep->cell_value (); 1720 return m_rep->cell_value ();
1711 } 1721 }
2180 XVALUE_EXTRACTOR (octave_user_code *, xuser_code_value, user_code_value) 2190 XVALUE_EXTRACTOR (octave_user_code *, xuser_code_value, user_code_value)
2181 XVALUE_EXTRACTOR (octave_fcn_handle *, xfcn_handle_value, fcn_handle_value) 2191 XVALUE_EXTRACTOR (octave_fcn_handle *, xfcn_handle_value, fcn_handle_value)
2182 2192
2183 XVALUE_EXTRACTOR (octave_value_list, xlist_value, list_value) 2193 XVALUE_EXTRACTOR (octave_value_list, xlist_value, list_value)
2184 2194
2195 // Make some stricter versions of XVALUE_EXTRACTOR,
2196 // especially for parsing integer arguments that cannot be floating point.
2197 // See bug #65538.
2198
2199 XVALUE_EXTRACTOR (int, yint_value, strict_int_value)
2200
2201 XVALUE_EXTRACTOR (bool, ybool_value, strict_bool_value)
2202
2203 XVALUE_EXTRACTOR (octave_idx_type, yidx_type_value, strict_idx_type_value)
2204
2185 #undef XVALUE_EXTRACTOR 2205 #undef XVALUE_EXTRACTOR
2206
2186 2207
2187 octave_value 2208 octave_value
2188 octave_value::storable_value () const 2209 octave_value::storable_value () const
2189 { 2210 {
2190 octave_value retval = *this; 2211 octave_value retval = *this;