Commit 65d66c895 for imagemagick.org

commit 65d66c89558e50f31bf9caae9c39d0d1b104d2d8
Author: Cristy <urban-warrior@imagemagick.org>
Date:   Tue Dec 1 19:23:03 2026 -0500

    the writer’s summary strings came from ImageMagick’s allocator but were freed with FPX_DeleteFPXSt

diff --git a/coders/fpx.c b/coders/fpx.c
index a7e444862..f7113b7cf 100644
--- a/coders/fpx.c
+++ b/coders/fpx.c
@@ -136,6 +136,18 @@ static void FPXRelinquishSummaryInfo(FPXSummaryInformation *summary_info)
     (void) FPX_DeleteFPXStr(&summary_info->appname);
 }

+#define ThrowFPXReaderException(severity,tag) \
+do \
+{ \
+  if (pixels != (unsigned char *) NULL) \
+    pixels=(unsigned char *) RelinquishMagickMemory(pixels); \
+  if (flashpix != (FPXImageHandle *) NULL) \
+    (void) FPX_CloseImage(flashpix); \
+  FPXRelinquishSummaryInfo(&summary_info); \
+  FPX_ClearSystem(); \
+  ThrowReaderException(severity,tag); \
+} while (0)
+
 static Image *ReadFPXImage(const ImageInfo *image_info,ExceptionInfo *exception)
 {
   const char
@@ -154,7 +166,7 @@ static Image *ReadFPXImage(const ImageInfo *image_info,ExceptionInfo *exception)
     fpx_info;

   FPXImageHandle
-    *flashpix;
+    *flashpix = (FPXImageHandle *) NULL;

   FPXStatus
     fpx_status;
@@ -191,7 +203,7 @@ static Image *ReadFPXImage(const ImageInfo *image_info,ExceptionInfo *exception)
     y;

   unsigned char
-    *pixels;
+    *pixels = (unsigned char *) NULL;

   unsigned int
     height,
@@ -230,22 +242,16 @@ static Image *ReadFPXImage(const ImageInfo *image_info,ExceptionInfo *exception)
   memory_limit=20000000;
   fpx_status=FPX_SetToolkitMemoryLimit(&memory_limit);
   if (fpx_status != FPX_OK)
-    {
-      FPXRelinquishSummaryInfo(&summary_info);
-      FPX_ClearSystem();
-      ThrowReaderException(CoderError,"UnableToInitializeFPXLibrary");
-    }
+    ThrowFPXReaderException(CoderError,"UnableToInitializeFPXLibrary");
   tile_width=64;
   tile_height=64;
   flashpix=(FPXImageHandle *) NULL;
   fpx_status=FPX_OpenImageByFilename(image->filename,(char *) NULL,&width,
     &height,&tile_width,&tile_height,&colorspace,&flashpix);
+  if (fpx_status != FPX_OK)
+    flashpix=(FPXImageHandle *) NULL;
   if (fpx_status == FPX_LOW_MEMORY_ERROR)
-    {
-      FPXRelinquishSummaryInfo(&summary_info);
-      FPX_ClearSystem();
-      ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
-    }
+    ThrowFPXReaderException(ResourceLimitError,"MemoryAllocationFailed");
   if (fpx_status != FPX_OK)
     {
       FPX_ClearSystem();
@@ -254,12 +260,9 @@ static Image *ReadFPXImage(const ImageInfo *image_info,ExceptionInfo *exception)
       image=DestroyImageList(image);
       return((Image *) NULL);
     }
-  if (colorspace.numberOfComponents == 0)
-    {
-      FPXRelinquishSummaryInfo(&summary_info);
-      FPX_ClearSystem();
-      ThrowReaderException(ImageError,"ImageTypeNotSupported");
-    }
+  if ((width == 0) || (height == 0) || (colorspace.numberOfComponents <= 0) ||
+      (colorspace.numberOfComponents > 4))
+    ThrowFPXReaderException(ImageError,"ImageTypeNotSupported");
   option=GetImageOption(image_info,"fpx:view");
   if (option == (const char *) NULL)
     {
@@ -272,17 +275,13 @@ static Image *ReadFPXImage(const ImageInfo *image_info,ExceptionInfo *exception)
       aspect_ratio=(float) width/height;
       fpx_status=FPX_GetImageResultAspectRatio(flashpix,&aspect_ratio);
       if (fpx_status != FPX_OK)
-        ThrowReaderException(DelegateError,"UnableToReadAspectRatio");
+        ThrowFPXReaderException(DelegateError,"UnableToReadAspectRatio");
       if (width != (size_t) floor((aspect_ratio*height)+0.5))
         Swap(width,height);
     }
   fpx_status=FPX_GetSummaryInformation(flashpix,&summary_info);
   if (fpx_status != FPX_OK)
-    {
-      FPXRelinquishSummaryInfo(&summary_info);
-      FPX_ClearSystem();
-      ThrowReaderException(DelegateError,"UnableToReadSummaryInfo");
-    }
+    ThrowFPXReaderException(DelegateError,"UnableToReadSummaryInfo");
   if (summary_info.title_valid)
     if ((summary_info.title.length != 0) &&
         (summary_info.title.ptr != (unsigned char *) NULL))
@@ -298,11 +297,7 @@ static Image *ReadFPXImage(const ImageInfo *image_info,ExceptionInfo *exception)
           label=(char *) AcquireQuantumMemory(summary_info.title.length+
             MagickPathExtent,sizeof(*label));
         if (label == (char *) NULL)
-          {
-            FPXRelinquishSummaryInfo(&summary_info);
-            FPX_ClearSystem();
-            ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
-          }
+          ThrowFPXReaderException(ResourceLimitError,"MemoryAllocationFailed");
         (void) CopyMagickString(label,(char *) summary_info.title.ptr,
           summary_info.title.length+1);
         (void) SetImageProperty(image,"label",label,exception);
@@ -323,11 +318,7 @@ static Image *ReadFPXImage(const ImageInfo *image_info,ExceptionInfo *exception)
           comments=(char *) AcquireQuantumMemory(summary_info.comments.length+
             MagickPathExtent,sizeof(*comments));
         if (comments == (char *) NULL)
-          {
-            FPXRelinquishSummaryInfo(&summary_info);
-            FPX_ClearSystem();
-            ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
-          }
+          ThrowFPXReaderException(ResourceLimitError,"MemoryAllocationFailed");
         (void) CopyMagickString(comments,(char *) summary_info.comments.ptr,
           summary_info.comments.length+1);
         (void) SetImageProperty(image,"comment",comments,exception);
@@ -366,11 +357,7 @@ static Image *ReadFPXImage(const ImageInfo *image_info,ExceptionInfo *exception)
         Create linear colormap.
       */
       if (AcquireImageColormap(image,MaxColormapSize,exception) == MagickFalse)
-        {
-          FPXRelinquishSummaryInfo(&summary_info);
-          FPX_ClearSystem();
-          ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
-        }
+        ThrowFPXReaderException(ResourceLimitError,"MemoryAllocationFailed");
     }
   if (image_info->ping != MagickFalse)
     {
@@ -381,19 +368,19 @@ static Image *ReadFPXImage(const ImageInfo *image_info,ExceptionInfo *exception)
     }
   status=SetImageExtent(image,image->columns,image->rows,exception);
   if (status == MagickFalse)
