changeset 17609:ec31ca0a5812

fill.m: Improve input validation to avoid endless loops (bug #38927). * scripts/plot/draw/fill.m: check input colorspec argument and apply option/value pair arguments to all patches. Simplify __find_patches__ subfunction to avoid endless loops. Add iscolorspec subfunction to check input arguments.
author Pantxo Diribarne <pantxo.diribarne@gmail.com>
date Thu, 09 May 2013 16:14:47 +0200
parents e7692efcbd0d
children 3f8b3588a9f0
files scripts/plot/draw/fill.m
diffstat 1 files changed, 29 insertions(+), 43 deletions(-) [+]
line wrap: on
line diff
--- a/scripts/plot/draw/fill.m	Tue Oct 08 23:18:17 2013 +0200
+++ b/scripts/plot/draw/fill.m	Thu May 09 16:14:47 2013 +0200
@@ -72,6 +72,15 @@
   hlist = [];
   iargs = __find_patches__ (varargin{:});
 
+  opts = {};
+  if (numel (varargin) > iargs(end) + 2)
+    opts = varargin(iargs(end) + (3:end));
+  endif
+  
+  if (! all (cellfun (@(x) iscolorspec (x), varargin(iargs + 2))))
+    print_usage ();
+  endif
+
   oldfig = [];
   if (! isempty (hax))
     oldfig = get (0, "currentfigure");
@@ -82,16 +91,13 @@
     set (hax, "nextplot", "add");
 
     for i = 1 : length (iargs)
-      if (i == length (iargs))
-        args = varargin(iargs(i):end);
-      else
-        args = varargin(iargs(i):iargs(i+1)-1);
-      endif
+      args = [varargin(iargs(i) + (0:2)) opts];
+
       [htmp, fail] = __patch__ (hax, args{:});
       if (fail)
         print_usage ();
       endif
-      hlist(end + 1, 1) = htmp;
+      hlist(end+1, 1) = htmp;
     endfor
 
     if (strcmp (old_nxtplt, "replace"))
@@ -111,45 +117,25 @@
 endfunction
 
 function iargs = __find_patches__ (varargin)
-  iargs = [];
-  i = 1;
-  while (i < nargin)
-    iargs(end + 1) = i;
-    if (ischar (varargin{i})
-        && (strcmpi (varargin{i}, "faces")
-            || strcmpi (varargin{i}, "vertices")))
-      i += 4;
-    elseif (isnumeric (varargin{i}))
-      i += 2;
-    endif
+  iargs = 1:3:nargin;
+  optidx = find (! cellfun (@isnumeric, varargin(iargs)), 1);
+  iargs(optidx:end) = [];
+endfunction
 
-    if (i <= nargin)
-      while (true);
-        if (ischar (varargin{i})
-            && (strcmpi (varargin{i}, "faces")
-                || strcmpi (varargin{i}, "vertices")))
-          break;
-        elseif (isnumeric (varargin{i}))
-          ## Assume its the colorspec
-          i++;
-          break;
-        elseif (ischar (varargin{i}))
-          colspec = tolower (varargin{i});
-          collen = length (colspec);
-          if (any (strncmp (colspec, 
-                            {"blue", "black", "k", "red", "green", ...
-                             "yellow", "magenta", "cyan", "white"},
-                            collen)))
-            i++;
-            break;
-          endif
-        else
-          i += 2;
-        endif
-      endwhile
+function retval = iscolorspec (arg)
+  retval = false;
+  if (ischar (arg))
+    persistent colors = {"y", "yellow", "r", "red", "m", "magenta", ...
+                         "c", "cyan", "g", "green", "b", "blue", ...
+                         "w", "white", "k", "black"};
+    if (any (strcmpi (arg, colors)))
+      retval = true;
     endif
-  endwhile
-
+  elseif (isvector (arg))
+    if (length (arg) == 3 || all (arg >= 0 && arg <=1))
+      retval = true;
+    endif
+  endif
 endfunction