GraphicsMagick: Fixes based on Clang Analyzer analysis.

GraphicsMagick Commits <[email protected]>
Newsgroups gmane.comp.video.graphicsmagick.cvs
Message-ID <mailman.22305.1669333916.1459.graphicsmagick-commit@lists.sourceforge.net>
changeset ac03e84f4529 in /hg/GraphicsMagick
details: http://hg.GraphicsMagick.org/hg/GraphicsMagick?cmd=changeset;node=ac03e84f4529
summary: Fixes based on Clang Analyzer analysis.

diffstat:

 ChangeLog              |  21 +++++++++++++++++++++
 coders/jpeg.c          |   4 ++--
 coders/png.c           |   1 +
 cscope.files           |   1 -
 magick/image-private.h |   9 +++++----
 magick/profile.c       |   2 +-
 magick/widget.c        |   2 +-
 magick/xwindow.c       |  29 +++++++++++++++++------------
 www/Changelog.html     |  14 ++++++++++++++
 9 files changed, 62 insertions(+), 21 deletions(-)

diffs (234 lines):

diff -r e45d44ad6810 -r ac03e84f4529 ChangeLog
--- a/ChangeLog	Thu Nov 24 09:51:03 2022 -0600
+++ b/ChangeLog	Thu Nov 24 17:51:45 2022 -0600
@@ -1,5 +1,26 @@
 2022-11-24  Bob Friesenhahn  <[email protected]>
 
+	* coders/jpeg.c (WriteJPEGImage): Useful data_precision range is 8
+	to 16.
+
+	* magick/profile.c (ProfileImagePixels): Make sure not to use
+	indexes if it is NULL.
+
+	* magick/xwindow.c (MagickXDitherImage): Eliminate use of
+	undefined behavior.  Make sure to deallocate memory upon error.
+
+	* magick/widget.c (MagickXCommandWidget): Assure that prerequisite
+	'number_selections' is satisfied.
+
+	* magick/xwindow.c (MagickXGetWindowImage): Assure that
+	prereqisite 'colors' table for PseudoClass case is satisifed.
+
+	* coders/png.c (ReadOnePNGImage): Make sure that background index
+	is initialized.
+
+	* magick/profile.c (ProfileImagePixels): Don't de-reference
+	indexes if they were not supplied.
+
 	* magick/widget.c (MagickXColorBrowserWidget): If mode_info.text
 	is NULL, ignore event due to user pushing mode button.
 
diff -r e45d44ad6810 -r ac03e84f4529 coders/jpeg.c
--- a/coders/jpeg.c	Thu Nov 24 09:51:03 2022 -0600
+++ b/coders/jpeg.c	Thu Nov 24 17:51:45 2022 -0600
@@ -2718,7 +2718,7 @@
   jpeg_set_defaults(&jpeg_info);
 
   /*
-    Determine bit depth.
+    Determine bit depth (valid range in 8-16).
   */
   {
     int
@@ -3069,7 +3069,7 @@
       ThrowJPEGWriterException(ResourceLimitError,MemoryAllocationFailed,image);
     }
   scanline[0]=(JSAMPROW) jpeg_pixels;
-  if (jpeg_info.data_precision > 8)
+  if (jpeg_info.data_precision > 8 && jpeg_info.data_precision <= 16)
     {
       unsigned int
         scale_short;
diff -r e45d44ad6810 -r ac03e84f4529 coders/png.c
--- a/coders/png.c	Thu Nov 24 09:51:03 2022 -0600
+++ b/coders/png.c	Thu Nov 24 17:51:45 2022 -0600
@@ -2077,6 +2077,7 @@
                   png_color_16
                     background;
 
+                  background.index=0;
 #ifndef PNG_READ_EMPTY_PLTE_SUPPORTED
                   if (mng_info->have_saved_bkgd_index)
                     background.index=mng_info->saved_bkgd_index;
diff -r e45d44ad6810 -r ac03e84f4529 cscope.files
--- a/cscope.files	Thu Nov 24 09:51:03 2022 -0600
+++ b/cscope.files	Thu Nov 24 17:51:45 2022 -0600
@@ -36,7 +36,6 @@
 coders/info.c
 coders/jbig.c
 coders/jnx.c
-coders/jpx.c
 coders/jp2.c
 coders/jpeg.c
 coders/label.c
diff -r e45d44ad6810 -r ac03e84f4529 magick/image-private.h
--- a/magick/image-private.h	Thu Nov 24 09:51:03 2022 -0600
+++ b/magick/image-private.h	Thu Nov 24 17:51:45 2022 -0600
@@ -16,10 +16,11 @@
 
   Emulates ((1U << bits)-1) but without the overflow problems.
 */
-#define MaxValueGivenBits(bits) ((unsigned long) \
-                                 (((int) bits <= 0) ? 0 :               \
-                                   ((0x01UL << (Min(sizeof(unsigned long)*8U,(size_t)bits)-1)) + \
-                                    ((0x01UL << (Min(sizeof(unsigned long)*8U,(size_t)bits)-1))-1))))
+#define MaxValueGivenBits(bits)                                         \
+  ((unsigned long)                                                      \
+   ((bits <= 0) ? 0 :                                                   \
+    ((0x01UL << (Min(sizeof(unsigned long)*8U,(size_t)bits)-1)) +       \
+     ((0x01UL << (Min(sizeof(unsigned long)*8U,(size_t)bits)-1))-1))))
 
 /*
   ImageExtra allows for expansion of Image without increasing its
diff -r e45d44ad6810 -r ac03e84f4529 magick/profile.c
--- a/magick/profile.c	Thu Nov 24 09:51:03 2022 -0600
+++ b/magick/profile.c	Thu Nov 24 17:51:45 2022 -0600
@@ -551,7 +551,7 @@
           pixels[i].green=ScaleShortToQuantum(beta.green);
           pixels[i].blue=ScaleShortToQuantum(beta.blue);
         }
-      if (image->matte)
+      if ((image->matte) && (NULL != indexes))
         {
           if ((source_colorspace == CMYKColorspace) &&
               (target_colorspace != CMYKColorspace))
diff -r e45d44ad6810 -r ac03e84f4529 magick/widget.c
--- a/magick/widget.c	Thu Nov 24 09:51:03 2022 -0600
+++ b/magick/widget.c	Thu Nov 24 17:51:45 2022 -0600
@@ -3095,7 +3095,7 @@
       default:
         break;
     }
-  if (state & UpdateConfigurationState)
+  if ((state & UpdateConfigurationState) && number_selections)
     {
       /*
         Initialize button information.
diff -r e45d44ad6810 -r ac03e84f4529 magick/xwindow.c
--- a/magick/xwindow.c	Thu Nov 24 09:51:03 2022 -0600
+++ b/magick/xwindow.c	Thu Nov 24 17:51:45 2022 -0600
@@ -2149,10 +2149,11 @@
   PixelPacket
     color;
 
-  int
+  long
+    x,
     y;
 
-  long
+  magick_int32_t
     value;
 
   register char
@@ -2163,13 +2164,12 @@
 
   register int
     i,
-    j,
-    x;
+    j;
 
   unsigned int
     scanline_pad;
 
-  register unsigned long
+  register magick_uint32_t
     pixel;
 
   unsigned char
@@ -2180,6 +2180,9 @@
   /*
     Allocate and initialize dither maps.
   */