-    return(DestroyImageList(image));
+    {
+      (void) FPX_CloseImage(flashpix);
+      FPXRelinquishSummaryInfo(&summary_info);
+      FPX_ClearSystem();
+      return(DestroyImageList(image));
+    }
   /*
     Allocate memory for the image and pixel buffer.
   */
   pixels=(unsigned char *) AcquireQuantumMemory(image->columns,(tile_height+1)*
     (size_t) colorspace.numberOfComponents*sizeof(*pixels));
   if (pixels == (unsigned char *) NULL)
-    {
-      FPXRelinquishSummaryInfo(&summary_info);
-      FPX_ClearSystem();
-      (void) FPX_CloseImage(flashpix);
-      ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
-    }
+    ThrowFPXReaderException(ResourceLimitError,"MemoryAllocationFailed");
   /*
     Initialize FlashPix image description.
   */
@@ -441,12 +428,16 @@ static Image *ReadFPXImage(const ImageInfo *image_info,ExceptionInfo *exception)
             (float) (y+tile_height-1)/image->rows,(int) image->columns,
             (int) tile_height,&fpx_info);
         if (fpx_status == FPX_LOW_MEMORY_ERROR)
+          ThrowFPXReaderException(ResourceLimitError,"MemoryAllocationFailed");
+        if (fpx_status != FPX_OK)
           {
-            pixels=(unsigned char *) RelinquishMagickMemory(pixels);
-            (void) FPX_CloseImage(flashpix);
-            FPXRelinquishSummaryInfo(&summary_info);
-            FPX_ClearSystem();
-            ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
+            /*
+              Do not silently emit stale tile data for a failed read.
+            */
+            (void) ThrowMagickException(exception,GetMagickModule(),
+              CorruptImageWarning,"UnableToReadImageData","`%s'",
+              image->filename);
+            break;
           }
       }
     /*
@@ -529,8 +520,14 @@ ModuleExport size_t RegisterFPXImage(void)
   entry->decoder=(DecodeImageHandler *) ReadFPXImage;
   entry->encoder=(EncodeImageHandler *) WriteFPXImage;
 #endif
-  entry->flags^=CoderAdjoinFlag;
-  entry->flags^=CoderBlobSupportFlag;
+  entry->flags&=~CoderAdjoinFlag;
+  entry->flags&=~CoderBlobSupportFlag;
+  /*
+    The FlashPix toolkit keeps global state (FPX_InitSystem/FPX_ClearSystem),
+    so do not let the coder run concurrently.
+  */
+  entry->flags&=~CoderDecoderThreadSupportFlag;
+  entry->flags&=~CoderEncoderThreadSupportFlag;
   (void) RegisterMagickInfo(entry);
   return(MagickImageCoderSignature);
 }
