From b4f8e1a387dd1d0a0af516071831a235f2fdf437 Mon Sep 17 00:00:00 2001
From: Dirk Lemstra <dirk@lemstra.org>
Date: Sun, 25 Jan 2026 18:49:10 +0100
Subject: [PATCH] No longer allow mutations on the first image of the list
 (GHSA-fwqw-2x5x-w566)

---
 coders/msl.c | 150 +++++++++++++++++++++++++--------------------------
 1 file changed, 75 insertions(+), 75 deletions(-)

Index: ImageMagick-7.1.2-0/coders/msl.c
===================================================================
--- ImageMagick-7.1.2-0.orig/coders/msl.c
+++ ImageMagick-7.1.2-0/coders/msl.c
@@ -382,7 +382,7 @@ static void MSLStartElement(void *contex
           /*
             Add noise image.
           */
-          if (msl_info->image[n] == (Image *) NULL)
+          if ((n < 1) || ((n < 1) || (msl_info->image[n] == (Image *) NULL)))
             {
               ThrowMSLException(OptionError,"NoImagesDefined",
                 (const char *) tag);
@@ -458,7 +458,7 @@ static void MSLStartElement(void *contex
           /*
             Annotate image.
           */
-          if (msl_info->image[n] == (Image *) NULL)
+          if ((n < 1) || (msl_info->image[n] == (Image *) NULL))
             {
               ThrowMSLException(OptionError,"NoImagesDefined",
                 (const char *) tag);
@@ -793,7 +793,7 @@ static void MSLStartElement(void *contex
           MagickBooleanType
             stack;
 
-          if (msl_info->image[n] == (Image *) NULL)
+          if ((n < 1) || (msl_info->image[n] == (Image *) NULL))
             {
               ThrowMSLException(OptionError,"NoImagesDefined",
                 (const char *) tag);
@@ -851,7 +851,7 @@ static void MSLStartElement(void *contex
           /*
             Adjusts an image's orientation
           */
-          if (msl_info->image[n] == (Image *) NULL)
+          if ((n < 1) || (msl_info->image[n] == (Image *) NULL))
             {
               ThrowMSLException(OptionError,"NoImagesDefined",
                 (const char *) tag);
@@ -881,7 +881,7 @@ static void MSLStartElement(void *contex
           /*
             Blur image.
           */
-          if (msl_info->image[n] == (Image *) NULL)
+          if ((n < 1) || (msl_info->image[n] == (Image *) NULL))
             {
               ThrowMSLException(OptionError,"NoImagesDefined",
                 (const char *) tag);
@@ -977,7 +977,7 @@ static void MSLStartElement(void *contex
           /*
             Border image.
           */
-          if (msl_info->image[n] == (Image *) NULL)
+          if ((n < 1) || (msl_info->image[n] == (Image *) NULL))
             {
               ThrowMSLException(OptionError,"NoImagesDefined",
                 (const char *) tag);
@@ -1099,7 +1099,7 @@ static void MSLStartElement(void *contex
           /*
             Add noise image.
           */
-          if (msl_info->image[n] == (Image *) NULL)
+          if ((n < 1) || (msl_info->image[n] == (Image *) NULL))
             {
               ThrowMSLException(OptionError,"NoImagesDefined",
                 (const char *) tag);
@@ -1164,7 +1164,7 @@ static void MSLStartElement(void *contex
             radius = 0.0,
             sigma = 1.0;
 
-        if (msl_info->image[n] == (Image *) NULL)
+        if ((n < 1) || (msl_info->image[n] == (Image *) NULL))
         {
           ThrowMSLException(OptionError,"NoImagesDefined",
             (const char *) tag);
@@ -1239,7 +1239,7 @@ static void MSLStartElement(void *contex
           /*
             Chop image.
           */
-          if (msl_info->image[n] == (Image *) NULL)
+          if ((n < 1) || (msl_info->image[n] == (Image *) NULL))
             {
               ThrowMSLException(OptionError,"NoImagesDefined",
                 (const char *) tag);
@@ -1346,7 +1346,7 @@ static void MSLStartElement(void *contex
           /*
             Color floodfill image.
           */
-          if (msl_info->image[n] == (Image *) NULL)
+          if ((n < 1) || (msl_info->image[n] == (Image *) NULL))
             {
               ThrowMSLException(OptionError,"NoImagesDefined",
                 (const char *) tag);
@@ -1478,7 +1478,7 @@ static void MSLStartElement(void *contex
           /*
             Composite image.
           */
-          if (msl_info->image[n] == (Image *) NULL)
+          if ((n < 1) || (msl_info->image[n] == (Image *) NULL))
             {
               ThrowMSLException(OptionError,"NoImagesDefined",
                 (const char *) tag);
@@ -1821,7 +1821,7 @@ static void MSLStartElement(void *contex
           /*
             Contrast image.
           */
-          if (msl_info->image[n] == (Image *) NULL)
+          if ((n < 1) || (msl_info->image[n] == (Image *) NULL))
             {
               ThrowMSLException(OptionError,"NoImagesDefined",
                 (const char *) tag);
@@ -1875,7 +1875,7 @@ static void MSLStartElement(void *contex
           /*
             Crop image.
           */
-          if (msl_info->image[n] == (Image *) NULL)
+          if ((n < 1) || (msl_info->image[n] == (Image *) NULL))
             {
               ThrowMSLException(OptionError,"NoImagesDefined",
                 (const char *) tag);
@@ -1977,7 +1977,7 @@ static void MSLStartElement(void *contex
           /*
             Cycle-colormap image.
           */
-          if (msl_info->image[n] == (Image *) NULL)
+          if ((n < 1) || (msl_info->image[n] == (Image *) NULL))
             {
               ThrowMSLException(OptionError,"NoImagesDefined",
                 (const char *) tag);
@@ -2031,7 +2031,7 @@ static void MSLStartElement(void *contex
           /*
             Despeckle image.
           */
-          if (msl_info->image[n] == (Image *) NULL)
+          if ((n < 1) || (msl_info->image[n] == (Image *) NULL))
             {
               ThrowMSLException(OptionError,"NoImagesDefined",
                 (const char *) tag);
@@ -2057,7 +2057,7 @@ static void MSLStartElement(void *contex
         }
       if (LocaleCompare((const char *) tag,"display") == 0)
         {
-          if (msl_info->image[n] == (Image *) NULL)
+          if ((n < 1) || (msl_info->image[n] == (Image *) NULL))
             {
               ThrowMSLException(OptionError,"NoImagesDefined",
                 (const char *) tag);
@@ -2085,7 +2085,7 @@ static void MSLStartElement(void *contex
           /*
             Annotate image.
           */
-          if (msl_info->image[n] == (Image *) NULL)
+          if ((n < 1) || (msl_info->image[n] == (Image *) NULL))
             {
               ThrowMSLException(OptionError,"NoImagesDefined",
                 (const char *) tag);
@@ -2448,7 +2448,7 @@ static void MSLStartElement(void *contex
           /*
             Edge image.
           */
-          if (msl_info->image[n] == (Image *) NULL)
+          if ((n < 1) || (msl_info->image[n] == (Image *) NULL))
             {
               ThrowMSLException(OptionError,"NoImagesDefined",
                 (const char *) tag);
@@ -2514,7 +2514,7 @@ static void MSLStartElement(void *contex
           /*
             Emboss image.
           */
-          if (msl_info->image[n] == (Image *) NULL)
+          if ((n < 1) || (msl_info->image[n] == (Image *) NULL))
             {
               ThrowMSLException(OptionError,"NoImagesDefined",
                 (const char *) tag);
@@ -2593,7 +2593,7 @@ static void MSLStartElement(void *contex
           /*
             Enhance image.
           */
-          if (msl_info->image[n] == (Image *) NULL)
+          if ((n < 1) || (msl_info->image[n] == (Image *) NULL))
             {
               ThrowMSLException(OptionError,"NoImagesDefined",
                 (const char *) tag);
@@ -2622,7 +2622,7 @@ static void MSLStartElement(void *contex
           /*
             Equalize image.
           */
-          if (msl_info->image[n] == (Image *) NULL)
+          if ((n < 1) || (msl_info->image[n] == (Image *) NULL))
             {
               ThrowMSLException(OptionError,"NoImagesDefined",
                 (const char *) tag);
@@ -2650,7 +2650,7 @@ static void MSLStartElement(void *contex
     {
       if (LocaleCompare((const char *) tag, "flatten") == 0)
       {
-        if (msl_info->image[n] == (Image *) NULL)
+        if ((n < 1) || (msl_info->image[n] == (Image *) NULL))
         {
           ThrowMSLException(OptionError,"NoImagesDefined",
             (const char *) tag);
@@ -2681,7 +2681,7 @@ static void MSLStartElement(void *contex
           /*
             Flip image.
           */
-          if (msl_info->image[n] == (Image *) NULL)
+          if ((n < 1) || (msl_info->image[n] == (Image *) NULL))
             {
               ThrowMSLException(OptionError,"NoImagesDefined",
                 (const char *) tag);
@@ -2713,7 +2713,7 @@ static void MSLStartElement(void *contex
           /*
             Flop image.
           */
-          if (msl_info->image[n] == (Image *) NULL)
+          if ((n < 1) || (msl_info->image[n] == (Image *) NULL))
             {
               ThrowMSLException(OptionError,"NoImagesDefined",
                 (const char *) tag);
@@ -2748,7 +2748,7 @@ static void MSLStartElement(void *contex
           /*
             Frame image.
           */
-          if (msl_info->image[n] == (Image *) NULL)
+          if ((n < 1) || (msl_info->image[n] == (Image *) NULL))
             {
               ThrowMSLException(OptionError,"NoImagesDefined",
                 (const char *) tag);
@@ -2902,7 +2902,7 @@ static void MSLStartElement(void *contex
           /*
             Gamma image.
           */
-          if (msl_info->image[n] == (Image *) NULL)
+          if ((n < 1) || (msl_info->image[n] == (Image *) NULL))
             {
               ThrowMSLException(OptionError,"NoImagesDefined",
                 (const char *) tag);
@@ -2997,7 +2997,7 @@ static void MSLStartElement(void *contex
         }
       else if (LocaleCompare((const char *) tag,"get") == 0)
         {
-          if (msl_info->image[n] == (Image *) NULL)
+          if ((n < 1) || (msl_info->image[n] == (Image *) NULL))
             {
               ThrowMSLException(OptionError,"NoImagesDefined",
                 (const char *) tag);
@@ -3131,7 +3131,7 @@ static void MSLStartElement(void *contex
           /*
             Implode image.
           */
-          if (msl_info->image[n] == (Image *) NULL)
+          if ((n < 1) || (msl_info->image[n] == (Image *) NULL))
             {
               ThrowMSLException(OptionError,"NoImagesDefined",
                 (const char *) tag);
@@ -3203,7 +3203,7 @@ static void MSLStartElement(void *contex
         double
           levelBlack = 0, levelGamma = 1, levelWhite = QuantumRange;
 
-        if (msl_info->image[n] == (Image *) NULL)
+        if ((n < 1) || (msl_info->image[n] == (Image *) NULL))
         {
           ThrowMSLException(OptionError,"NoImagesDefined",
             (const char *) tag);
@@ -3278,7 +3278,7 @@ static void MSLStartElement(void *contex
           /*
             Magnify image.
           */
-          if (msl_info->image[n] == (Image *) NULL)
+          if ((n < 1) || (msl_info->image[n] == (Image *) NULL))
             {
               ThrowMSLException(OptionError,"NoImagesDefined",
                 (const char *) tag);
@@ -3316,7 +3316,7 @@ static void MSLStartElement(void *contex
           /*
             Map image.
           */
-          if (msl_info->image[n] == (Image *) NULL)
+          if ((n < 1) || (msl_info->image[n] == (Image *) NULL))
             {
               ThrowMSLException(OptionError,"NoImagesDefined",
                 (const char *) tag);
@@ -3407,7 +3407,7 @@ static void MSLStartElement(void *contex
             Matte floodfill image.
           */
           opacity=0.0;
-          if (msl_info->image[n] == (Image *) NULL)
+          if ((n < 1) || (msl_info->image[n] == (Image *) NULL))
             {
               ThrowMSLException(OptionError,"NoImagesDefined",
                 (const char *) tag);
@@ -3539,7 +3539,7 @@ static void MSLStartElement(void *contex
           /*
             Median-filter image.
           */
-          if (msl_info->image[n] == (Image *) NULL)
+          if ((n < 1) || (msl_info->image[n] == (Image *) NULL))
             {
               ThrowMSLException(OptionError,"NoImagesDefined",
                 (const char *) tag);
@@ -3607,7 +3607,7 @@ static void MSLStartElement(void *contex
           /*
             Minify image.
           */
-          if (msl_info->image[n] == (Image *) NULL)
+          if ((n < 1) || (msl_info->image[n] == (Image *) NULL))
             {
               ThrowMSLException(OptionError,"NoImagesDefined",
                 (const char *) tag);
@@ -3641,7 +3641,7 @@ static void MSLStartElement(void *contex
           /*
             Modulate image.
           */
-          if (msl_info->image[n] == (Image *) NULL)
+          if ((n < 1) || (msl_info->image[n] == (Image *) NULL))
             {
               ThrowMSLException(OptionError,"NoImagesDefined",
                 (const char *) tag);
@@ -3771,7 +3771,7 @@ static void MSLStartElement(void *contex
           /*
             Negate image.
           */
-          if (msl_info->image[n] == (Image *) NULL)
+          if ((n < 1) || (msl_info->image[n] == (Image *) NULL))
             {
               ThrowMSLException(OptionError,"NoImagesDefined",
                 (const char *) tag);
@@ -3840,7 +3840,7 @@ static void MSLStartElement(void *contex
           /*
             Normalize image.
           */
-          if (msl_info->image[n] == (Image *) NULL)
+          if ((n < 1) || (msl_info->image[n] == (Image *) NULL))
             {
               ThrowMSLException(OptionError,"NoImagesDefined",
                 (const char *) tag);
@@ -3898,7 +3898,7 @@ static void MSLStartElement(void *contex
           /*
             Oil-paint image.
           */
-          if (msl_info->image[n] == (Image *) NULL)
+          if ((n < 1) || (msl_info->image[n] == (Image *) NULL))
             {
               ThrowMSLException(OptionError,"NoImagesDefined",
                 (const char *) tag);
@@ -3966,7 +3966,7 @@ static void MSLStartElement(void *contex
           /*
             Opaque image.
           */
-          if (msl_info->image[n] == (Image *) NULL)
+          if ((n < 1) || (msl_info->image[n] == (Image *) NULL))
             {
               ThrowMSLException(OptionError,"NoImagesDefined",
                 (const char *) tag);
@@ -4076,7 +4076,7 @@ static void MSLStartElement(void *contex
         }
         if (LocaleCompare((const char *) tag, "profile") == 0)
           {
-            if (msl_info->image[n] == (Image *) NULL)
+            if ((n < 1) || (msl_info->image[n] == (Image *) NULL))
               {
                 ThrowMSLException(OptionError,"NoImagesDefined",
                   (const char *) tag);
@@ -4190,7 +4190,7 @@ static void MSLStartElement(void *contex
           /*
             Quantize image.
           */
-          if (msl_info->image[n] == (Image *) NULL)
+          if ((n < 1) || (msl_info->image[n] == (Image *) NULL))
             {
               ThrowMSLException(OptionError,"NoImagesDefined",
                 (const char *) tag);
@@ -4669,7 +4669,7 @@ static void MSLStartElement(void *contex
           /*
             Raise image.
           */
-          if (msl_info->image[n] == (Image *) NULL)
+          if ((n < 1) || (msl_info->image[n] == (Image *) NULL))
             {
               ThrowMSLException(OptionError,"NoImagesDefined",
                 (const char *) tag);
@@ -4807,7 +4807,7 @@ static void MSLStartElement(void *contex
           /*
             Reduce-noise image.
           */
-          if (msl_info->image[n] == (Image *) NULL)
+          if ((n < 1) || (msl_info->image[n] == (Image *) NULL))
             {
               ThrowMSLException(OptionError,"NoImagesDefined",
                 (const char *) tag);
@@ -4869,18 +4869,17 @@ static void MSLStartElement(void *contex
         }
       else if (LocaleCompare((const char *) tag,"repage") == 0)
       {
-        /* init the values */
-        width=msl_info->image[n]->page.width;
-        height=msl_info->image[n]->page.height;
-        x=msl_info->image[n]->page.x;
-        y=msl_info->image[n]->page.y;
-
-        if (msl_info->image[n] == (Image *) NULL)
+        if ((n < 1) || (msl_info->image[n] == (Image *) NULL))
         {
           ThrowMSLException(OptionError,"NoImagesDefined",
             (const char *) tag);
           break;
         }
+        /* init the values */
+        width=msl_info->image[n]->page.width;
+        height=msl_info->image[n]->page.height;
+        x=msl_info->image[n]->page.x;
+        y=msl_info->image[n]->page.y;
         if (attributes == (const xmlChar **) NULL)
           break;
         for (i=0; (attributes[i] != (const xmlChar *) NULL); i++)
@@ -5001,7 +5000,7 @@ static void MSLStartElement(void *contex
         x_resolution,
         y_resolution;
 
-      if (msl_info->image[n] == (Image *) NULL)
+      if ((n < 1) || (msl_info->image[n] == (Image *) NULL))
         {
           ThrowMSLException(OptionError,"NoImagesDefined",
             (const char *) tag);
@@ -5103,7 +5102,7 @@ static void MSLStartElement(void *contex
           /*
             Resize image.
           */
-          if (msl_info->image[n] == (Image *) NULL)
+          if ((n < 1) || (msl_info->image[n] == (Image *) NULL))
             {
               ThrowMSLException(OptionError,"NoImagesDefined",
                 (const char *) tag);
@@ -5198,7 +5197,7 @@ static void MSLStartElement(void *contex
           /*
             Roll image.
           */
-          if (msl_info->image[n] == (Image *) NULL)
+          if ((n < 1) || (msl_info->image[n] == (Image *) NULL))
             {
               ThrowMSLException(OptionError,"NoImagesDefined",
                 (const char *) tag);
@@ -5272,19 +5271,18 @@ static void MSLStartElement(void *contex
         }
       else if (LocaleCompare((const char *) tag,"roll") == 0)
       {
-        /* init the values */
-        width=msl_info->image[n]->columns;
-        height=msl_info->image[n]->rows;
-        x = y = 0;
-
-        if (msl_info->image[n] == (Image *) NULL)
+        if ((n < 1) || (msl_info->image[n] == (Image *) NULL))
         {
           ThrowMSLException(OptionError,"NoImagesDefined",
             (const char *) tag);
           break;
         }
+        /* init the values */
+        width=msl_info->image[n]->columns;
+        height=msl_info->image[n]->rows;
+        x = y = 0;
         if (attributes == (const xmlChar **) NULL)
-        break;
+          break;
         for (i=0; (attributes[i] != (const xmlChar *) NULL); i++)
         {
           keyword=(const char *) attributes[i++];
@@ -5359,7 +5357,7 @@ static void MSLStartElement(void *contex
           /*
             Rotate image.
           */
-          if (msl_info->image[n] == (Image *) NULL)
+          if ((n < 1) || (msl_info->image[n] == (Image *) NULL))
             {
               ThrowMSLException(OptionError,"NoImagesDefined",
                 (const char *) tag);
@@ -5423,7 +5421,7 @@ static void MSLStartElement(void *contex
         /* init the values */
         double  degrees = 0;
 
-        if (msl_info->image[n] == (Image *) NULL)
+        if ((n < 1) || (msl_info->image[n] == (Image *) NULL))
         {
           ThrowMSLException(OptionError,"NoImagesDefined",
             (const char *) tag);
@@ -5489,7 +5487,7 @@ static void MSLStartElement(void *contex
           /*
             Sample image.
           */
-          if (msl_info->image[n] == (Image *) NULL)
+          if ((n < 1) || (msl_info->image[n] == (Image *) NULL))
             {
               ThrowMSLException(OptionError,"NoImagesDefined",
                 (const char *) tag);
@@ -5566,7 +5564,7 @@ static void MSLStartElement(void *contex
           /*
             Scale image.
           */
-          if (msl_info->image[n] == (Image *) NULL)
+          if ((n < 1) || (msl_info->image[n] == (Image *) NULL))
             {
               ThrowMSLException(OptionError,"NoImagesDefined",
                 (const char *) tag);
@@ -5646,7 +5644,7 @@ static void MSLStartElement(void *contex
           /*
             Segment image.
           */
-          if (msl_info->image[n] == (Image *) NULL)
+          if ((n < 1) || (msl_info->image[n] == (Image *) NULL))
             {
               ThrowMSLException(OptionError,"NoImagesDefined",
                 (const char *) tag);
@@ -5730,7 +5728,7 @@ static void MSLStartElement(void *contex
         }
       else if (LocaleCompare((const char *) tag, "set") == 0)
       {
-        if (msl_info->image[n] == (Image *) NULL)
+        if ((n < 1) || (msl_info->image[n] == (Image *) NULL))
         {
           ThrowMSLException(OptionError,"NoImagesDefined",(const char *) tag);
           break;
@@ -5905,7 +5903,7 @@ static void MSLStartElement(void *contex
           /*
             Shade image.
           */
-          if (msl_info->image[n] == (Image *) NULL)
+          if ((n < 1) || (msl_info->image[n] == (Image *) NULL))
             {
               ThrowMSLException(OptionError,"NoImagesDefined",
                 (const char *) tag);
@@ -5996,7 +5994,7 @@ static void MSLStartElement(void *contex
           /*
             Shear image.
           */
-          if (msl_info->image[n] == (Image *) NULL)
+          if ((n < 1) || (msl_info->image[n] == (Image *) NULL))
             {
               ThrowMSLException(OptionError,"NoImagesDefined",
                 (const char *) tag);
@@ -6096,7 +6094,7 @@ static void MSLStartElement(void *contex
             radius = 0.0,
             sigma = 1.0;
 
-        if (msl_info->image[n] == (Image *) NULL)
+        if ((n < 1) || (msl_info->image[n] == (Image *) NULL))
           {
             ThrowMSLException(OptionError,"NoImagesDefined",
               (const char *) tag);
@@ -6169,7 +6167,7 @@ static void MSLStartElement(void *contex
         width = height = 0;
         x = y = 0;
 
-        if (msl_info->image[n] == (Image *) NULL)
+        if ((n < 1) || (msl_info->image[n] == (Image *) NULL))
         {
           ThrowMSLException(OptionError,"NoImagesDefined",
             (const char *) tag);
@@ -6260,7 +6258,7 @@ static void MSLStartElement(void *contex
           /*
             Shear image.
           */
-          if (msl_info->image[n] == (Image *) NULL)
+          if ((n < 1) || (msl_info->image[n] == (Image *) NULL))
             {
               ThrowMSLException(OptionError,"NoImagesDefined",
                 (const char *) tag);
@@ -6349,7 +6347,7 @@ static void MSLStartElement(void *contex
           /*
             Signature image.
           */
-          if (msl_info->image[n] == (Image *) NULL)
+          if ((n < 1) || (msl_info->image[n] == (Image *) NULL))
             {
               ThrowMSLException(OptionError,"NoImagesDefined",
                 (const char *) tag);
@@ -6373,7 +6371,7 @@ static void MSLStartElement(void *contex
           /*
             Solarize image.
           */
-          if (msl_info->image[n] == (Image *) NULL)
+          if ((n < 1) || (msl_info->image[n] == (Image *) NULL))
             {
               ThrowMSLException(OptionError,"NoImagesDefined",
                 (const char *) tag);
@@ -6435,7 +6433,7 @@ static void MSLStartElement(void *contex
           /*
             Spread image.
           */
-          if (msl_info->image[n] == (Image *) NULL)
+          if ((n < 1) || (msl_info->image[n] == (Image *) NULL))
             {
               ThrowMSLException(OptionError,"NoImagesDefined",
                 (const char *) tag);
@@ -6500,7 +6498,7 @@ static void MSLStartElement(void *contex
         Image *
           watermark = (Image*) NULL;
 
-        if (msl_info->image[n] == (Image *) NULL)
+        if ((n < 1) || (msl_info->image[n] == (Image *) NULL))
           {
             ThrowMSLException(OptionError,"NoImagesDefined",
               (const char *) tag);
@@ -6568,7 +6566,7 @@ static void MSLStartElement(void *contex
         Image *
           stereoImage = (Image*) NULL;
 
-        if (msl_info->image[n] == (Image *) NULL)
+        if ((n < 1) || (msl_info->image[n] == (Image *) NULL))
           {
             ThrowMSLException(OptionError,"NoImagesDefined",(const char *) tag);
             break;
@@ -6635,7 +6633,7 @@ static void MSLStartElement(void *contex
           /*
             Strip image.
           */
-          if (msl_info->image[n] == (Image *) NULL)
+          if ((n < 1) || (msl_info->image[n] == (Image *) NULL))
             {
               ThrowMSLException(OptionError,"NoImagesDefined",
                 (const char *) tag);
@@ -6665,7 +6663,7 @@ static void MSLStartElement(void *contex
             index,
             swap_index;
 
-          if (msl_info->image[n] == (Image *) NULL)
+          if ((n < 1) || (msl_info->image[n] == (Image *) NULL))
             {
               ThrowMSLException(OptionError,"NoImagesDefined",
                 (const char *) tag);
@@ -6731,7 +6729,7 @@ static void MSLStartElement(void *contex
           /*
             Swirl image.
           */
-          if (msl_info->image[n] == (Image *) NULL)
+          if ((n < 1) || (msl_info->image[n] == (Image *) NULL))
             {
               ThrowMSLException(OptionError,"NoImagesDefined",
                 (const char *) tag);
@@ -6795,7 +6793,7 @@ static void MSLStartElement(void *contex
           /*
             Sync image.
           */
-          if (msl_info->image[n] == (Image *) NULL)
+          if ((n < 1) || (msl_info->image[n] == (Image *) NULL))
             {
               ThrowMSLException(OptionError,"NoImagesDefined",
                 (const char *) tag);
@@ -6828,7 +6826,7 @@ static void MSLStartElement(void *contex
           /*
             Texture image.
           */
-          if (msl_info->image[n] == (Image *) NULL)
+          if ((n < 1) || (msl_info->image[n] == (Image *) NULL))
             {
               ThrowMSLException(OptionError,"NoImagesDefined",
                 (const char *) tag);
@@ -6883,7 +6881,7 @@ static void MSLStartElement(void *contex
         /* init the values */
         double  threshold = 0;
 
-        if (msl_info->image[n] == (Image *) NULL)
+        if ((n < 1) || (msl_info->image[n] == (Image *) NULL))
           {
             ThrowMSLException(OptionError,"NoImagesDefined",(const char *) tag);
             break;
@@ -6928,7 +6926,7 @@ static void MSLStartElement(void *contex
       }
       else if (LocaleCompare((const char *) tag, "transparent") == 0)
       {
-        if (msl_info->image[n] == (Image *) NULL)
+        if ((n < 1) || (msl_info->image[n] == (Image *) NULL))
           {
             ThrowMSLException(OptionError,"NoImagesDefined",(const char *) tag);
             break;
@@ -6972,7 +6970,7 @@ static void MSLStartElement(void *contex
       }
       else if (LocaleCompare((const char *) tag, "trim") == 0)
       {
-        if (msl_info->image[n] == (Image *) NULL)
+        if ((n < 1) || (msl_info->image[n] == (Image *) NULL))
           {
             ThrowMSLException(OptionError,"NoImagesDefined",(const char *) tag);
             break;
@@ -7007,7 +7005,7 @@ static void MSLStartElement(void *contex
     {
       if (LocaleCompare((const char *) tag,"write") == 0)
         {
-          if (msl_info->image[n] == (Image *) NULL)
+          if ((n < 1) || (msl_info->image[n] == (Image *) NULL))
             {
               ThrowMSLException(OptionError,"NoImagesDefined",
                 (const char *) tag);
@@ -7103,7 +7101,7 @@ static void MSLEndElement(void *context,
     {
       if (LocaleCompare((const char *) tag,"comment") == 0 )
         {
-          if (msl_info->image[n] == (Image *) NULL)
+          if ((n < 1) || (msl_info->image[n] == (Image *) NULL))
             {
               ThrowMSLException(OptionError,"NoImagesDefined",
                 (const char *) tag);
@@ -7158,7 +7156,7 @@ static void MSLEndElement(void *context,
     {
       if (LocaleCompare((const char *) tag,"label") == 0 )
         {
-          if (msl_info->image[n] == (Image *) NULL)
+          if ((n < 1) || (msl_info->image[n] == (Image *) NULL))
             {
               ThrowMSLException(OptionError,"NoImagesDefined",
                 (const char *) tag);
