From 0417cea1b6d72f90bd4f1f573f91e42a8ba66a89 Mon Sep 17 00:00:00 2001 From: Cristy Date: Mon, 17 Jul 2017 13:47:41 -0400 Subject: [PATCH] https://github.com/ImageMagick/ImageMagick/issues/574 --- MagickCore/quantize.c | 7 ++++-- coders/palm.c | 58 ++++++++++++++++++++++++++----------------- 2 files changed, 40 insertions(+), 25 deletions(-) diff --git a/MagickCore/quantize.c b/MagickCore/quantize.c index 073ae7f14..851a24015 100644 --- a/MagickCore/quantize.c +++ b/MagickCore/quantize.c @@ -3329,8 +3329,11 @@ static MagickBooleanType SetGrayscaleImage(Image *image, (void) ResetMagickMemory(colormap_index,(-1),MaxColormapSize* sizeof(*colormap_index)); if (AcquireImageColormap(image,MaxColormapSize,exception) == MagickFalse) - ThrowBinaryException(ResourceLimitError,"MemoryAllocationFailed", - image->filename); + { + colormap_index=(ssize_t *) RelinquishMagickMemory(colormap_index); + ThrowBinaryException(ResourceLimitError,"MemoryAllocationFailed", + image->filename); + } image->colors=0; status=MagickTrue; image_view=AcquireAuthenticCacheView(image,exception); diff --git a/coders/palm.c b/coders/palm.c index 8b1152b30..6283f3f94 100644 --- a/coders/palm.c +++ b/coders/palm.c @@ -278,7 +278,7 @@ static Image *ReadPALMImage(const ImageInfo *image_info, y; unsigned char - *lastrow, + *last_row, *one_row, *ptr; @@ -408,12 +408,12 @@ static Image *ReadPALMImage(const ImageInfo *image_info, 2*image->columns),sizeof(*one_row)); if (one_row == (unsigned char *) NULL) ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed"); - lastrow=(unsigned char *) NULL; + last_row=(unsigned char *) NULL; if (compressionType == PALM_COMPRESSION_SCANLINE) { - lastrow=(unsigned char *) AcquireQuantumMemory(MagickMax(bytes_per_row, - 2*image->columns),sizeof(*lastrow)); - if (lastrow == (unsigned char *) NULL) + last_row=(unsigned char *) AcquireQuantumMemory(MagickMax(bytes_per_row, + 2*image->columns),sizeof(*last_row)); + if (last_row == (unsigned char *) NULL) ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed"); } mask=(size_t) (1U << bits_per_pixel)-1; @@ -464,10 +464,10 @@ static Image *ReadPALMImage(const ImageInfo *image_info, if ((y == 0) || (count & (one << (7 - bit)))) one_row[i+bit]=(unsigned char) ReadBlobByte(image); else - one_row[i+bit]=lastrow[i+bit]; + one_row[i+bit]=last_row[i+bit]; } } - (void) CopyMagickMemory(lastrow, one_row, bytes_per_row); + (void) CopyMagickMemory(last_row, one_row, bytes_per_row); } } ptr=one_row; @@ -480,7 +480,7 @@ static Image *ReadPALMImage(const ImageInfo *image_info, { one_row=(unsigned char *) RelinquishMagickMemory(one_row); if (compressionType == PALM_COMPRESSION_SCANLINE) - lastrow=(unsigned char *) RelinquishMagickMemory(lastrow); + last_row=(unsigned char *) RelinquishMagickMemory(last_row); ThrowReaderException(CorruptImageError,"CorruptImage"); } for (x=0; x < (ssize_t) image->columns; x++) @@ -506,7 +506,7 @@ static Image *ReadPALMImage(const ImageInfo *image_info, { one_row=(unsigned char *) RelinquishMagickMemory(one_row); if (compressionType == PALM_COMPRESSION_SCANLINE) - lastrow=(unsigned char *) RelinquishMagickMemory(lastrow); + last_row=(unsigned char *) RelinquishMagickMemory(last_row); ThrowReaderException(CorruptImageError,"CorruptImage"); } index=(Quantum) (mask-(((*ptr) & (mask << bit)) >> bit)); @@ -543,7 +543,7 @@ static Image *ReadPALMImage(const ImageInfo *image_info, } one_row=(unsigned char *) RelinquishMagickMemory(one_row); if (compressionType == PALM_COMPRESSION_SCANLINE) - lastrow=(unsigned char *) RelinquishMagickMemory(lastrow); + last_row=(unsigned char *) RelinquishMagickMemory(last_row); if (EOFBlob(image) != MagickFalse) { ThrowFileException(exception,CorruptImageError,"UnexpectedEndOfFile", @@ -718,7 +718,7 @@ static MagickBooleanType WritePALMImage(const ImageInfo *image_info, bit, byte, color, - *lastrow, + *last_row, *one_row, *ptr, version; @@ -861,15 +861,24 @@ static MagickBooleanType WritePALMImage(const ImageInfo *image_info, } if (flags & PALM_IS_COMPRESSED_FLAG) (void) WriteBlobMSBShort(image,0); /* fill in size later */ - lastrow=(unsigned char *) NULL; + last_row=(unsigned char *) NULL; if (image_info->compression == FaxCompression) - lastrow=(unsigned char *) AcquireQuantumMemory(bytes_per_row, - sizeof(*lastrow)); - /* TODO check whether memory really was acquired? */ + { + last_row=(unsigned char *) AcquireQuantumMemory(bytes_per_row, + sizeof(*last_row)); + if (last_row == (unsigned char *) NULL) + { + quantize_info=DestroyQuantizeInfo(quantize_info); + ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed"); + } + } one_row=(unsigned char *) AcquireQuantumMemory(bytes_per_row, sizeof(*one_row)); if (one_row == (unsigned char *) NULL) - ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed"); + { + quantize_info=DestroyQuantizeInfo(quantize_info); + ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed"); + } for (y=0; y < (ssize_t) image->rows; y++) { ptr=one_row; @@ -949,7 +958,7 @@ static MagickBooleanType WritePALMImage(const ImageInfo *image_info, tptr = tmpbuf; for (bit=0, byte=0; bit < (unsigned char) MagickMin(8,(ssize_t) bytes_per_row-x); bit++) { - if ((y == 0) || (lastrow[x + bit] != one_row[x + bit])) + if ((y == 0) || (last_row[x + bit] != one_row[x + bit])) { byte |= (1 << (7 - bit)); *tptr++ = (char) one_row[x + bit]; @@ -958,7 +967,7 @@ static MagickBooleanType WritePALMImage(const ImageInfo *image_info, (void) WriteBlobByte(image, byte); (void) WriteBlob(image,tptr-tmpbuf,(unsigned char *) tmpbuf); } - (void) CopyMagickMemory(lastrow,one_row,bytes_per_row); + (void) CopyMagickMemory(last_row,one_row,bytes_per_row); } else (void) WriteBlob(image,bytes_per_row,one_row); @@ -974,9 +983,12 @@ static MagickBooleanType WritePALMImage(const ImageInfo *image_info, { offset=SeekBlob(image,currentOffset+20,SEEK_SET); (void) WriteBlobByte(image,0); /* reserved by Palm */ - (void) WriteBlobByte(image,(unsigned char) ((31*transpix.red)/QuantumRange)); - (void) WriteBlobByte(image,(unsigned char) ((63*transpix.green)/QuantumRange)); - (void) WriteBlobByte(image,(unsigned char) ((31*transpix.blue)/QuantumRange)); + (void) WriteBlobByte(image,(unsigned char) ((31*transpix.red)/ + QuantumRange)); + (void) WriteBlobByte(image,(unsigned char) ((63*transpix.green)/ + QuantumRange)); + (void) WriteBlobByte(image,(unsigned char) ((31*transpix.blue)/ + QuantumRange)); } if (flags & PALM_IS_COMPRESSED_FLAG) /* fill in size now */ { @@ -986,8 +998,8 @@ static MagickBooleanType WritePALMImage(const ImageInfo *image_info, } if (one_row != (unsigned char *) NULL) one_row=(unsigned char *) RelinquishMagickMemory(one_row); - if (lastrow != (unsigned char *) NULL) - lastrow=(unsigned char *) RelinquishMagickMemory(lastrow); + if (last_row != (unsigned char *) NULL) + last_row=(unsigned char *) RelinquishMagickMemory(last_row); if (GetNextImageInList(image) == (Image *) NULL) break; /* padding to 4 byte word */ -- 2.40.0