@@ -770,19 +767,38 @@ static void SetSaturation(double saturation,FPXColorTwistMatrix *color_twist)
   *color_twist=result;
 }

+static void FPXRelinquishWriteSummaryInfo(FPXSummaryInformation *summary_info)
+{
+  /*
+    Strings stored by the writer are allocated with ImageMagick's allocator, so
+    they must not be released with FPX_DeleteFPXStr().
+  */
+  if (summary_info->title.ptr != (unsigned char *) NULL)
+    summary_info->title.ptr=(unsigned char *) RelinquishMagickMemory(
+      summary_info->title.ptr);
+  summary_info->title.length=0;
+  summary_info->title_valid=MagickFalse;
+  if (summary_info->comments.ptr != (unsigned char *) NULL)
+    summary_info->comments.ptr=(unsigned char *) RelinquishMagickMemory(
+      summary_info->comments.ptr);
+  summary_info->comments.length=0;
+  summary_info->comments_valid=MagickFalse;
+}
+
 static MagickBooleanType WriteFPXImage(const ImageInfo *image_info,Image *image,
   ExceptionInfo *exception)
 {
 #define ThrowFPXException(severity,tag) \
+do \
 { \
   if (quantum_info != (QuantumInfo *) NULL) \
     quantum_info=DestroyQuantumInfo(quantum_info); \
   if (flashpix != (FPXImageHandle *) NULL) \
     (void) FPX_CloseImage(flashpix); \
-  FPXRelinquishSummaryInfo(&summary_info); \
+  FPXRelinquishWriteSummaryInfo(&summary_info); \
   FPX_ClearSystem(); \
   ThrowWriterException(severity,tag); \
-}
+} while (0)

   FPXBackground
     background_color;
@@ -869,6 +885,9 @@ static MagickBooleanType WriteFPXImage(const ImageInfo *image_info,Image *image,
     Initialize FPX toolkit.
   */
   image->depth=8;
+  fpx_status=FPX_InitSystem();
+  if (fpx_status != FPX_OK)
+    ThrowWriterException(DelegateError,"UnableToInitializeFPXLibrary");
   memory_limit=20000000;
   fpx_status=FPX_SetToolkitMemoryLimit(&memory_limit);
   if (fpx_status != FPX_OK)
@@ -897,14 +916,17 @@ static MagickBooleanType WriteFPXImage(const ImageInfo *image_info,Image *image,
     image->rows,tile_width,tile_height,colorspace,background_color,compression,
     &flashpix);
   if (fpx_status != FPX_OK)
-    return(status);
+    {
+      flashpix=(FPXImageHandle *) NULL;
+      ThrowFPXException(DelegateError,"UnableToCreateImageFile");
+    }
   if (compression == JPEG_UNSPECIFIED)
     {
       /*
         Initialize the compression by quality for the entire image.
       */
       fpx_status=FPX_SetJPEGCompression(flashpix,(unsigned short)
-        image->quality == UndefinedCompressionQuality ? 75 : image->quality);
+        (image->quality == UndefinedCompressionQuality ? 75 : image->quality));
       if (fpx_status != FPX_OK)
         ThrowFPXException(DelegateError,"UnableToSetJPEGLevel");
     }
