changeset 26120:80643ff8c8b7 stable

contourc.m: Tighten input validation to avoid segfault in __contourc__ (bug #55071). * contourc.m: Specify in documentation that Z data must be at least 2x2. Add additional check that X, Y, and Z inputs are numeric. Add check that Z data has at least 2 rows and 2 columns. Add input validation tests.
author Rik <rik@octave.org>
date Thu, 22 Nov 2018 07:26:58 -0800
parents ed2339e64a57
children 80d284ab86b6 a4a7e887ddb0
files scripts/plot/draw/contourc.m
diffstat 1 files changed, 20 insertions(+), 5 deletions(-) [+]
line wrap: on
line diff
--- a/scripts/plot/draw/contourc.m	Wed Nov 21 10:35:19 2018 -0800
+++ b/scripts/plot/draw/contourc.m	Thu Nov 22 07:26:58 2018 -0800
@@ -26,7 +26,7 @@
 ## The matrix @var{z} contains height values above the rectangular grid
 ## determined by @var{x} and @var{y}.  If only a single input @var{z} is
 ## provided then @var{x} is taken to be @code{1:columns (@var{z})} and @var{y}
-## is taken to be @code{1:rows (@var{z})}.
+## is taken to be @code{1:rows (@var{z})}.  The minimum data size is 2x2.
 ##
 ## The optional input @var{vn} is either a scalar denoting the number of
 ## contour lines to compute or a vector containing the Z values where lines
@@ -67,8 +67,6 @@
 ## @seealso{contour, contourf, contour3, clabel}
 ## @end deftypefn
 
-## Author: Shai Ayal <shaiay@users.sourceforge.net>
-
 function [c, lev] = contourc (varargin)
 
   if (nargin < 1 || nargin > 4)
@@ -97,8 +95,13 @@
     vn = varargin{4};
   endif
 
-  if (! ismatrix (z) || ! ismatrix (x) || ! ismatrix (y))
-    error ("contourc: X, Y, and Z must be matrices");
+  if (! (isnumeric (z) && isnumeric (x) && isnumeric (y))
+      || ! (ismatrix (z) && ismatrix (x) && ismatrix (y)))
+    error ("contourc: X, Y, and Z must be numeric matrices");
+  endif
+
+  if (rows (z) < 2 || columns (z) < 2)
+    error ("contourc: Z data must have at least 2 rows and 2 columns");
   endif
 
   if (isscalar (vn))
@@ -168,3 +171,15 @@
 %! [c_obs, lev_obs] = contourc (x, y, z, 2:3);
 %! assert (c_obs, c_exp, eps);
 %! assert (lev_obs, lev_exp, eps);
+
+## Test input validation
+%!error contourc ()
+%!error contourc (1,2,3,4,5)
+%!error <X, Y, and Z must be numeric> contourc ({1})
+%!error <X, Y, and Z must be numeric> contourc ({1}, 2, 3)
+%!error <X, Y, and Z must be numeric> contourc (1, {2}, 3)
+%!error <X, Y, and Z must be .* matrices> contourc (ones (3,3,3))
+%!error <X, Y, and Z must be .* matrices> contourc (ones (3,3,3), 2, 3)
+%!error <X, Y, and Z must be .* matrices> contourc (1, ones (3,3,3), 3)
+%!error <Z data must have at least 2 rows> contourc ([1, 2])
+%!error <Z data must have at least .* 2 columns> contourc ([1; 2])