+  memset(blue_map,0,sizeof(blue_map));
+  memset(green_map,0,sizeof(green_map));
+  memset(red_map,0,sizeof(red_map));
   for (i=0; i < 2; i++)
     for (j=0; j < 16; j++)
     {
@@ -2193,7 +2196,7 @@
         {
           MagickError3(ResourceLimitError,MemoryAllocationFailed,
             UnableToDitherImage);
-          return;
+          goto done_xditherimage;
         }
     }
   /*
@@ -2218,7 +2221,7 @@
         value=x-32;
         if (x < 112)
           value=x/2+24;
-        value+=(dither_blue[i][j] << 1);
+        value+=((magick_uint32_t) dither_blue[i][j] << 1);
         blue_map[i][j][x]=(unsigned char)
           ((value < 0) ? 0 : (value > 255) ? 255 : value);
       }
@@ -2240,10 +2243,10 @@
       color.red=red_map[i][j][ScaleQuantumToChar(p->red)] << 8;
       color.green=green_map[i][j][ScaleQuantumToChar(p->green)] << 8;
       color.blue=blue_map[i][j][ScaleQuantumToChar(p->blue)] << 8;
-      pixel=(unsigned long) ((color.red & 0xe0) |
-        ((unsigned long) (color.green & 0xe0) >> 3) |
-        ((unsigned long) (color.blue & 0xc0) >> 6));
-      *q++=(unsigned char) pixel;
+      pixel=(magick_uint32_t) ((color.red & 0xe0) |
+        ((magick_uint32_t) (color.green & 0xe0) >> 3) |
+        ((magick_uint32_t) (color.blue & 0xc0) >> 6));
+      *q++=(magick_uint32_t) pixel;
       p++;
       j++;
       if (j == 16)
@@ -2254,6 +2257,7 @@
     if (i == 2)
       i=0;
   }
+ done_xditherimage:
   /*
     Free allocated memory.
   */
@@ -4488,7 +4492,8 @@
             /*
               Create colormap.
             */
-            if (!AllocateImageColormap(composite_image,number_colors))
+            if ((NULL == colors) ||
+                !AllocateImageColormap(composite_image,number_colors))
               {
                 XDestroyImage(ximage);
                 ximage=(XImage *) NULL;
diff -r e45d44ad6810 -r ac03e84f4529 www/Changelog.html
--- a/www/Changelog.html	Thu Nov 24 09:51:03 2022 -0600
+++ b/www/Changelog.html	Thu Nov 24 17:51:45 2022 -0600
@@ -40,6 +40,20 @@
 <p>2022-11-24  Bob Friesenhahn  &lt;<a class="reference external" href="mailto:bfriesen&#37;&#52;&#48;simple&#46;dallas&#46;tx&#46;us">bfriesen<span>&#64;</span>simple<span>&#46;</span>dallas<span>&#46;</span>tx<span>&#46;</span>us</a>&gt;</p>
 <blockquote>
 <ul class="simple">
+<li><p>coders/jpeg.c (WriteJPEGImage): Useful data_precision range is 8
+to 16.</p></li>
+<li><p>magick/profile.c (ProfileImagePixels): Make sure not to use
+indexes if it is NULL.</p></li>
+<li><p>magick/xwindow.c (MagickXDitherImage): Eliminate use of
+undefined behavior.  Make sure to deallocate memory upon error.</p></li>
+<li><p>magick/widget.c (MagickXCommandWidget): Assure that prerequisite
+'number_selections' is satisfied.</p></li>
+<li><p>magick/xwindow.c (MagickXGetWindowImage): Assure that
+prereqisite 'colors' table for PseudoClass case is satisifed.</p></li>
+<li><p>coders/png.c (ReadOnePNGImage): Make sure that background index
+is initialized.</p></li>
+<li><p>magick/profile.c (ProfileImagePixels): Don't de-reference
+indexes if they were not supplied.</p></li>
 <li><p>magick/widget.c (MagickXColorBrowserWidget): If mode_info.text
 is NULL, ignore event due to user pushing mode button.</p></li>
 <li><p>coders/heif.c (ReadMetadata): Also guard against profile name
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.