Commit b56258d57 for imagemagick.org

commit b56258d57ef16f85241832dd2206d1906846ea96
Author: Cristy <urban-warrior@imagemagick.org>
Date:   Sun Nov 29 09:27:48 2026 -0500

    clamp the image resolution to a max of 65535

diff --git a/coders/pcx.c b/coders/pcx.c
index 7526547d1..7197aa378 100644
--- a/coders/pcx.c
+++ b/coders/pcx.c
@@ -65,6 +65,11 @@
 #include "MagickCore/string_.h"
 #include "MagickCore/module.h"

+/*
+  Define declarations.
+*/
+#define MaxNumberScenes  1024
+
 /*
   Typedef declarations.
 */
@@ -103,6 +108,15 @@ typedef struct _PCXInfo
 */
 static MagickBooleanType
   WritePCXImage(const ImageInfo *,Image *,ExceptionInfo *);
+
+static inline unsigned short PCXClampResolution(const double value)
+{
+  if ((value != value) || (value <= 0.0))
+    return(0);
+  if (value >= 65535.0)
+    return(65535);
+  return((unsigned short) (value+0.5));
+}

 /*
 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
@@ -202,7 +216,6 @@ static MagickBooleanType IsPCX(const unsigned char *magick,const size_t length)
 */
 static Image *ReadPCXImage(const ImageInfo *image_info,ExceptionInfo *exception)
 {
-#define MaxNumberScenes  1024
 #define ThrowPCXException(severity,tag) \
 { \
   if (scanline != (unsigned char *) NULL) \
@@ -300,6 +313,10 @@ static Image *ReadPCXImage(const ImageInfo *image_info,ExceptionInfo *exception)
         if (page_table[id] == 0)
           break;
       }
+      if (EOFBlob(image) != MagickFalse)
+        ThrowPCXException(CorruptImageError,"UnexpectedEndOfFile");
+      if (page_table[0] == 0)
+        ThrowPCXException(CorruptImageError,"ImproperImageHeader");
     }
   if (page_table != (MagickOffsetType *) NULL)
     {
@@ -351,7 +368,7 @@ static Image *ReadPCXImage(const ImageInfo *image_info,ExceptionInfo *exception)
     if ((image_info->ping != MagickFalse) && (image_info->number_scenes != 0))
       if (image->scene >= (image_info->scene+image_info->number_scenes-1))
         break;
-    if ((MagickSizeType) (image->columns*image->rows/255) > GetBlobSize(image))
+    if (((MagickSizeType) image->columns*image->rows/255) > GetBlobSize(image))
       ThrowPCXException(CorruptImageError,"InsufficientImageDataInFile");
     status=SetImageExtent(image,image->columns,image->rows,exception);
     if (status == MagickFalse)
@@ -367,6 +384,9 @@ static Image *ReadPCXImage(const ImageInfo *image_info,ExceptionInfo *exception)
       ThrowPCXException(CorruptImageError,"ImproperImageHeader");
     if (pcx_info.planes > 6)
       ThrowPCXException(CorruptImageError,"ImproperImageHeader");
+    if ((pcx_info.bits_per_pixel == 8) && (pcx_info.planes != 1) &&
+        ((pcx_info.planes < 3) || (pcx_info.planes > 4)))
+      ThrowPCXException(CorruptImageError,"ImproperImageHeader");
     if ((pcx_info.bits_per_pixel*pcx_info.planes) >= 64)
       ThrowPCXException(CorruptImageError,"ImproperImageHeader");
     one=1;
@@ -484,30 +504,7 @@ static Image *ReadPCXImage(const ImageInfo *image_info,ExceptionInfo *exception)
           r=scanline+i;
           for (x=0; x < (ssize_t) pcx_info.bytes_per_line; x++)
           {
-            switch (i)
-            {
-              case 0:
-              {
-                *r=(*p++);
-                break;
-              }
-              case 1:
-              {
-                *r=(*p++);
-                break;
-              }
-              case 2:
-              {
-                *r=(*p++);
-                break;
-              }
-              case 3:
-              default:
-              {
-                *r=(*p++);
-                break;
-              }
-            }
+            *r=(*p++);
             r+=(ptrdiff_t) pcx_info.planes;
           }
         }
@@ -649,6 +646,9 @@ static Image *ReadPCXImage(const ImageInfo *image_info,ExceptionInfo *exception)
                   */
                   pcx_info.colormap_signature=(unsigned char) ReadBlobByte(image);
                   count=ReadBlob(image,3*image->colors,pcx_colormap);
