--- ImageMagick-6.8.1-1/coders/pcx.c.orig 2012-12-07 08:14:16.000000000 -0500 +++ ImageMagick-6.8.1-1/coders/pcx.c 2014-11-13 19:01:34.905378965 -0500 @@ -220,6 +220,13 @@ static inline size_t MagickMin(const siz static Image *ReadPCXImage(const ImageInfo *image_info,ExceptionInfo *exception) { +#define ThrowPCXException(severity,tag) \ + { \ + scanline=(unsigned char *) RelinquishMagickMemory(scanline); \ + pcx_pixels=(unsigned char *) RelinquishMagickMemory(pcx_pixels); \ + ThrowReaderException(severity,tag); \ + } + Image *image; @@ -264,7 +271,7 @@ static Image *ReadPCXImage(const ImageIn unsigned char packet, - *pcx_colormap, + pcx_colormap[768], *pcx_pixels, *scanline; @@ -317,7 +324,6 @@ static Image *ReadPCXImage(const ImageIn if (offset < 0) ThrowReaderException(CorruptImageError,"ImproperImageHeader"); } - pcx_colormap=(unsigned char *) NULL; count=ReadBlob(image,1,&pcx_info.identifier); for (id=1; id < 1024; id++) { @@ -350,10 +356,6 @@ static Image *ReadPCXImage(const ImageIn image->x_resolution=(double) pcx_info.horizontal_resolution; image->y_resolution=(double) pcx_info.vertical_resolution; image->colors=16; - pcx_colormap=(unsigned char *) AcquireQuantumMemory(256UL, - 3*sizeof(*pcx_colormap)); - if (pcx_colormap == (unsigned char *) NULL) - ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed"); count=ReadBlob(image,3*image->colors,pcx_colormap); pcx_info.reserved=(unsigned char) ReadBlobByte(image); pcx_info.planes=(unsigned char) ReadBlobByte(image); @@ -387,6 +389,9 @@ static Image *ReadPCXImage(const ImageIn pcx_packets=(size_t) image->rows*pcx_info.bytes_per_line* pcx_info.planes; pcx_pixels=(unsigned char *) AcquireQuantumMemory(pcx_packets, + if ((size_t) (pcx_info.bits_per_pixel*pcx_info.planes*image->columns) > + (pcx_packets*8U)) + ThrowReaderException(CorruptImageError,"ImproperImageHeader"); sizeof(*pcx_pixels)); scanline=(unsigned char *) AcquireQuantumMemory(MagickMax(image->columns, pcx_info.bytes_per_line),MagickMax(8,pcx_info.planes)*sizeof(*scanline)); @@ -402,7 +407,7 @@ static Image *ReadPCXImage(const ImageIn { packet=(unsigned char) ReadBlobByte(image); if (EOFBlob(image) != MagickFalse) - ThrowReaderException(CorruptImageError,"UnexpectedEndOfFile"); + ThrowPCXException(CorruptImageError,"UnexpectedEndOfFile"); *p++=packet; pcx_packets--; } @@ -411,7 +416,7 @@ static Image *ReadPCXImage(const ImageIn { packet=(unsigned char) ReadBlobByte(image); if (EOFBlob(image) != MagickFalse) - ThrowReaderException(CorruptImageError,"UnexpectedEndOfFile"); + ThrowPCXException(CorruptImageError,"UnexpectedEndOfFile"); if ((packet & 0xc0) != 0xc0) { *p++=packet; @@ -421,7 +426,7 @@ static Image *ReadPCXImage(const ImageIn count=(ssize_t) (packet & 0x3f); packet=(unsigned char) ReadBlobByte(image); if (EOFBlob(image) != MagickFalse) - ThrowReaderException(CorruptImageError,"UnexpectedEndOfFile"); + ThrowPCXException(CorruptImageError,"UnexpectedEndOfFile"); for ( ; count != 0; count--) { *p++=packet; @@ -440,7 +445,7 @@ static Image *ReadPCXImage(const ImageIn Initialize image colormap. */ if (image->colors > 256) - ThrowReaderException(CorruptImageError,"ColormapExceeds256Colors"); + ThrowPCXException(CorruptImageError,"ColormapExceeds256Colors"); if ((pcx_info.bits_per_pixel*pcx_info.planes) == 1) { /* @@ -469,7 +474,6 @@ static Image *ReadPCXImage(const ImageIn image->colormap[i].blue=ScaleCharToQuantum(*p++); } } - pcx_colormap=(unsigned char *) RelinquishMagickMemory(pcx_colormap); } /* Convert PCX raster image to pixel packets. @@ -626,8 +630,6 @@ static Image *ReadPCXImage(const ImageIn if (image->storage_class == PseudoClass) (void) SyncImage(image); scanline=(unsigned char *) RelinquishMagickMemory(scanline); - if (pcx_colormap != (unsigned char *) NULL) - pcx_colormap=(unsigned char *) RelinquishMagickMemory(pcx_colormap); pcx_pixels=(unsigned char *) RelinquishMagickMemory(pcx_pixels); if (EOFBlob(image) != MagickFalse) {