# HG changeset patch # User Arun Giridhar # Date 1712962439 14400 # Node ID c714266d9f0dea194b4c48c1b3551243abf1692f # Parent 4bb532314db1c7c8b697369d7a167aae4e7e7ca6 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 diff -r 4bb532314db1 -r c714266d9f0d libinterp/corefcn/cellfun.cc --- 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; diff -r 4bb532314db1 -r c714266d9f0d libinterp/corefcn/daspk.cc --- 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; diff -r 4bb532314db1 -r c714266d9f0d libinterp/corefcn/dasrt.cc --- 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; diff -r 4bb532314db1 -r c714266d9f0d libinterp/corefcn/dassl.cc --- 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; diff -r 4bb532314db1 -r c714266d9f0d libinterp/corefcn/kron.cc --- 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 kron (const MArray& a, const MArray& 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 kron (const MDiagArray2& a, const MArray& 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 (); diff -r 4bb532314db1 -r c714266d9f0d libinterp/corefcn/pr-output.cc --- 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& 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 (',') << ')'; diff -r 4bb532314db1 -r c714266d9f0d libinterp/corefcn/schur.cc --- 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 (); diff -r 4bb532314db1 -r c714266d9f0d libinterp/corefcn/variables.cc --- 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];