Commit e474066ef for imagemagick.org

commit e474066eff38fc63bd97e0f9650cc7ec90a96ae0
Author: Cristy <urban-warrior@imagemagick.org>
Date:   Fri Dec 4 18:46:55 2026 -0500

    ReadSFWImage now fcloses the temporary JPEG stream after ReadImage

diff --git a/coders/sfw.c b/coders/sfw.c
index 2f2992afb..6b32937c7 100644
--- a/coders/sfw.c
+++ b/coders/sfw.c
@@ -272,7 +272,7 @@ static Image *ReadSFWImage(const ImageInfo *image_info,ExceptionInfo *exception)
   header=SFWScan(buffer,buffer+count-1,(const unsigned char *)
     "\377\310\377\320",4);
   if ((header == (unsigned char *) NULL) ||
-      ((header+140) > (buffer+GetBlobSize(image))))
+      ((header+140) > (buffer+count)))
     {
       buffer=(unsigned char *) RelinquishMagickMemory(buffer);
       ThrowReaderException(CorruptImageError,"ImproperImageHeader");
@@ -290,7 +290,7 @@ static Image *ReadSFWImage(const ImageInfo *image_info,ExceptionInfo *exception)
     if ((offset+4) > (buffer+count-1))
       {
         buffer=(unsigned char *) RelinquishMagickMemory(buffer);
-        ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
+        ThrowReaderException(CorruptImageError,"ImproperImageHeader");
       }
     TranslateSFWMarker(offset);
     if (offset[1] == 0xda)
@@ -330,8 +330,8 @@ static Image *ReadSFWImage(const ImageInfo *image_info,ExceptionInfo *exception)
   extent=fwrite(HuffmanTable,1,sizeof(HuffmanTable)/sizeof(*HuffmanTable),file);
   extent=fwrite(offset+1,1,(size_t) (data-offset),file);
   status=ferror(file) != 0 ? MagickFalse : MagickTrue;
-  if (fseek(file,0,SEEK_SET) != 0)
-    ThrowReaderException(FileOpenError,"UnableToCreateTemporaryFile");
+  if ((fflush(file) != 0) || (fseek(file,0,SEEK_SET) != 0))
+    status=MagickFalse;
   (void) close_utf8(unique_file);
   buffer=(unsigned char *) RelinquishMagickMemory(buffer);
   if (status == MagickFalse)
@@ -355,6 +355,12 @@ static Image *ReadSFWImage(const ImageInfo *image_info,ExceptionInfo *exception)
   (void) CopyMagickString(read_info->magick,"JPEG",MagickPathExtent);
   read_info->file=file;
   jpeg_image=ReadImage(read_info,exception);
+  /*
+    ReadImage() does not close a caller-supplied FILE; close it here so the
+    stream is not leaked and the temporary file can actually be removed.
+  */
+  read_info->file=(FILE *) NULL;
+  (void) fclose(file);
   (void) RelinquishUniqueFileResource(read_info->filename);
   read_info=DestroyImageInfo(read_info);
   if (jpeg_image == (Image *) NULL)