Skip to content

Commit

Permalink
Fixed memory leaks reported in #1041 and improved the error messages.
Browse files Browse the repository at this point in the history
  • Loading branch information
dlemstra committed Mar 24, 2018
1 parent 1569037 commit 53060aa
Show file tree
Hide file tree
Showing 5 changed files with 341 additions and 431 deletions.
118 changes: 44 additions & 74 deletions coders/bgr.c
Expand Up @@ -200,6 +200,7 @@ static Image *ReadBGRImage(const ImageInfo *image_info,
count=0;
length=0;
scene=0;
status=MagickTrue;
do
{
/*
Expand All @@ -210,10 +211,7 @@ static Image *ReadBGRImage(const ImageInfo *image_info,
break;
status=SetImageExtent(image,image->columns,image->rows);
if (status == MagickFalse)
{
InheritException(exception,&image->exception);
return(DestroyImageList(image));
}
break;
switch (image_info->interlace)
{
case NoInterlace:
Expand All @@ -227,8 +225,6 @@ static Image *ReadBGRImage(const ImageInfo *image_info,
length=GetQuantumExtent(canvas_image,quantum_info,quantum_type);
pixels=(const unsigned char *) ReadBlobStream(image,length,
GetQuantumPixels(quantum_info),&count);
if (count != (ssize_t) length)
break;
}
for (y=0; y < (ssize_t) image->extract_info.height; y++)
{
Expand All @@ -243,6 +239,7 @@ static Image *ReadBGRImage(const ImageInfo *image_info,

if (count != (ssize_t) length)
{
status=MagickFalse;
ThrowFileException(exception,CorruptImageError,
"UnexpectedEndOfFile",image->filename);
break;
Expand Down Expand Up @@ -288,8 +285,6 @@ static Image *ReadBGRImage(const ImageInfo *image_info,
}
pixels=(const unsigned char *) ReadBlobStream(image,length,
GetQuantumPixels(quantum_info),&count);
if (count != (ssize_t) length)
break;
}
break;
}
Expand All @@ -312,8 +307,6 @@ static Image *ReadBGRImage(const ImageInfo *image_info,
length=GetQuantumExtent(canvas_image,quantum_info,RedQuantum);
pixels=(const unsigned char *) ReadBlobStream(image,length,
GetQuantumPixels(quantum_info),&count);
if (count != (ssize_t) length)
break;
}
for (y=0; y < (ssize_t) image->extract_info.height; y++)
{
Expand All @@ -328,6 +321,7 @@ static Image *ReadBGRImage(const ImageInfo *image_info,

if (count != (ssize_t) length)
{
status=MagickFalse;
ThrowFileException(exception,CorruptImageError,
"UnexpectedEndOfFile",image->filename);
break;
Expand Down Expand Up @@ -393,8 +387,6 @@ static Image *ReadBGRImage(const ImageInfo *image_info,
}
pixels=(const unsigned char *) ReadBlobStream(image,length,
GetQuantumPixels(quantum_info),&count);
if (count != (ssize_t) length)
break;
}
if (image->previous == (Image *) NULL)
{
Expand All @@ -416,8 +408,6 @@ static Image *ReadBGRImage(const ImageInfo *image_info,
length=GetQuantumExtent(canvas_image,quantum_info,RedQuantum);
pixels=(const unsigned char *) ReadBlobStream(image,length,
GetQuantumPixels(quantum_info),&count);
if (count != (ssize_t) length)
break;
}
for (y=0; y < (ssize_t) image->extract_info.height; y++)
{
Expand All @@ -432,6 +422,7 @@ static Image *ReadBGRImage(const ImageInfo *image_info,

if (count != (ssize_t) length)
{
status=MagickFalse;
ThrowFileException(exception,CorruptImageError,
"UnexpectedEndOfFile",image->filename);
break;
Expand Down Expand Up @@ -465,8 +456,6 @@ static Image *ReadBGRImage(const ImageInfo *image_info,
}
pixels=(const unsigned char *) ReadBlobStream(image,length,
GetQuantumPixels(quantum_info),&count);
if (count != (ssize_t) length)
break;
}
if (image->previous == (Image *) NULL)
{
Expand All @@ -487,6 +476,7 @@ static Image *ReadBGRImage(const ImageInfo *image_info,

if (count != (ssize_t) length)
{
status=MagickFalse;
ThrowFileException(exception,CorruptImageError,
"UnexpectedEndOfFile",image->filename);
break;
Expand Down Expand Up @@ -520,8 +510,6 @@ static Image *ReadBGRImage(const ImageInfo *image_info,
}
pixels=(const unsigned char *) ReadBlobStream(image,length,
GetQuantumPixels(quantum_info),&count);
if (count != (ssize_t) length)
break;
}
if (image->previous == (Image *) NULL)
{
Expand All @@ -542,6 +530,7 @@ static Image *ReadBGRImage(const ImageInfo *image_info,

if (count != (ssize_t) length)
{
status=MagickFalse;
ThrowFileException(exception,CorruptImageError,
"UnexpectedEndOfFile",image->filename);
break;
Expand Down Expand Up @@ -575,8 +564,6 @@ static Image *ReadBGRImage(const ImageInfo *image_info,
}
pixels=(const unsigned char *) ReadBlobStream(image,length,
GetQuantumPixels(quantum_info),&count);
if (count != (ssize_t) length)
break;
}
if (image->previous == (Image *) NULL)
{
Expand Down Expand Up @@ -605,6 +592,7 @@ static Image *ReadBGRImage(const ImageInfo *image_info,

if (count != (ssize_t) length)
{
status=MagickFalse;
ThrowFileException(exception,CorruptImageError,
"UnexpectedEndOfFile",image->filename);
break;
Expand Down Expand Up @@ -638,8 +626,6 @@ static Image *ReadBGRImage(const ImageInfo *image_info,
}
pixels=(const unsigned char *) ReadBlobStream(image,length,
GetQuantumPixels(quantum_info),&count);
if (count != (ssize_t) length)
break;
}
if (image->previous == (Image *) NULL)
{
Expand All @@ -664,31 +650,29 @@ static Image *ReadBGRImage(const ImageInfo *image_info,
AppendImageFormat("B",image->filename);
status=OpenBlob(image_info,image,ReadBinaryBlobMode,exception);
if (status == MagickFalse)
break;
if (DiscardBlobBytes(image,(MagickSizeType) image->offset) == MagickFalse)
{
canvas_image=DestroyImageList(canvas_image);
image=DestroyImageList(image);
return((Image *) NULL);
status=MagickFalse;
ThrowFileException(exception,CorruptImageError,"UnexpectedEndOfFile",
image->filename);
break;
}
if (DiscardBlobBytes(image,(MagickSizeType) image->offset) == MagickFalse)
ThrowFileException(exception,CorruptImageError,"UnexpectedEndOfFile",
image->filename);
length=GetQuantumExtent(canvas_image,quantum_info,BlueQuantum);
for (i=0; i < (ssize_t) scene; i++)
{
for (y=0; y < (ssize_t) image->extract_info.height; y++)
{
(void) ReadBlobStream(image,length,GetQuantumPixels(quantum_info),
&count);
if (count != (ssize_t) length)
{
ThrowFileException(exception,CorruptImageError,
"UnexpectedEndOfFile",image->filename);
break;
}
break;
}
if (count != (ssize_t) length)
break;
}
pixels=(const unsigned char *) ReadBlobStream(image,length,
GetQuantumPixels(quantum_info),&count);
if (count != (ssize_t) length)
break;
for (y=0; y < (ssize_t) image->extract_info.height; y++)
{
register const PixelPacket
Expand All @@ -702,6 +686,7 @@ static Image *ReadBGRImage(const ImageInfo *image_info,

if (count != (ssize_t) length)
{
status=MagickFalse;
ThrowFileException(exception,CorruptImageError,
"UnexpectedEndOfFile",image->filename);
break;
Expand Down Expand Up @@ -735,8 +720,6 @@ static Image *ReadBGRImage(const ImageInfo *image_info,
}
pixels=(const unsigned char *) ReadBlobStream(image,length,
GetQuantumPixels(quantum_info),&count);
if (count != (ssize_t) length)
break;
}
if (image->previous == (Image *) NULL)
{
Expand All @@ -748,28 +731,22 @@ static Image *ReadBGRImage(const ImageInfo *image_info,
AppendImageFormat("G",image->filename);
status=OpenBlob(image_info,image,ReadBinaryBlobMode,exception);
if (status == MagickFalse)
{
canvas_image=DestroyImageList(canvas_image);
image=DestroyImageList(image);
return((Image *) NULL);
}
break;
length=GetQuantumExtent(canvas_image,quantum_info,GreenQuantum);
for (i=0; i < (ssize_t) scene; i++)
{
for (y=0; y < (ssize_t) image->extract_info.height; y++)
{
(void) ReadBlobStream(image,length,GetQuantumPixels(quantum_info),
&count);
if (count != (ssize_t) length)
{
ThrowFileException(exception,CorruptImageError,
"UnexpectedEndOfFile",image->filename);
break;
}
break;
}
if (count != (ssize_t) length)
break;
}
pixels=(const unsigned char *) ReadBlobStream(image,length,
GetQuantumPixels(quantum_info),&count);
if (count != (ssize_t) length)
break;
for (y=0; y < (ssize_t) image->extract_info.height; y++)
{
register const PixelPacket
Expand All @@ -783,6 +760,7 @@ static Image *ReadBGRImage(const ImageInfo *image_info,

if (count != (ssize_t) length)
{
status=MagickFalse;
ThrowFileException(exception,CorruptImageError,
"UnexpectedEndOfFile",image->filename);
break;
Expand Down Expand Up @@ -816,8 +794,6 @@ static Image *ReadBGRImage(const ImageInfo *image_info,
}
pixels=(const unsigned char *) ReadBlobStream(image,length,
GetQuantumPixels(quantum_info),&count);
if (count != (ssize_t) length)
break;
}
if (image->previous == (Image *) NULL)
{
Expand All @@ -829,28 +805,22 @@ static Image *ReadBGRImage(const ImageInfo *image_info,
AppendImageFormat("R",image->filename);
status=OpenBlob(image_info,image,ReadBinaryBlobMode,exception);
if (status == MagickFalse)
{
canvas_image=DestroyImageList(canvas_image);
image=DestroyImageList(image);
return((Image *) NULL);
}
break;
length=GetQuantumExtent(canvas_image,quantum_info,RedQuantum);
for (i=0; i < (ssize_t) scene; i++)
{
for (y=0; y < (ssize_t) image->extract_info.height; y++)
{
(void) ReadBlobStream(image,length,GetQuantumPixels(quantum_info),
&count);
if (count != (ssize_t) length)
{
ThrowFileException(exception,CorruptImageError,
"UnexpectedEndOfFile",image->filename);
break;
}
break;
}
if (count != (ssize_t) length)
break;
}
pixels=(const unsigned char *) ReadBlobStream(image,length,
GetQuantumPixels(quantum_info),&count);
if (count != (ssize_t) length)
break;
for (y=0; y < (ssize_t) image->extract_info.height; y++)
{
register const PixelPacket
Expand All @@ -864,6 +834,7 @@ static Image *ReadBGRImage(const ImageInfo *image_info,

if (count != (ssize_t) length)
{
status=MagickFalse;
ThrowFileException(exception,CorruptImageError,
"UnexpectedEndOfFile",image->filename);
break;
Expand Down Expand Up @@ -897,8 +868,6 @@ static Image *ReadBGRImage(const ImageInfo *image_info,
}
pixels=(const unsigned char *) ReadBlobStream(image,length,
GetQuantumPixels(quantum_info),&count);
if (count != (ssize_t) length)
break;
}
if (image->previous == (Image *) NULL)
{
Expand All @@ -919,21 +888,19 @@ static Image *ReadBGRImage(const ImageInfo *image_info,
}
length=GetQuantumExtent(canvas_image,quantum_info,AlphaQuantum);
for (i=0; i < (ssize_t) scene; i++)
{
for (y=0; y < (ssize_t) image->extract_info.height; y++)
{
(void) ReadBlobStream(image,length,GetQuantumPixels(
quantum_info),&count);
if (count != (ssize_t) length)
{
ThrowFileException(exception,CorruptImageError,
"UnexpectedEndOfFile",image->filename);
break;
}
break;
}
if (count != (ssize_t) length)
break;
}
pixels=(const unsigned char *) ReadBlobStream(image,length,
GetQuantumPixels(quantum_info),&count);
if (count != (ssize_t) length)
break;
for (y=0; y < (ssize_t) image->extract_info.height; y++)
{
register const PixelPacket
Expand All @@ -947,6 +914,7 @@ static Image *ReadBGRImage(const ImageInfo *image_info,

if (count != (ssize_t) length)
{
status=MagickFalse;
ThrowFileException(exception,CorruptImageError,
"UnexpectedEndOfFile",image->filename);
break;
Expand Down Expand Up @@ -980,8 +948,6 @@ static Image *ReadBGRImage(const ImageInfo *image_info,
}
pixels=(const unsigned char *) ReadBlobStream(image,length,
GetQuantumPixels(quantum_info),&count);
if (count != (ssize_t) length)
break;
}
if (image->previous == (Image *) NULL)
{
Expand All @@ -1000,6 +966,8 @@ static Image *ReadBGRImage(const ImageInfo *image_info,
break;
}
}
if (status == MagickFalse)
break;
SetQuantumImageType(image,quantum_type);
/*
Proceed to next image.
Expand Down Expand Up @@ -1030,6 +998,8 @@ static Image *ReadBGRImage(const ImageInfo *image_info,
InheritException(&image->exception,&canvas_image->exception);
canvas_image=DestroyImage(canvas_image);
(void) CloseBlob(image);
if (status == MagickFalse)
return(DestroyImageList(image));
return(GetFirstImageInList(image));
}

Expand Down

0 comments on commit 53060aa

Please sign in to comment.