[devel] Changes to remove gcc warnings (John Bowler)

Certain optional gcc warning flags resulted in warnings in libpng code.
With these changes only -Wconversion and -Wcast-qual cannot be turned on.
Changes are trivial rearrangements of code.  -Wconversion is not possible
for pngrutil.c (because of the widespread use of += et al on variables
smaller than (int) or (unsigned int)) and -Wcast-qual is not possible
with pngwio.c and pngwutil.c because the 'write' callback and zlib
compression both fail to declare their input buffers with 'const'.
diff --git a/ANNOUNCE b/ANNOUNCE
index b98b196..4ad0e31 100644
--- a/ANNOUNCE
+++ b/ANNOUNCE
@@ -442,6 +442,15 @@
   Fixed atomicity of chunk header serialization (Cosmin)
   Added test for io_state in pngtest.c (Cosmin)
   Added "#!/bin/sh" at the top of contrib/pngminim/*/gather.sh scripts.
+  Changes to remove gcc warnings (John Bowler)
+    Certain optional gcc warning flags resulted in warnings in libpng code.
+    With these changes only -Wconversion and -Wcast-qual cannot be turned on.
+    Changes are trivial rearrangements of code.  -Wconversion is not possible
+    for pngrutil.c (because of the widespread use of += et al on variables
+    smaller than (int) or (unsigned int)) and -Wcast-qual is not possible
+    with pngwio.c and pngwutil.c because the 'write' callback and zlib
+    compression both fail to declare their input buffers with 'const'.
+
 
 Send comments/corrections/commendations to png-mng-implement at lists.sf.net:
 (subscription required; visit
diff --git a/CHANGES b/CHANGES
index 5d1b443..a816a9e 100644
--- a/CHANGES
+++ b/CHANGES
@@ -3080,6 +3080,15 @@
   Fixed atomicity of chunk header serialization (Cosmin)
   Added test for io_state in pngtest.c (Cosmin)
   Added "#!/bin/sh" at the top of contrib/pngminim/*/gather.sh scripts.
+  Changes to remove gcc warnings (John Bowler)
+    Certain optional gcc warning flags resulted in warnings in libpng code.
+    With these changes only -Wconversion and -Wcast-qual cannot be turned on.
+    Changes are trivial rearrangements of code.  -Wconversion is not possible
+    for pngrutil.c (because of the widespread use of += et al on variables
+    smaller than (int) or (unsigned int)) and -Wcast-qual is not possible
+    with pngwio.c and pngwutil.c because the 'write' callback and zlib
+    compression both fail to declare their input buffers with 'const'.
+
 
 Send comments/corrections/commendations to png-mng-implement at lists.sf.net
 (subscription required; visit
diff --git a/pngpread.c b/pngpread.c
index 67455c3..c893104 100644
--- a/pngpread.c
+++ b/pngpread.c
@@ -1137,6 +1137,7 @@
             break;
          }
 
+         default:
          case 6:
          {
             png_push_have_row(png_ptr, png_ptr->row_buf + 1);
diff --git a/pngread.c b/pngread.c
index fe180fd..30605d1 100644
--- a/pngread.c
+++ b/pngread.c
@@ -618,6 +618,7 @@
             }
             break;
 
+         default:
          case 6:
             if (!(png_ptr->row_number & 1))
             {
diff --git a/pngrtran.c b/pngrtran.c
index 9cd85df..b3425d3 100644
--- a/pngrtran.c
+++ b/pngrtran.c
@@ -719,6 +719,11 @@
 
       case 3:
          png_ptr->transformations |= PNG_RGB_TO_GRAY_ERR;
+         break;
+
+      default:
+         png_error(png_ptr, "invalid error action to rgb_to_gray");
+         break;
    }
    if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE)
 #ifdef PNG_READ_EXPAND_SUPPORTED
@@ -886,6 +891,8 @@
                }
                break;
 
+            default:
+
             case 8:
 
             case 16:
@@ -1088,6 +1095,9 @@
                   gs = png_reciprocal2(png_ptr->background_gamma,
                       png_ptr->screen_gamma);
                   break;
+
+               default:
+                  png_error(png_ptr, "invalid background gamma type");
             }
 
             png_ptr->background_1.gray = png_gamma_correct(png_ptr,
@@ -1725,6 +1735,9 @@
             }
             break;
          }
+
+         default:
+            break;
       }
       row_info->bit_depth = 8;
       row_info->pixel_depth = (png_byte)(8 * row_info->channels);
@@ -1785,6 +1798,9 @@
 
       switch (row_info->bit_depth)
       {
+         default:
+            break; 
+
          case 2:
          {
             png_bytep bp;
@@ -2966,6 +2982,9 @@
                   }
                   break;
                }
+
+               default:
+                  break;
             }
             break;
          }
@@ -3438,6 +3457,9 @@
             }
             break;
          }
+
+         default:
+            break;
       }
 
       if (row_info->color_type & PNG_COLOR_MASK_ALPHA)
@@ -3646,6 +3668,9 @@
             }
             break;
          }
+
+         default:
+            break;
       }
    }
 }
@@ -3744,6 +3769,9 @@
                }
                break;
             }
+
+            default:
+               break;
          }
          row_info->bit_depth = 8;
          row_info->pixel_depth = 8;
@@ -3901,6 +3929,9 @@
                   }
                   break;
                }
+
+               default:
+                  break;
             }
 
             row_info->bit_depth = 8;
diff --git a/pngrutil.c b/pngrutil.c
index 2cc0b10..38464d2 100644
--- a/pngrutil.c
+++ b/pngrutil.c
@@ -98,6 +98,11 @@
 png_uint_16 (PNGAPI
 png_get_uint_16)(png_const_bytep buf)
 {
+   /* ANSI-C requires an int value to accomodate at least 16 bits so this
+    * works and allows the compiler not to worry about possible narrowing
+    * on 32 bit systems.  (Pre-ANSI systems did not make integers smaller
+    * than 16 bits either.)
+    */
    unsigned int val =
        ((unsigned int)(*buf) << 8) +
        ((unsigned int)(*(buf + 1)));
diff --git a/pngwtran.c b/pngwtran.c
index 066e1e2..1765ef8 100644
--- a/pngwtran.c
+++ b/pngwtran.c
@@ -220,6 +220,9 @@
 
             break;
          }
+
+         default:
+            break;
       }
 
       row_info->bit_depth = (png_byte)bit_depth;
diff --git a/pngwutil.c b/pngwutil.c
index 3377d38..71f2625 100644
--- a/pngwutil.c
+++ b/pngwutil.c
@@ -257,10 +257,12 @@
     */
 
    /* Set up the compression buffers */
+   /* TODO: the following cast hides a potential overflow problem. */
    png_ptr->zstream.avail_in = (uInt)text_len;
+   /* NOTE: assume zlib doesn't overwrite the input */
    png_ptr->zstream.next_in = (Bytef *)text;
-   png_ptr->zstream.avail_out = (uInt)png_ptr->zbuf_size;
-   png_ptr->zstream.next_out = (Bytef *)png_ptr->zbuf;
+   png_ptr->zstream.avail_out = png_ptr->zbuf_size;
+   png_ptr->zstream.next_out = png_ptr->zbuf;
 
    /* This is the same compression loop as in png_write_row() */
    do