@@ -918,16 +940,14 @@ static MagickBooleanType WriteFPXImage(const ImageInfo *image_info,Image *image,
       /*
         Note image label.
       */
-      summary_info.title_valid=MagickTrue;
       length=strlen(label);
-      summary_info.title.length=length;
-      if (~length >= (MagickPathExtent-1))
-        summary_info.title.ptr=(unsigned char *) AcquireQuantumMemory(
-          length+MagickPathExtent,sizeof(*summary_info.title.ptr));
+      summary_info.title.ptr=(unsigned char *) AcquireQuantumMemory(length+1,
+        sizeof(*summary_info.title.ptr));
       if (summary_info.title.ptr == (unsigned char *) NULL)
         ThrowFPXException(DelegateError,"UnableToSetImageTitle");
-      (void) CopyMagickString((char *) summary_info.title.ptr,label,
-        MagickPathExtent);
+      (void) CopyMagickString((char *) summary_info.title.ptr,label,length+1);
+      summary_info.title.length=length;
+      summary_info.title_valid=MagickTrue;
     }
   comment=GetImageProperty(image,"comment",exception);
   if (comment != (const char *) NULL)
@@ -935,9 +955,9 @@ static MagickBooleanType WriteFPXImage(const ImageInfo *image_info,Image *image,
       /*
         Note image comment.
       */
-      summary_info.comments_valid=MagickTrue;
       summary_info.comments.ptr=(unsigned char *) AcquireString(comment);
       summary_info.comments.length=strlen(comment);
+      summary_info.comments_valid=MagickTrue;
     }
   fpx_status=FPX_SetSummaryInformation(flashpix,&summary_info);
   if (fpx_status != FPX_OK)
@@ -980,15 +1000,27 @@ static MagickBooleanType WriteFPXImage(const ImageInfo *image_info,Image *image,
       break;
     length=ExportQuantumPixels(image,(CacheView *) NULL,quantum_info,
       quantum_type,pixels,exception);
+    if (length == 0)
+      ThrowFPXException(CoderError,"UnableToExportPixels");
     fpx_status=FPX_WriteImageLine(flashpix,&fpx_info);
     if (fpx_status != FPX_OK)
-      break;
+      ThrowFPXException(DelegateError,"UnableToWriteImageData");
     status=SetImageProgress(image,SaveImageTag,(MagickOffsetType) y,
       image->rows);
     if (status == MagickFalse)
       break;
   }
   quantum_info=DestroyQuantumInfo(quantum_info);
+  if (status == MagickFalse)
+    {
+      /*
+        Pixel access failed or the operation was cancelled.
+      */
+      (void) FPX_CloseImage(flashpix);
+      FPXRelinquishWriteSummaryInfo(&summary_info);
+      FPX_ClearSystem();
+      return(MagickFalse);
+    }
   option=GetImageOption(image_info,"fpx:view");
   if (option != (const char *) NULL)
     {
@@ -1113,9 +1145,11 @@ static MagickBooleanType WriteFPXImage(const ImageInfo *image_info,Image *image,
             ThrowFPXException(DelegateError,"UnableToSetRegionOfInterest");
         }
     }
-  (void) FPX_CloseImage(flashpix);
-  FPXRelinquishSummaryInfo(&summary_info);
+  fpx_status=FPX_CloseImage(flashpix);
+  FPXRelinquishWriteSummaryInfo(&summary_info);
   FPX_ClearSystem();
+  if (fpx_status != FPX_OK)
+    ThrowWriterException(DelegateError,"UnableToWriteImageData");
   return(MagickTrue);
 }
 #endif