GraphicsMagick: PNG: Use a macro to report exceptions rather tha...

GraphicsMagick Commits <[email protected]>
Newsgroups gmane.comp.video.graphicsmagick.cvs
Message-ID <mailman.29786.1670710218.1459.graphicsmagick-commit@lists.sourceforge.net>
changeset f6d9d7d268c6 in /hg/GraphicsMagick
details: http://hg.GraphicsMagick.org/hg/GraphicsMagick?cmd=changeset;node=f6d9d7d268c6
summary: PNG: Use a macro to report exceptions rather than calling libpng's png_error()

diffstat:

 ChangeLog          |   9 ++++++++
 coders/png.c       |  56 ++++++++++++++++++++++++++++++++++++++++++-----------
 magick/widget.c    |   2 +-
 www/Changelog.html |   7 ++++++
 4 files changed, 61 insertions(+), 13 deletions(-)

diffs (148 lines):

diff -r 8317983b3b31 -r f6d9d7d268c6 ChangeLog
--- a/ChangeLog	Sat Dec 10 11:03:37 2022 -0600
+++ b/ChangeLog	Sat Dec 10 16:10:03 2022 -0600
@@ -1,5 +1,14 @@
 2022-12-10  Bob Friesenhahn  <[email protected]>
 
+	* coders/png.c (png_error): Use a macro to report exceptions
+	(rather than calling libpng's png_error()) for exceptions thrown
+	from this module. This provides useful file/line information as
+	well as allowing code analyzers to have a more accurate idea of
+	what is going on.
+
+	* magick/widget.c (MagickXFileBrowserWidget): Fix Coverity 382252
+	"Wrong sizeof argument (SIZEOF_MISMATCH)".
+
 	* coders/meta.c (parse8BIM): Fix Coverity 382245 "Incorrect
 	expression (SIZEOF_MISMATCH)".
 	(parse8BIMW): Fix Coverity 382250 "Incorrect expression
diff -r 8317983b3b31 -r f6d9d7d268c6 coders/png.c
--- a/coders/png.c	Sat Dec 10 11:03:37 2022 -0600
+++ b/coders/png.c	Sat Dec 10 16:10:03 2022 -0600
@@ -679,6 +679,28 @@
 }
 #endif
 
+/*
+  Use a macro to report exceptions (rather than calling libpng's
+  png_error()) for exceptions thrown from this module.
+
+  This provides useful file/line information as well as allowing code
+  analyzers to have a more accurate idea of what is going on.
+*/
+#undef png_error
+#define png_error(png_ptr,error_message)                              \
+  do                                                                  \
+    {                                                                 \
+      Image *                                                         \
+        _image;                                                       \
+                                                                      \
+      _image=(Image *) png_get_error_ptr(png_ptr);                    \
+      (void) LogMagickEvent(CoderEvent,GetMagickModule(),             \
+                            "  error: %.1024s", error_message);       \
+      (void) ThrowException2(&_image->exception,CoderError,           \
+                             error_message,_image->filename);         \
+      longjmp(png_jmpbuf(png_ptr),1);                                 \
+    } while(0)
+
 #if PNG_LIBPNG_VER > 10011
 /*
 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
@@ -1784,6 +1806,25 @@
 
                       &ping_filter_method);
 
+#if (defined(__clang_analyzer__) || defined(__COVERITY__))
+  /*
+    png_get_IHDR() should already have thrown an exception but
+    Coverity and Clang Analyzer can not see that since it is library
+    code.
+  */
+  if (ping_bit_depth != 1 &&
+      ping_bit_depth != 2 &&
+      ping_bit_depth != 4 &&
+      ping_bit_depth != 8 &&
+      ping_bit_depth != 16)
+    {
+      (void) LogMagickEvent(CoderEvent,GetMagickModule(),
+                            "  libpng-%.1024s error: %.1024s",
+                            PNG_LIBPNG_VER_STRING, "Bit depth is not valid");
+      longjmp(png_jmpbuf(ping),1);
+    }
+#endif /* #if (defined(__clang_analyzer__)  || defined(__COVERITY__)) */
+
 #if (QuantumDepth == 8)
   if (ping_bit_depth > 8)
   {
@@ -1851,15 +1892,6 @@
     png_error(ping, "Number of pixels exceeds resource limit");
   }
 
-  if (ping_bit_depth != 1 &&
-      ping_bit_depth != 2 &&
-      ping_bit_depth != 4 &&
-      ping_bit_depth != 8 &&
-      ping_bit_depth != 16)
-    {
-      png_error(ping, "Bit depth is not valid");
-    }
-
   if (ping_bit_depth < 8)
     {
        png_set_packing(ping);
@@ -2063,7 +2095,7 @@
                     if (mng_info->global_trns_length >
                         mng_info->global_plte_length)
                        png_error(ping, "global tRNS has more entries"
-                                             " than global PLTE");
+                                 " than global PLTE");
                     png_set_tRNS(ping,ping_info,mng_info->global_trns,
                                  (int) mng_info->global_trns_length,NULL);
                   }
@@ -2202,7 +2234,7 @@
 #endif
             transparent_color.opacity=(unsigned long) (
                 ping_trans_color->gray *
-                (65535L/MaxValueGivenBits(ping_file_depth)));
+                (65535L/MaxValueGivenBits(ping_file_depth))); /* Coverity 381876 Division or modulo by zero (DIVIDE_BY_ZERO) */
 
 #if (QuantumDepth == 8)
           else
@@ -2261,7 +2293,7 @@
        (ping_colortype == PNG_COLOR_TYPE_GRAY)))
     {
       image->storage_class=PseudoClass;
-      image->colors=1U << ping_file_depth;
+      image->colors=1U << ping_file_depth; /* Coverity (#1-3 of 3): Bad bit shift operation (BAD_SHIFT) */
 #if (QuantumDepth == 8)
       if (image->colors > 256)
         image->colors=256;
diff -r 8317983b3b31 -r f6d9d7d268c6 magick/widget.c
--- a/magick/widget.c	Sat Dec 10 11:03:37 2022 -0600
+++ b/magick/widget.c	Sat Dec 10 16:10:03 2022 -0600
@@ -4945,7 +4945,7 @@
             if (event.xbutton.window == windows->widget.id)
               if (MatteIsActive(special_info,event.xbutton))
                 {
-                  (void) strlcpy(reply_info.text,"x:",sizeof(reply_info.text));
+                  (void) strlcpy(reply_info.text,"x:",MaxTextExtent);
                   state|=ExitState;
                 }
             special_info.raised=True;
diff -r 8317983b3b31 -r f6d9d7d268c6 www/Changelog.html
--- a/www/Changelog.html	Sat Dec 10 11:03:37 2022 -0600
+++ b/www/Changelog.html	Sat Dec 10 16:10:03 2022 -0600
@@ -40,6 +40,13 @@
 <p>2022-12-10  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 (png_error): Use a macro to report exceptions
+(rather than calling libpng's png_error()) for exceptions thrown
+from this module. This provides useful file/line information as
+well as allowing code analyzers to have a more accurate idea of
+what is going on.</p></li>
+<li><p>magick/widget.c (MagickXFileBrowserWidget): Fix Coverity 382252
+&quot;Wrong sizeof argument (SIZEOF_MISMATCH)&quot;.</p></li>
 <li><p>coders/meta.c (parse8BIM): Fix Coverity 382245 &quot;Incorrect
 expression (SIZEOF_MISMATCH)&quot;.
 (parse8BIMW): Fix Coverity 382250 &quot;Incorrect expression
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.