+                  if (count != (ssize_t) (3*image->colors))
+                    ThrowPCXException(CorruptImageError,
+                      "UnexpectedEndOfFile");
                   p=pcx_colormap;
                   for (i=0; i < (ssize_t) image->colors; i++)
                   {
@@ -972,14 +972,16 @@ static MagickBooleanType WritePCXImage(const ImageInfo *image_info,Image *image,
       case PixelsPerInchResolution:
       default:
       {
-        pcx_info.horizontal_resolution=CastDoubleToUShort(image->resolution.x);
-        pcx_info.vertical_resolution=CastDoubleToUShort(image->resolution.y);
+        pcx_info.horizontal_resolution=PCXClampResolution(image->resolution.x);
+        pcx_info.vertical_resolution=PCXClampResolution(image->resolution.y);
         break;
       }
       case PixelsPerCentimeterResolution:
       {
-        pcx_info.horizontal_resolution=CastDoubleToUShort(2.54*image->resolution.x+0.5);
-        pcx_info.vertical_resolution=CastDoubleToUShort(2.54*image->resolution.y+0.5);
+        pcx_info.horizontal_resolution=PCXClampResolution(
+          2.54*image->resolution.x);
+        pcx_info.vertical_resolution=PCXClampResolution(
+          2.54*image->resolution.y);
         break;
       }
     }
@@ -1043,7 +1045,8 @@ static MagickBooleanType WritePCXImage(const ImageInfo *image_info,Image *image,
     (void) WriteBlobByte(image,pcx_info.planes);
     (void) WriteBlobLSBShort(image,pcx_info.bytes_per_line);
     (void) WriteBlobLSBShort(image,pcx_info.palette_info);
-    (void) WriteBlobPadding(image,58,'\0');
+    for (i=0; i < 58; i++)
+      (void) WriteBlobByte(image,'\0');
     length=(size_t) pcx_info.bytes_per_line;
     pixel_info=AcquireVirtualMemory(length,pcx_info.planes*sizeof(*pixels));
     if (pixel_info == (MemoryInfo *) NULL)
@@ -1063,19 +1066,26 @@ static MagickBooleanType WritePCXImage(const ImageInfo *image_info,Image *image,
         for (y=0; y < (ssize_t) image->rows; y++)
         {
           q=pixels;
+          p=GetVirtualPixels(image,0,y,image->columns,1,exception);
+          if (p == (const Quantum *) NULL)
+            {
+              status=MagickFalse;
+              break;
+            }
           for (i=0; i < pcx_info.planes; i++)
           {
-            p=GetVirtualPixels(image,0,y,image->columns,1,exception);
-            if (p == (const Quantum *) NULL)
-              break;
+            const Quantum
+              *r;
+
+            r=p;
             switch ((int) i)
             {
               case 0:
               {
                 for (x=0; x < (ssize_t) pcx_info.bytes_per_line; x++)
                 {
-                  *q++=ScaleQuantumToChar(GetPixelRed(image,p));
-                  p+=(ptrdiff_t) GetPixelChannels(image);
+                  *q++=ScaleQuantumToChar(GetPixelRed(image,r));
+                  r+=(ptrdiff_t) GetPixelChannels(image);
                 }
                 break;
               }
@@ -1083,8 +1093,8 @@ static MagickBooleanType WritePCXImage(const ImageInfo *image_info,Image *image,
               {
                 for (x=0; x < (ssize_t) pcx_info.bytes_per_line; x++)
                 {
-                  *q++=ScaleQuantumToChar(GetPixelGreen(image,p));
-                  p+=(ptrdiff_t) GetPixelChannels(image);
+                  *q++=ScaleQuantumToChar(GetPixelGreen(image,r));
+                  r+=(ptrdiff_t) GetPixelChannels(image);
                 }
                 break;
               }
@@ -1092,8 +1102,8 @@ static MagickBooleanType WritePCXImage(const ImageInfo *image_info,Image *image,
               {
                 for (x=0; x < (ssize_t) pcx_info.bytes_per_line; x++)
                 {
-                  *q++=ScaleQuantumToChar(GetPixelBlue(image,p));
-                  p+=(ptrdiff_t) GetPixelChannels(image);
+                  *q++=ScaleQuantumToChar(GetPixelBlue(image,r));
+                  r+=(ptrdiff_t) GetPixelChannels(image);
                 }
                 break;
               }
@@ -1102,8 +1112,8 @@ static MagickBooleanType WritePCXImage(const ImageInfo *image_info,Image *image,
               {
                 for (x=(ssize_t) pcx_info.bytes_per_line; x != 0; x--)
                 {
-                  *q++=ScaleQuantumToChar((Quantum) (GetPixelAlpha(image,p)));
-                  p+=(ptrdiff_t) GetPixelChannels(image);
+                  *q++=ScaleQuantumToChar((Quantum) (GetPixelAlpha(image,r)));
+                  r+=(ptrdiff_t) GetPixelChannels(image);
                 }
                 break;
               }
@@ -1130,7 +1140,10 @@ static MagickBooleanType WritePCXImage(const ImageInfo *image_info,Image *image,
           {
             p=GetVirtualPixels(image,0,y,image->columns,1,exception);
             if (p == (const Quantum *) NULL)
-              break;
+              {
+                status=MagickFalse;
+                break;
+              }
             q=pixels;
             for (x=0; x < (ssize_t) image->columns; x++)
             {
@@ -1163,7 +1176,10 @@ static MagickBooleanType WritePCXImage(const ImageInfo *image_info,Image *image,
             {
               p=GetVirtualPixels(image,0,y,image->columns,1,exception);
               if (p == (const Quantum *) NULL)
-                break;
+                {
+                  status=MagickFalse;
+                  break;
+                }
               for (i=0; i < (ssize_t) pcx_info.planes; i++)
               {
                 r=p;
@@ -1214,7 +1230,10 @@ static MagickBooleanType WritePCXImage(const ImageInfo *image_info,Image *image,
             {
               p=GetVirtualPixels(image,0,y,image->columns,1,exception);
               if (p == (const Quantum *) NULL)
-                break;
+                {
+                  status=MagickFalse;
+                  break;
+                }
               bit=0;
               byte=0;
               q=pixels;