changeset 14462:af552038cc52 stable

Fix segfault in pchip when input array is too small (bug #35835). * __pchip_deriv__.cc (F__pchip_deriv__): Error out if x isn't at least of length 2. * pchip.m: Write two tests for this.
author Jordi Gutiérrez Hermoso <jordigh@octave.org>
date Wed, 14 Mar 2012 13:34:59 -0400
parents 721be41ea988
children 21ac4b576003
files scripts/polynomial/pchip.m src/DLD-FUNCTIONS/__pchip_deriv__.cc
diffstat 2 files changed, 17 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- a/scripts/polynomial/pchip.m
+++ b/scripts/polynomial/pchip.m
@@ -167,3 +167,6 @@
 %!assert(squeeze(yi1(1,2,:)),[1/sqrt(2); 0; -1/sqrt(2);-1],1e-14);
 %!assert(size(yi2),[3,2,5,4]);
 %!assert(squeeze(yi2(1,2,3,:)),[1/sqrt(2); 0; -1/sqrt(2);-1],1e-14);
+
+%!error (pchip (1,2));
+%!error (pchip (1,2,3));
--- a/src/DLD-FUNCTIONS/__pchip_deriv__.cc
+++ b/src/DLD-FUNCTIONS/__pchip_deriv__.cc
@@ -69,6 +69,13 @@
           FloatMatrix ymat (args(1).float_matrix_value ());
 
           octave_idx_type nx = xvec.length ();
+
+          if (nx < 2)
+            {
+              error ("__pchip_deriv__: X must be at least of length 2");
+              return retval;
+            }
+
           octave_idx_type nyr = ymat.rows ();
           octave_idx_type nyc = ymat.columns ();
 
@@ -109,6 +116,13 @@
           Matrix ymat (args(1).matrix_value ());
 
           octave_idx_type nx = xvec.length ();
+
+          if (nx < 2)
+            {
+              error ("__pchip_deriv__: X must be at least of length 2");
+              return retval;
+            }
+
           octave_idx_type nyr = ymat.rows ();
           octave_idx_type nyc = ymat.columns ();