GraphicsMagick: ReadOnePNGImage(): More work on Coverity issue 3...

GraphicsMagick Commits <[email protected]>
Newsgroups gmane.comp.video.graphicsmagick.cvs
Message-ID <mailman.14704.1667952488.1459.graphicsmagick-commit@lists.sourceforge.net>
changeset e492d0c4773a in /hg/GraphicsMagick
details: http://hg.GraphicsMagick.org/hg/GraphicsMagick?cmd=changeset;node=e492d0c4773a
summary: ReadOnePNGImage(): More work on Coverity issue 381864.

diffstat:

 ChangeLog              |   4 ++++
 coders/png.c           |  12 ++++++------
 magick/image-private.h |  15 ++++++++++++++-
 magick/image.h         |  11 -----------
 www/Changelog.html     |   2 ++
 5 files changed, 26 insertions(+), 18 deletions(-)

diffs (123 lines):

diff -r 819c2c57d38c -r e492d0c4773a ChangeLog
--- a/ChangeLog	Tue Nov 08 16:54:45 2022 -0600
+++ b/ChangeLog	Tue Nov 08 18:07:54 2022 -0600
@@ -1,5 +1,9 @@
 2022-11-08  Bob Friesenhahn  <[email protected]>
 
+	* coders/png.c (ReadOnePNGImage): More work on Coverity issue 381864.
+
+	* magick/image-private.h (MaxValueGivenBits): Move to private header.
+
 	* coders/xpm.c (ReadXPMImage): More work on Coverity issue 381862
 	"(DEADCODE)".
 
diff -r 819c2c57d38c -r e492d0c4773a coders/png.c
--- a/coders/png.c	Tue Nov 08 16:54:45 2022 -0600
+++ b/coders/png.c	Tue Nov 08 18:07:54 2022 -0600
@@ -2159,7 +2159,7 @@
 
   if (png_get_valid(ping, ping_info, PNG_INFO_tRNS) &&
       (ping_trans_color != (png_color_16p) NULL) &&
-      (unsigned int) ping_file_depth <= MaxColormapSize)
+      (((unsigned int) ping_file_depth) <= MaxColormapSize))
     {
       unsigned int
         bit_mask;
@@ -2168,7 +2168,7 @@
         (void) LogMagickEvent(CoderEvent,GetMagickModule(),
           "    Reading PNG tRNS chunk.");
 
-      bit_mask = (1U << ping_file_depth) - 1;
+      bit_mask = MaxValueGivenBits(ping_file_depth);
 
       /*
         Image has a transparent background.
@@ -2190,13 +2190,13 @@
 #endif
             transparent_color.opacity=(unsigned long) (
                 ping_trans_color->gray *
-                (65535L/((1UL << ping_file_depth)-1)));
+                (65535L/MaxValueGivenBits(ping_file_depth)));
 
 #if (QuantumDepth == 8)
           else
             transparent_color.opacity=(unsigned long) (
-                (ping_trans_color->gray * 65535L)/
-                ((1UL << ping_file_depth)-1));
+                (ping_trans_color->gray * 65535UL)/
+                MaxValueGivenBits(ping_file_depth));
 #endif
           if (logging != MagickFalse)
             {
@@ -2302,7 +2302,7 @@
           unsigned long
             scale;
 
-          scale=(MaxRGB/((1U << ping_file_depth)-1));
+          scale=(MaxRGB/MaxValueGivenBits(ping_file_depth));
           if (scale < 1)
             scale=1;
           for (i=0; i < (long) image->colors; i++)
diff -r 819c2c57d38c -r e492d0c4773a magick/image-private.h
--- a/magick/image-private.h	Tue Nov 08 16:54:45 2022 -0600
+++ b/magick/image-private.h	Tue Nov 08 18:07:54 2022 -0600
@@ -1,5 +1,5 @@
 /*
-  Copyright (C) 2018 - 2020 GraphicsMagick Group
+  Copyright (C) 2018 - 2022 GraphicsMagick Group
 
   This program is covered by multiple licenses, which are described in
   Copyright.txt. You should have received a copy of Copyright.txt with this
@@ -9,6 +9,19 @@
 */
 
 /*
+  Maximum unsigned RGB value which fits in the specified bits
+
+  If bits <= 0, then zero is returned.  If bits exceeds bits in unsigned long,
+  then max value of unsigned long is returned.
+
+  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))))
+
+/*
   ImageExtra allows for expansion of Image without increasing its
   size.  The internals are defined only in this private header file.
   Clients using the library can access the internals via the access
diff -r 819c2c57d38c -r e492d0c4773a magick/image.h
--- a/magick/image.h	Tue Nov 08 16:54:45 2022 -0600
+++ b/magick/image.h	Tue Nov 08 18:07:54 2022 -0600
@@ -31,17 +31,6 @@
 #  define QuantumDepth  16
 #endif
 
-/*
-  Maximum unsigned RGB value which fits in the specified bits
-
-  If bits <= 0, then zero is returned.  If bits exceeds bits in unsigned long,
-  then max value of unsigned long is returned.
-*/
-#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))))
-
 #if (QuantumDepth == 8)
 #  define MaxColormapSize  256U
 #  define MaxMap  255U
diff -r 819c2c57d38c -r e492d0c4773a www/Changelog.html
--- a/www/Changelog.html	Tue Nov 08 16:54:45 2022 -0600
+++ b/www/Changelog.html	Tue Nov 08 18:07:54 2022 -0600
@@ -40,6 +40,8 @@
 <p>2022-11-08  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/png.c (ReadOnePNGImage): More work on Coverity issue 381864.</p></li>
+<li><p>magick/image-private.h (MaxValueGivenBits): Move to private header.</p></li>
 <li><p>coders/xpm.c (ReadXPMImage): More work on Coverity issue 381862
 &quot;(DEADCODE)&quot;.</p></li>
 <li><p>magick/operator.c (QuantumLogCB): Fix Coverity issue 381861
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.