Skip to content

Commit

Permalink
Refactored ReadMNGImage to fix memory leak reported in #442
Browse files Browse the repository at this point in the history
  • Loading branch information
dlemstra committed Apr 27, 2017
1 parent 852b590 commit 7dea03d
Showing 1 changed file with 63 additions and 44 deletions.
107 changes: 63 additions & 44 deletions coders/png.c
Expand Up @@ -5268,7 +5268,7 @@ static Image *ReadJNGImage(const ImageInfo *image_info,
}
#endif

static Image *ReadMNGImage(const ImageInfo *image_info,
static Image *ReadOneMNGImage(MngInfo* mng_info, const ImageInfo *image_info,
ExceptionInfo *exception)
{
char
Expand All @@ -5295,9 +5295,6 @@ static Image *ReadMNGImage(const ImageInfo *image_info,
MagickOffsetType
offset;

MngInfo
*mng_info;

MngBox
default_fb,
fb,
Expand Down Expand Up @@ -5368,36 +5365,10 @@ static Image *ReadMNGImage(const ImageInfo *image_info,
default_fb.left=0;
default_fb.right=0;

/* Open image file. */

assert(image_info != (const ImageInfo *) NULL);
assert(image_info->signature == MagickCoreSignature);
(void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",
image_info->filename);
assert(exception != (ExceptionInfo *) NULL);
assert(exception->signature == MagickCoreSignature);
logging=LogMagickEvent(CoderEvent,GetMagickModule(),"Enter ReadMNGImage()");
image=AcquireImage(image_info,exception);
mng_info=(MngInfo *) NULL;
status=OpenBlob(image_info,image,ReadBinaryBlobMode,exception);

if (status == MagickFalse)
return((Image *) NULL);

first_mng_object=MagickFalse;
skipping_loop=(-1);

/* Allocate a MngInfo structure. */

mng_info=(MngInfo *) AcquireMagickMemory(sizeof(MngInfo));

if (mng_info == (MngInfo *) NULL)
ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");

/* Initialize members of the MngInfo structure. */
logging=LogMagickEvent(CoderEvent,GetMagickModule(),
" Enter ReadOneMNGImage()");

(void) ResetMagickMemory(mng_info,0,sizeof(MngInfo));
mng_info->image=image;
image=mng_info->image;

if (LocaleCompare(image_info->magick,"MNG") == 0)
{
Expand All @@ -5418,6 +5389,7 @@ static Image *ReadMNGImage(const ImageInfo *image_info,
mng_info->exists[0]=MagickTrue;
}

skipping_loop=(-1);
first_mng_object=MagickTrue;
mng_type=0;
#if defined(MNG_INSERT_LAYERS)
Expand Down Expand Up @@ -6039,7 +6011,6 @@ static Image *ReadMNGImage(const ImageInfo *image_info,
if (GetNextImageInList(image) == (Image *) NULL)
{
image=DestroyImageList(image);
mng_info=MngInfoFreeStruct(mng_info);
return((Image *) NULL);
}

Expand Down Expand Up @@ -6616,7 +6587,6 @@ static Image *ReadMNGImage(const ImageInfo *image_info,
if (GetNextImageInList(image) == (Image *) NULL)
{
image=DestroyImageList(image);
MngInfoFreeStruct(mng_info);
return((Image *) NULL);
}

Expand Down Expand Up @@ -6669,7 +6639,6 @@ static Image *ReadMNGImage(const ImageInfo *image_info,
if (GetNextImageInList(image) == (Image *) NULL)
{
image=DestroyImageList(image);
mng_info=MngInfoFreeStruct(mng_info);
return((Image *) NULL);
}

Expand Down Expand Up @@ -6718,7 +6687,6 @@ static Image *ReadMNGImage(const ImageInfo *image_info,
if (GetNextImageInList(image) == (Image *) NULL)
{
image=DestroyImageList(image);
mng_info=MngInfoFreeStruct(mng_info);
return((Image *) NULL);
}

Expand Down Expand Up @@ -6788,15 +6756,13 @@ static Image *ReadMNGImage(const ImageInfo *image_info,
(void) LogMagickEvent(CoderEvent,GetMagickModule(),
"exit ReadJNGImage() with error");

mng_info=MngInfoFreeStruct(mng_info);
return((Image *) NULL);
}

if (image->columns == 0 || image->rows == 0)
{
(void) CloseBlob(image);
image=DestroyImageList(image);
mng_info=MngInfoFreeStruct(mng_info);
return((Image *) NULL);
}

Expand Down Expand Up @@ -6910,7 +6876,6 @@ static Image *ReadMNGImage(const ImageInfo *image_info,
if (GetNextImageInList(image) == (Image *) NULL)
{
image=DestroyImageList(image);
mng_info=MngInfoFreeStruct(mng_info);
return((Image *) NULL);
}

Expand Down Expand Up @@ -6993,7 +6958,6 @@ static Image *ReadMNGImage(const ImageInfo *image_info,
(next == (Quantum *) NULL))
{
image=DestroyImageList(image);
mng_info=MngInfoFreeStruct(mng_info);
ThrowReaderException(ResourceLimitError,
"MemoryAllocationFailed");
}
Expand Down Expand Up @@ -7491,7 +7455,6 @@ static Image *ReadMNGImage(const ImageInfo *image_info,
if (GetNextImageInList(image) == (Image *) NULL)
{
image=DestroyImageList(image);
mng_info=MngInfoFreeStruct(mng_info);

if (logging != MagickFalse)
(void) LogMagickEvent(CoderEvent,GetMagickModule(),
Expand Down Expand Up @@ -7574,7 +7537,6 @@ static Image *ReadMNGImage(const ImageInfo *image_info,
if (image != (Image *) NULL)
image=DestroyImageList(image);

mng_info=MngInfoFreeStruct(mng_info);
return((Image *) NULL);
}

Expand Down Expand Up @@ -7709,9 +7671,66 @@ static Image *ReadMNGImage(const ImageInfo *image_info,
}
}

image=GetFirstImageInList(image);
if (logging != MagickFalse)
(void) LogMagickEvent(CoderEvent,GetMagickModule(),
" exit ReadOneMNGImage();");

return(image);
}

static Image *ReadMNGImage(const ImageInfo *image_info,
ExceptionInfo *exception)
{
Image
*image;

MagickBooleanType
logging,
status;

MngInfo
*mng_info;

/* Open image file. */

assert(image_info != (const ImageInfo *) NULL);
assert(image_info->signature == MagickCoreSignature);
(void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",
image_info->filename);
assert(exception != (ExceptionInfo *) NULL);
assert(exception->signature == MagickCoreSignature);
logging=LogMagickEvent(CoderEvent,GetMagickModule(),"Enter ReadMNGImage()");
image=AcquireImage(image_info,exception);
mng_info=(MngInfo *) NULL;
status=OpenBlob(image_info,image,ReadBinaryBlobMode,exception);

if (status == MagickFalse)
return((Image *) NULL);

/* Allocate a MngInfo structure. */

mng_info=(MngInfo *) AcquireMagickMemory(sizeof(MngInfo));

if (mng_info == (MngInfo *) NULL)
ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");

/* Initialize members of the MngInfo structure. */

(void) ResetMagickMemory(mng_info,0,sizeof(MngInfo));
mng_info->image=image;
image=ReadOneMNGImage(mng_info,image_info,exception);
mng_info=MngInfoFreeStruct(mng_info);

if (image == (Image *) NULL)
{
if (logging != MagickFalse)
(void) LogMagickEvent(CoderEvent,GetMagickModule(),
"exit ReadMNGImage() with error");

return((Image *) NULL);
}
(void) CloseBlob(image);

if (logging != MagickFalse)
(void) LogMagickEvent(CoderEvent,GetMagickModule(),"exit ReadMNGImage()");

Expand Down

0 comments on commit 7dea03d

Please sign in to comment.