From 426e56c65935995029a77538ea71ff3a68407a10 Mon Sep 17 00:00:00 2001
From: dirk <dirk@git.imagemagick.org>
Date: Sun, 29 May 2016 08:15:48 +0200
Subject: [PATCH] Added check for invalid number of frames in mat file

Fix crash for corrupted files
(cherry picked from commit 8a370f9ab120faf182aa160900ba692ba8e2bcf0)

Origin: upstream, https://github.com/ImageMagick/ImageMagick/commit/8a370f9ab120faf182aa160900ba692ba8e2bcf0
bug-debian: https;//bugs.debian.org/845244
---
 coders/mat.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)

diff --git a/coders/mat.c b/coders/mat.c
index e4e9cb198..de3bf92cf 100644
--- a/coders/mat.c
+++ b/coders/mat.c
@@ -742,7 +742,9 @@ MATLAB_KO: ThrowReaderException(CorruptImageError,"ImproperImageHeader");
       case 16: z2=z = ReadBlobXXXLong(image2);  /* 4D matrix animation */
          if(z!=3 && z!=1)
            ThrowReaderException(CoderError, "MultidimensionalMatricesAreNotSupported");
-          Frames = ReadBlobXXXLong(image2);
+         Frames = ReadBlobXXXLong(image2);
+         if (Frames == 0)
+           ThrowReaderException(CorruptImageError,"ImproperImageHeader");
          break;
       default: ThrowReaderException(CoderError, "MultidimensionalMatricesAreNotSupported");
     }
