Mercurial > octave
changeset 33389:c714266d9f0d
Replace some `panic_unless` with `error` for input validation
Some instances of `panic_unless` were being used to validate function inputs.
This patch changes many of those instances to `error ()` with a more detailed
error message than before.
Files affected: cellfun.cc, daspk.cc, dasrt.cc, dassl.cc, kron.cc,
pr-output.cc, schur.cc, variables.cc
author | Arun Giridhar <arungiridhar@gmail.com> |
---|---|
date | Fri, 12 Apr 2024 18:53:59 -0400 |
parents | 4bb532314db1 |
children | 3607ae0a5d88 35901ae8a563 |
files | libinterp/corefcn/cellfun.cc libinterp/corefcn/daspk.cc libinterp/corefcn/dasrt.cc libinterp/corefcn/dassl.cc libinterp/corefcn/kron.cc libinterp/corefcn/pr-output.cc libinterp/corefcn/schur.cc libinterp/corefcn/variables.cc |
diffstat | 8 files changed, 35 insertions(+), 19 deletions(-) [+] |
line wrap: on
line diff
--- a/libinterp/corefcn/cellfun.cc Fri Apr 12 17:47:16 2024 -0400 +++ b/libinterp/corefcn/cellfun.cc Fri Apr 12 18:53:59 2024 -0400 @@ -1991,8 +1991,8 @@ { Cell retval; - panic_unless (nd == 1 || nd == 2); - panic_unless (a.ndims () == 2); + if ((nd != 1 && nd != 2) || a.ndims () != 2) + error ("do_mat2cell_2d: A must be two-dimensional, and ND must be 1 or 2"); if (mat2cell_mismatch (a.dims (), d, nd)) return retval; @@ -2049,7 +2049,8 @@ { Cell retval; - panic_unless (nd >= 1); + if (nd < 1) + error ("do_mat2cell_nd: ND must be at least 1"); if (mat2cell_mismatch (a.dims (), d, nd)) return retval; @@ -2132,7 +2133,8 @@ { Cell retval; - panic_unless (nd >= 1); + if (nd < 1) + error ("do_mat2cell: ND must be at least 1"); if (mat2cell_mismatch (a.dims (), d, nd)) return retval;
--- a/libinterp/corefcn/daspk.cc Fri Apr 12 17:47:16 2024 -0400 +++ b/libinterp/corefcn/daspk.cc Fri Apr 12 18:53:59 2024 -0400 @@ -68,7 +68,8 @@ { ColumnVector retval; - panic_unless (x.numel () == xdot.numel ()); + if (x.numel () != xdot.numel ()) + error ("daspk_user_function: X and XDOT must have the same number of elements"); octave_value_list args; @@ -119,7 +120,8 @@ { Matrix retval; - panic_unless (x.numel () == xdot.numel ()); + if (x.numel () != xdot.numel ()) + error ("daspk_user_jacobian: X and XDOT must have the same number of elements"); octave_value_list args;
--- a/libinterp/corefcn/dasrt.cc Fri Apr 12 17:47:16 2024 -0400 +++ b/libinterp/corefcn/dasrt.cc Fri Apr 12 18:53:59 2024 -0400 @@ -69,7 +69,8 @@ { ColumnVector retval; - panic_unless (x.numel () == xdot.numel ()); + if (x.numel () != xdot.numel ()) + error ("dasrt_user_f: X and XDOT must have the same number of elements"); octave_value_list args; @@ -159,7 +160,8 @@ { Matrix retval; - panic_unless (x.numel () == xdot.numel ()); + if (x.numel () != xdot.numel ()) + error ("dasrt_user_j: X and XDOT must have the same number of elements"); octave_value_list args;
--- a/libinterp/corefcn/dassl.cc Fri Apr 12 17:47:16 2024 -0400 +++ b/libinterp/corefcn/dassl.cc Fri Apr 12 18:53:59 2024 -0400 @@ -67,7 +67,8 @@ { ColumnVector retval; - panic_unless (x.numel () == xdot.numel ()); + if (x.numel () != xdot.numel ()) + error ("dassl_user_function: X and XDOT must have the same number of elements"); octave_value_list args; @@ -118,7 +119,8 @@ { Matrix retval; - panic_unless (x.numel () == xdot.numel ()); + if (x.numel () != xdot.numel ()) + error ("dassl_user_jacobian: X and XDOT must have the same number of elements"); octave_value_list args;
--- a/libinterp/corefcn/kron.cc Fri Apr 12 17:47:16 2024 -0400 +++ b/libinterp/corefcn/kron.cc Fri Apr 12 18:53:59 2024 -0400 @@ -55,8 +55,8 @@ static MArray<T> kron (const MArray<R>& a, const MArray<T>& b) { - panic_unless (a.ndims () == 2); - panic_unless (b.ndims () == 2); + if (a.ndims () != 2 || b.ndims () != 2) + error ("kron: A and B must both be two-dimensional"); octave_idx_type nra = a.rows (); octave_idx_type nrb = b.rows (); @@ -86,7 +86,8 @@ static MArray<T> kron (const MDiagArray2<R>& a, const MArray<T>& b) { - panic_unless (b.ndims () == 2); + if (b.ndims () != 2) + error ("kron: B must be two-dimensional"); octave_idx_type nra = a.rows (); octave_idx_type nrb = b.rows ();
--- a/libinterp/corefcn/pr-output.cc Fri Apr 12 17:47:16 2024 -0400 +++ b/libinterp/corefcn/pr-output.cc Fri Apr 12 18:53:59 2024 -0400 @@ -300,7 +300,8 @@ pr_max_internal (const MArray<T>& m) { // We expect a 2-d array. - panic_unless (m.ndims () == 2); + if (m.ndims () != 2) + error ("pr_max_internal: M must be two-dimensional"); octave_idx_type nr = m.rows (); octave_idx_type nc = m.columns (); @@ -681,7 +682,8 @@ make_matrix_format (const MT& m) { // We expect a 2-d array. - panic_unless (m.ndims () == 2); + if (m.ndims () != 2) + error ("make_matrix_format: M must be two-dimensional"); if (free_format) return float_display_format (); @@ -1570,7 +1572,8 @@ print_empty_matrix (std::ostream& os, octave_idx_type nr, octave_idx_type nc, bool pr_as_read_syntax) { - panic_unless (nr == 0 || nc == 0); + if (nr && nc) + error ("print_empty_matrix: at least one of NR and NC must be zero"); if (pr_as_read_syntax) { @@ -1592,7 +1595,8 @@ print_empty_nd_array (std::ostream& os, const dim_vector& dims, bool pr_as_read_syntax) { - panic_unless (dims.any_zero ()); + if (! dims.any_zero ()) + error ("print_empty_nd_array: at least one of DIMS must be zero"); if (pr_as_read_syntax) os << "zeros (" << dims.str (',') << ')';
--- a/libinterp/corefcn/schur.cc Fri Apr 12 17:47:16 2024 -0400 +++ b/libinterp/corefcn/schur.cc Fri Apr 12 18:53:59 2024 -0400 @@ -46,7 +46,9 @@ octave_value retval = a; octave_idx_type n = a.rows (); - panic_unless (a.columns () == n); + + if (a.columns () != n) + error ("mark_upper_triangular: A must be a square matrix"); const typename Matrix::element_type zero = typename Matrix::element_type ();
--- a/libinterp/corefcn/variables.cc Fri Apr 12 17:47:16 2024 -0400 +++ b/libinterp/corefcn/variables.cc Fri Apr 12 18:53:59 2024 -0400 @@ -768,7 +768,8 @@ int nargin = args.length (); - panic_unless (var < nchoices); + if (var >= nchoices) + error ("set_internal_variable: VAR too large for CHOICES"); if (nargout > 0 || nargin == 0) retval = choices[var];