Commit 41219518f for imagemagick.org
commit 41219518f4cbca4b552636aa7566c74360f94c59
Author: Cristy <urban-warrior@imagemagick.org>
Date: Fri Dec 4 19:02:54 2026 -0500
ReadCUTImage no longer writes ".PAL" at clone_info->filename[-1] when the input filename is empty
diff --git a/coders/cut.c b/coders/cut.c
index a558b387d..fb7c03125 100644
--- a/coders/cut.c
+++ b/coders/cut.c
@@ -132,7 +132,6 @@ static MagickBooleanType InsertRow(Image *image,ssize_t bpp,unsigned char *p,
SetPixelIndex(image,index,q);
if (index < image->colors)
SetPixelViaPixelInfo(image,image->colormap+(ssize_t) index,q);
- SetPixelViaPixelInfo(image,image->colormap+(ssize_t) index,q);
q+=(ptrdiff_t) GetPixelChannels(image);
}
p++;
@@ -320,10 +319,17 @@ static Image *ReadCUTImage(const ImageInfo *image_info,ExceptionInfo *exception)
{
#define ThrowCUTReaderException(severity,tag) \
{ \
+ if (BImgBuff != NULL) \
+ BImgBuff=(unsigned char *) RelinquishMagickMemory(BImgBuff); \
if (palette != NULL) \
palette=DestroyImage(palette); \
if (clone_info != NULL) \
- clone_info=DestroyImageInfo(clone_info); \
+ { \
+ if (clone_info->file != (FILE *) NULL) \
+ (void) fclose(clone_info->file); \
+ clone_info->file=(FILE *) NULL; \
+ clone_info=DestroyImageInfo(clone_info); \
+ } \
ThrowReaderException(severity,tag); \
}
@@ -414,16 +420,24 @@ static Image *ReadCUTImage(const ImageInfo *image_info,ExceptionInfo *exception)
/* ----- Do something with palette ----- */
if ((clone_info=CloneImageInfo(image_info)) == NULL) goto NoPalette;
+ /*
+ CloneImageInfo() copies any caller-supplied FILE; drop it so only the
+ palette file opened below is ever closed by this reader.
+ */
+ clone_info->file=(FILE *) NULL;
+ /*
+ Locate the extension to replace with .PAL; if there is none (including
+ an empty filename, e.g. when reading from a blob) append to the end
+ rather than writing at a negative or zero offset.
+ */
i=(ssize_t) strlen(clone_info->filename);
j=i;
- while(--i>0)
+ while (--i > 0)
{
if (clone_info->filename[i]=='.')
- {
- break;
- }
+ break;
if (clone_info->filename[i]=='/' || clone_info->filename[i]=='\\' ||
clone_info->filename[i]==':' )
{
@@ -431,6 +445,14 @@ static Image *ReadCUTImage(const ImageInfo *image_info,ExceptionInfo *exception)
break;
}
}
+ if ((i <= 0) || (i > j) || (j >= (ssize_t) (MagickPathExtent-5)))
+ i=j;
+ if ((j == 0) || (j >= (ssize_t) (MagickPathExtent-5)))
+ {
+ clone_info=DestroyImageInfo(clone_info);
+ clone_info=NULL;
+ goto NoPalette;
+ }
(void) CopyMagickString(clone_info->filename+i,".PAL",(size_t)
(MagickPathExtent-i));
@@ -466,6 +488,9 @@ static Image *ReadCUTImage(const ImageInfo *image_info,ExceptionInfo *exception)
ErasePalette:
palette=DestroyImage(palette);
palette=NULL;
+ if (clone_info->file != (FILE *) NULL)
+ (void) fclose(clone_info->file);
+ clone_info->file=(FILE *) NULL;
goto NoPalette;
}
@@ -485,7 +510,7 @@ static Image *ReadCUTImage(const ImageInfo *image_info,ExceptionInfo *exception)
PalHeader.MaxGreen=ReadBlobLSBShort(palette);
PalHeader.MaxBlue=ReadBlobLSBShort(palette);
(void) ReadBlob(palette,20,(unsigned char *) PalHeader.PaletteId);
- if (EOFBlob(image))
+ if (EOFBlob(palette) != MagickFalse)
ThrowCUTReaderException(CorruptImageError,"UnexpectedEndOfFile");
if (PalHeader.MaxIndex<1) goto ErasePalette;
@@ -504,7 +529,7 @@ static Image *ReadCUTImage(const ImageInfo *image_info,ExceptionInfo *exception)
j=((j / 512)+1)*512;
offset=SeekBlob(palette,j,SEEK_SET);
if (offset < 0)
- ThrowReaderException(CorruptImageError,"ImproperImageHeader");
+ ThrowCUTReaderException(CorruptImageError,"ImproperImageHeader");
}
image->colormap[i].red=(Quantum) ReadBlobLSBShort(palette);
if (QuantumRange != (Quantum) PalHeader.MaxRed)
@@ -529,7 +554,7 @@ static Image *ReadCUTImage(const ImageInfo *image_info,ExceptionInfo *exception)
}
}
- if (EOFBlob(image))
+ if (EOFBlob(palette) != MagickFalse)
ThrowCUTReaderException(CorruptImageError,"UnexpectedEndOfFile");
}
@@ -563,14 +588,7 @@ static Image *ReadCUTImage(const ImageInfo *image_info,ExceptionInfo *exception)
offset=SeekBlob(image,6 /*sizeof(Header)*/,SEEK_SET);
if (offset < 0)
- {
- if (palette != NULL)
- palette=DestroyImage(palette);
- if (clone_info != NULL)
- clone_info=DestroyImageInfo(clone_info);
- BImgBuff=(unsigned char *) RelinquishMagickMemory(BImgBuff);
- ThrowReaderException(CorruptImageError,"ImproperImageHeader");
- }
+ ThrowCUTReaderException(CorruptImageError,"ImproperImageHeader");
for (i=0; i < (int) Header.Height; i++)
{
EncodedByte=ReadBlobLSBShort(image);
@@ -609,7 +627,8 @@ static Image *ReadCUTImage(const ImageInfo *image_info,ExceptionInfo *exception)
RunCountMasked=RunCount & 0x7F;
}
- InsertRow(image,depth,BImgBuff,i,exception);
+ if (InsertRow(image,depth,BImgBuff,i,exception) == MagickFalse)
+ break;
}
(void) SyncImage(image,exception);
@@ -663,7 +682,16 @@ static Image *ReadCUTImage(const ImageInfo *image_info,ExceptionInfo *exception)
if (palette != NULL)
palette=DestroyImage(palette);
if (clone_info != NULL)
- clone_info=DestroyImageInfo(clone_info);
+ {
+ /*
+ The palette FILE was opened here and handed to OpenBlob() via
+ clone_info->file, which marks it exempt; close it explicitly.
+ */
+ if (clone_info->file != (FILE *) NULL)
+ (void) fclose(clone_info->file);
+ clone_info->file=(FILE *) NULL;
+ clone_info=DestroyImageInfo(clone_info);
+ }
if (EOFBlob(image) != MagickFalse)
ThrowFileException(exception,CorruptImageError,"UnexpectedEndOfFile",
image->filename);