changeset 28041:5e44268dca6f

Replace input validation relying on str2func with alternatives (bug #57351). * ode15i.m, ode15s.m, ode23.m, ode45.m, check_default_input.m: Replace try/catch blocks around str2func with a call to exist() to check whether function exists before calling str2func.
author Rik <rik@octave.org>
date Mon, 03 Feb 2020 20:22:19 -0800
parents a077eadd1325
children 89f3e53e9723
files scripts/ode/ode15i.m scripts/ode/ode15s.m scripts/ode/ode23.m scripts/ode/ode45.m scripts/ode/private/check_default_input.m
diffstat 5 files changed, 72 insertions(+), 73 deletions(-) [+]
line wrap: on
line diff
--- a/scripts/ode/ode15i.m	Mon Feb 03 13:46:20 2020 -0800
+++ b/scripts/ode/ode15i.m	Mon Feb 03 20:22:19 2020 -0800
@@ -117,44 +117,43 @@
 
   if (! isempty (options.Jacobian))
     if (ischar (options.Jacobian))
-      try
-        options.Jacobian = str2func (options.Jacobian);
-      catch
-        warning (lasterr);
-      end_try_catch
-      if (! is_function_handle (options.Jacobian))
+      if (! exist (options.Jacobian))
         error ("Octave:invalid-input-arg",
-               [solver ": invalid value assigned to field 'Jacobian'"]);
+               [solver ": function '" options.Jacobian "' not found"]);
       endif
+      options.Jacobian = str2func (options.Jacobian);
+    endif
+    if (! is_function_handle (options.Jacobian))
+      error ("Octave:invalid-input-arg",
+             [solver ": invalid value assigned to field 'Jacobian'"]);
     endif
   endif
 
   if (! isempty (options.OutputFcn))
     if (ischar (options.OutputFcn))
-      try
-        options.OutputFcn = str2func (options.OutputFcn);
-      catch
-        warning (lasterr);
-      end_try_catch
-      if (! is_function_handle (options.OutputFcn))
+      if (! exist (options.OutputFcn))
         error ("Octave:invalid-input-arg",
-               [solver ": invalid value assigned to field 'OutputFcn'"]);
+               [solver ": function '" options.OutputFcn "' not found"]);
       endif
+      options.OutputFcn = str2func (options.OutputFcn);
+    endif
+    if (! is_function_handle (options.OutputFcn))
+      error ("Octave:invalid-input-arg",
+             [solver ": invalid value assigned to field 'OutputFcn'"]);
     endif
   endif
 
   if (! isempty (options.Events))
     if (ischar (options.Events))
-      try
-        options.Events = str2func (options.Events);
-      catch
-        warning (lasterr);
-      end_try_catch
-      if (! is_function_handle (options.Events)
-          && ! ismatrix (options.Events))
+      if (! exist (options.Events))
         error ("Octave:invalid-input-arg",
-               [solver ": invalid value assigned to field 'Events'"]);
+               [solver ": function '" options.Events "' not found"]);
       endif
+      options.Events = str2func (options.Events);
+    endif
+    if (! is_function_handle (options.Events))
+      error ("Octave:invalid-input-arg",
+             [solver ": invalid value assigned to field 'Events'"]);
     endif
   endif
 
--- a/scripts/ode/ode15s.m	Mon Feb 03 13:46:20 2020 -0800
+++ b/scripts/ode/ode15s.m	Mon Feb 03 20:22:19 2020 -0800
@@ -111,57 +111,57 @@
 
   if (! isempty (options.Mass))
     if (ischar (options.Mass))
-      try
-        options.Mass = str2func (options.Mass);
-      catch
-        warning (lasterr);
-      end_try_catch
-      if (! is_function_handle (options.Mass))
+      if (! exist (options.Mass))
         error ("Octave:invalid-input-arg",
-               [solver ": invalid value assigned to field 'Mass'"]);
+               [solver ": function '" options.Mass "' not found"]);
       endif
+      options.Mass = str2func (options.Mass);
+    endif
+    if (! is_function_handle (options.Mass))
+      error ("Octave:invalid-input-arg",
+             [solver ": invalid value assigned to field 'Mass'"]);
     endif
   endif
 
   if (! isempty (options.Jacobian))
     if (ischar (options.Jacobian))
-      try
-        options.Jacobian = str2func (options.Jacobian);
-      catch
-        warning (lasterr);
-      end_try_catch
-      if (! is_function_handle (options.Jacobian))
+      if (! exist (options.Jacobian))
         error ("Octave:invalid-input-arg",
-               [solver ": invalid value assigned to field 'Jacobian'"]);
+               [solver ": function '" options.Jacobian "' not found"]);
       endif
+      options.Jacobian = str2func (options.Jacobian);
+    endif
+    if (! is_function_handle (options.Jacobian))
+      error ("Octave:invalid-input-arg",
+             [solver ": invalid value assigned to field 'Jacobian'"]);
     endif
   endif
 
   if (! isempty (options.OutputFcn))
     if (ischar (options.OutputFcn))
