diff options
-rw-r--r-- | coders/mat.c | 8 |
1 files changed, 7 insertions, 1 deletions
diff --git a/coders/mat.c b/coders/mat.c index 515fc5829..be7abaf0c 100644 --- a/coders/mat.c +++ b/coders/mat.c @@ -628,6 +628,7 @@ static Image *ReadMATImageV4(const ImageInfo *image_info,Image *image, (void) SeekBlob(image,0,SEEK_SET); ldblk=ReadBlobLSBLong(image); + if(EOFBlob(image)) return((Image *) NULL); if ((ldblk > 9999) || (ldblk < 0)) return((Image *) NULL); HDR.Type[3]=ldblk % 10; ldblk /= 10; /* T digit */ @@ -887,16 +888,20 @@ static Image *ReadMATImage(const ImageInfo *image_info,ExceptionInfo *exception) MATLAB_KO: ThrowReaderException(CorruptImageError,"ImproperImageHeader"); filepos = TellBlob(image); - while(!EOFBlob(image)) /* object parser loop */ + while(filepos < GetBlobSize(image) && !EOFBlob(image)) /* object parser loop */ { Frames = 1; (void) SeekBlob(image,filepos,SEEK_SET); + if(filepos > GetBlobSize(image) || filepos < 0) + break; /* printf("pos=%X\n",TellBlob(image)); */ MATLAB_HDR.DataType = ReadBlobXXXLong(image); if(EOFBlob(image)) break; MATLAB_HDR.ObjectSize = ReadBlobXXXLong(image); if(EOFBlob(image)) break; + if((MagickSizeType) (MATLAB_HDR.ObjectSize+filepos) >= GetBlobSize(image)) + goto MATLAB_KO; filepos += MATLAB_HDR.ObjectSize + 4 + 4; image2 = image; @@ -1105,6 +1110,7 @@ RestoreMSCWarning { if (logging) (void)LogMagickEvent(CoderEvent,GetMagickModule(), " MAT cannot read scanrow %u from a file.", (unsigned)(MATLAB_HDR.SizeY-i-1)); + ThrowReaderException(CorruptImageError,"UnexpectedEndOfFile"); goto ExitLoop; } if((CellType==miINT8 || CellType==miUINT8) && (MATLAB_HDR.StructureFlag & FLAG_LOGICAL)) |