changeset 17916:c7f089c560cc

imread: avoid out of bounds error when reading part of multipage image file. * __magick_read__.cc (F__magick_read__): the nFrames variable holds the total number of frames in the file which may be different than the length of frameidx (the indices for the frames to read) so use frameidx.nelem () in a for loop instead of nFrames. Also, return after printing error (and no setting error_state)
author Carnë Draug <carandraug@octave.org>
date Wed, 13 Nov 2013 06:02:48 +0000
parents 56d2b6838405
children c9f622fd7307
files libinterp/dldfcn/__magick_read__.cc
diffstat 1 files changed, 15 insertions(+), 13 deletions(-) [+]
line wrap: on
line diff
--- a/libinterp/dldfcn/__magick_read__.cc	Tue Nov 12 22:37:50 2013 -0800
+++ b/libinterp/dldfcn/__magick_read__.cc	Wed Nov 13 06:02:48 2013 +0000
@@ -770,19 +770,21 @@
   // time we decode the image because that's done in many different places,
   // to cover the different types of images which would lead to a lot of
   // copy and paste.
-  const unsigned int nRows = imvec[frameidx(0)].rows ();
-  const unsigned int nCols = imvec[frameidx(0)].columns ();
-  for (octave_idx_type frame = 0; frame < nFrames; frame++)
-    {
-      if (nRows != imvec[frameidx(frame)].rows () ||
-          nCols != imvec[frameidx(frame)].columns ())
-        {
-          error ("imread: all frames must have the same size but frame %i is different",
-                 frameidx(frame) +1);
-          error_state = 1;
-        }
-    }
-
+  {
+    const unsigned int nRows = imvec[frameidx(0)].rows ();
+    const unsigned int nCols = imvec[frameidx(0)].columns ();
+    const octave_idx_type n = frameidx.nelem ();
+    for (octave_idx_type frame = 0; frame < n; frame++)
+      {
+        if (nRows != imvec[frameidx(frame)].rows () ||
+            nCols != imvec[frameidx(frame)].columns ())
+          {
+            error ("imread: all frames must have the same size but frame %i is different",
+                   frameidx(frame) +1);
+            return output;
+          }
+      }
+  }
 
   const octave_idx_type depth = get_depth (imvec[frameidx(0)]);
   if (is_indexed (imvec[frameidx(0)]))