-      try
-        options.OutputFcn = str2func (options.OutputFcn);
-      catch
-        warning (lasterr);
-      end_try_catch
-      if (! is_function_handle (options.OutputFcn))
+      if (! exist (options.OutputFcn))
         error ("Octave:invalid-input-arg",
-               [solver ": invalid value assigned to field '%s'"], "OutputFcn");
+               [solver ": function '" options.OutputFcn "' not found"]);
       endif
+      options.OutputFcn = str2func (options.OutputFcn);
+    endif
+    if (! is_function_handle (options.OutputFcn))
+      error ("Octave:invalid-input-arg",
+             [solver ": invalid value assigned to field 'OutputFcn'"]);
     endif
   endif
 
   if (! isempty (options.Events))
     if (ischar (options.Events))
-      try
-        options.Events = str2func (options.Events);
-      catch
-        warning (lasterr);
-      end_try_catch
-      if (! is_function_handle (options.Events))
+      if (! exist (options.Events))
         error ("Octave:invalid-input-arg",
-               [solver ": invalid value assigned to field 'Events'"]);
+               [solver ": function '" options.Events "' not found"]);
       endif
+      options.Events = str2func (options.Events);
+    endif
+    if (! is_function_handle (options.Events))
+      error ("Octave:invalid-input-arg",
+             [solver ": invalid value assigned to field 'Events'"]);
     endif
   endif
 
--- a/scripts/ode/ode23.m	Mon Feb 03 13:46:20 2020 -0800
+++ b/scripts/ode/ode23.m	Mon Feb 03 20:22:19 2020 -0800
@@ -142,15 +142,15 @@
   init = init(:);
 
   if (ischar (fun))
-    try
-      fun = str2func (fun);
-    catch
-      warning (lasterr);
-    end_try_catch
+    if (! exist (fun))
+      error ("Octave:invalid-input-arg",
+             [solver ": function '" fun "' not found"]);
+    endif
+    fun = str2func (fun);
   endif
   if (! is_function_handle (fun))
     error ("Octave:invalid-input-arg",
-           "ode23: FUN must be a valid function handle");
+           [solver ": FUN must be a valid function handle"]);
   endif
 
   ## Start preprocessing, have a look which options are set in odeopts,
--- a/scripts/ode/ode45.m	Mon Feb 03 13:46:20 2020 -0800
+++ b/scripts/ode/ode45.m	Mon Feb 03 20:22:19 2020 -0800
@@ -139,15 +139,15 @@
   init = init(:);
 
   if (ischar (fun))
-    try
-      fun = str2func (fun);
-    catch
-      warning (lasterr);
-    end_try_catch
+    if (! exist (fun))
+      error ("Octave:invalid-input-arg",
+             [solver ": function '" fun "' not found"]);
+    endif
+    fun = str2func (fun);
   endif
   if (! is_function_handle (fun))
     error ("Octave:invalid-input-arg",
-           "ode45: FUN must be a valid function handle");
+           [solver ": FUN must be a valid function handle"]);
   endif
 
   ## Start preprocessing, have a look which options are set in odeopts,
--- a/scripts/ode/private/check_default_input.m	Mon Feb 03 13:46:20 2020 -0800
+++ b/scripts/ode/private/check_default_input.m	Mon Feb 03 20:22:19 2020 -0800
@@ -30,30 +30,30 @@
 
   if (! (nargin (fun) == nargin - 2))
     error ("Octave:invalid-input-arg",
-           [solver ": invalid value assigned to field '%s'"], "fun");
+           [solver ": invalid value assigned to field 'fun'"]);
   endif
 
   if (ischar (fun))
-    try
-      fun = str2func (fun);
-    catch
-      warning (lasterr);
-    end_try_catch
+    if (! exist (fun))
+      error ("Octave:invalid-input-arg",
+             [solver ": function '" fun "' not found"]);
+    endif
+    fun = str2func (fun);
   endif
   if (! is_function_handle (fun))
     error ("Octave:invalid-input-arg",
-               [solver ": invalid value assigned to field '%s'"], "fun");
+           [solver ": invalid value assigned to field '" fun "'"]);
   endif
 
   ## Check trange
   validateattributes (trange, {"float"}, {"vector", "real"}, solver, "trange");
 
   if (numel (trange) < 2)
-       error ("Octave:invalid-input-arg",
-               [solver ": invalid value assigned to field '%s'"], "trange");
+    error ("Octave:invalid-input-arg",
+           [solver ": invalid value assigned to field 'trange'"]);
   elseif (! ((all (diff (trange) > 0)) || all (diff (-trange) > 0)))
-        error ("Octave:invalid-input-arg",
-               [solver ": invalid value assigned to field '%s'"], "trange");
+    error ("Octave:invalid-input-arg",
+           [solver ": invalid value assigned to field 'trange'"]);
   endif
 
   ## Check y0 and yp0