libtiff: Fix some Coverity warnings.

From b4ca093e738e7d199314a2e4040a1632d4dadca8 Mon Sep 17 00:00:00 2001
From: Lee Howard <[EMAIL REDACTED]>
Date: Thu, 16 May 2024 16:13:28 +0000
Subject: [PATCH] Fix some Coverity warnings.

---
 tools/rgb2ycbcr.c |  4 ++--
 tools/tiff2pdf.c  |  2 ++
 tools/tiffcrop.c  | 38 +++++++++++---------------------------
 3 files changed, 15 insertions(+), 29 deletions(-)

diff --git a/tools/rgb2ycbcr.c b/tools/rgb2ycbcr.c
index d4eabbb1..ad51a178 100644
--- a/tools/rgb2ycbcr.c
+++ b/tools/rgb2ycbcr.c
@@ -273,7 +273,7 @@ static int cvtRaster(TIFF *tif, uint32_t *raster, uint32_t width,
     uint32_t nrows = (rowsperstrip > rheight ? rheight : rowsperstrip);
     uint32_t rnrows = roundup(nrows, vertSubSampling);
 
-    cc = rnrows * rwidth +
+    cc = (tsize_t) rnrows * rwidth +
          2 * ((rnrows * rwidth) / (horizSubSampling * vertSubSampling));
     buf = (unsigned char *)_TIFFmalloc(cc);
     // FIXME unchecked malloc
@@ -282,7 +282,7 @@ static int cvtRaster(TIFF *tif, uint32_t *raster, uint32_t width,
         uint32_t nr = (y > nrows ? nrows : y);
         cvtStrip(buf, raster + (y - 1) * width, nr, width);
         nr = roundup(nr, vertSubSampling);
-        acc = nr * rwidth +
+        acc = (tsize_t) nr * rwidth +
               2 * ((nr * rwidth) / (horizSubSampling * vertSubSampling));
         if (!TIFFWriteEncodedStrip(tif, strip++, buf, acc))
         {
diff --git a/tools/tiff2pdf.c b/tools/tiff2pdf.c
index 1dbdc87a..a50e4f96 100644
--- a/tools/tiff2pdf.c
+++ b/tools/tiff2pdf.c
@@ -4475,6 +4475,7 @@ tsize_t t2p_write_pdf_string(const char *pdfstr, TIFF *output)
         {
             switch (pdfstr[i])
             {
+                /* These cases are not reached.  They are handled above.
                 case 0x08:
                     written += t2pWriteFile(output, (tdata_t) "\\b", 2);
                     break;
@@ -4490,6 +4491,7 @@ tsize_t t2p_write_pdf_string(const char *pdfstr, TIFF *output)
                 case 0x0D:
                     written += t2pWriteFile(output, (tdata_t) "\\r", 2);
                     break;
+                */
                 case 0x28:
                     written += t2pWriteFile(output, (tdata_t) "\\(", 2);
                     break;
diff --git a/tools/tiffcrop.c b/tools/tiffcrop.c
index 05a7c5f1..95983479 100644
--- a/tools/tiffcrop.c
+++ b/tools/tiffcrop.c
@@ -1133,9 +1133,6 @@ static int readContigTilesIntoBuffer(TIFF *in, uint8_t *buf,
                             return 1;
                     }
                 }
-                prev_trailing_bits += trailing_bits;
-                /* if (prev_trailing_bits > 7) */
-                /*   prev_trailing_bits-= 8; */
             }
         }
     }
@@ -1369,7 +1366,7 @@ static int writeBufferToSeparateStrips(TIFF *out, uint8_t *buf, uint32_t length,
                   "bytes_per_sample * (width + 1)");
         return 1;
     }
-    rowstripsize = rowsperstrip * bytes_per_sample * (width + 1);
+    rowstripsize = (tsize_t) rowsperstrip * bytes_per_sample * (width + 1);
 
     /* Add 3 padding bytes for extractContigSamples32bits */
     obuf = limitMalloc(rowstripsize + NUM_BUFF_OVERSIZE_BYTES);
@@ -1402,8 +1399,8 @@ static int writeBufferToSeparateStrips(TIFF *out, uint8_t *buf, uint32_t length,
                               (uint64_t)scanlinesize);
                 }
                 dump_info(dump->outfile, dump->format, "",
-                          "Sample %2d, Strip: %2d, bytes: %4d, Row %4d, bytes: "
-                          "%4d, Input offset: %6d",
+                          "Sample %2d, Strip: %2d, bytes: %4zd, Row %4d, bytes: "
+                          "%4d, Input offset: %6zd",
                           s + 1, strip + 1, stripsize, row + 1,
                           (uint32_t)scanlinesize, src - buf);
                 dump_buffer(dump->outfile, dump->format, nrows,
@@ -2647,8 +2644,7 @@ int main(int argc, char *argv[])
 
     if ((argc - optind) == 2)
         pageNum = -1;
-    else
-        total_images = 0;
+
     /* Read multiple input files and write to output file(s) */
     while (optind < argc - 1)
     {
@@ -3418,7 +3414,6 @@ static int extractContigSamples16bits(uint8_t *in, uint8_t *out, uint32_t cols,
             buff1 = (buff1 & matchbits) << (src_bit);
             if (ready_bits < 8) /* add another bps bits to the buffer */
             {
-                bytebuff = 0;
                 buff2 = (buff2 | (buff1 >> ready_bits));
             }
             else /* If we have a full buffer's worth, write it out */
@@ -3536,7 +3531,6 @@ static int extractContigSamples24bits(uint8_t *in, uint8_t *out, uint32_t cols,
 
             if (ready_bits < 16) /* add another bps bits to the buffer */
             {
-                bytebuff1 = bytebuff2 = 0;
                 buff2 = (buff2 | (buff1 >> ready_bits));
             }
             else /* If we have a full buffer's worth, write it out */
@@ -3672,7 +3666,6 @@ static int extractContigSamples32bits(uint8_t *in, uint8_t *out, uint32_t cols,
             }
             else
             { /* add another bps bits to the buffer */
-                bytebuff1 = bytebuff2 = bytebuff3 = bytebuff4 = 0;
                 buff2 = (buff2 | (buff1 >> ready_bits));
             }
             ready_bits += bps;
@@ -3980,7 +3973,6 @@ static int extractContigSamplesShifted24bits(uint8_t *in, uint8_t *out,
 
             if (ready_bits < 16) /* add another bps bits to the buffer */
             {
-                bytebuff1 = bytebuff2 = 0;
                 buff2 = (buff2 | (buff1 >> ready_bits));
             }
             else /* If we have a full buffer's worth, write it out */
@@ -4104,7 +4096,6 @@ static int extractContigSamplesShifted32bits(uint8_t *in, uint8_t *out,
 
             if (ready_bits < 32)
             { /* add another bps bits to the buffer */
-                bytebuff1 = bytebuff2 = bytebuff3 = bytebuff4 = 0;
                 buff2 = (buff2 | (buff1 >> ready_bits));
             }
             else /* If we have a full buffer's worth, write it out */
@@ -4387,7 +4378,6 @@ static int combineSeparateSamplesBytes(unsigned char *srcbuffs[],
                 src = srcbuffs[s] + col_offset;
                 for (i = 0; i < bytes_per_sample; i++)
                     *(dst + i) = *(src + i);
-                src += bytes_per_sample;
                 dst += bytes_per_sample;
             }
         }
@@ -4612,7 +4602,7 @@ static int combineSeparateSamples16bits(uint8_t *in[], uint8_t *out,
             {
                 dump_info(dumpfile, format, "",
                           "Row %3d, Col %3d, Src byte offset %3d  bit offset "
-                          "%2d  Dst offset %3d",
+                          "%2d  Dst offset %3zd",
                           row + 1, col + 1, src_byte, src_bit, dst - out);
                 dump_byte(dumpfile, format, "Final bits", bytebuff);
             }
@@ -4742,7 +4732,7 @@ static int combineSeparateSamples24bits(uint8_t *in[], uint8_t *out,
         {
             dump_info(dumpfile, format, "",
                       "Row %3d, Col %3d, Src byte offset %3d  bit offset %2d  "
-                      "Dst offset %3d",
+                      "Dst offset %3zd",
                       row + 1, col + 1, src_byte, src_bit, dst - out);
 
             dump_long(dumpfile, format, "Match bits ", matchbits);
@@ -4883,7 +4873,7 @@ static int combineSeparateSamples32bits(uint8_t *in[], uint8_t *out,
         {
             dump_info(dumpfile, format, "",
                       "Row %3d, Col %3d, Src byte offset %3d  bit offset %2d  "
-                      "Dst offset %3d",
+                      "Dst offset %3zd",
                       row + 1, col + 1, src_byte, src_bit, dst - out);
 
             dump_wide(dumpfile, format, "Match bits ", matchbits);
@@ -5061,7 +5051,7 @@ static int combineSeparateTileSamples8bits(uint8_t *in[], uint8_t *out,
             {
                 dump_info(dumpfile, format, "",
                           "Row %3d, Col %3d, Src byte offset %3d  bit offset "
-                          "%2d  Dst offset %3d",
+                          "%2d  Dst offset %3zd",
                           row + 1, col + 1, src_byte, src_bit, dst - out);
                 dump_byte(dumpfile, format, "Final bits", buff1);
             }
@@ -5179,7 +5169,7 @@ static int combineSeparateTileSamples16bits(uint8_t *in[], uint8_t *out,
             {
                 dump_info(dumpfile, format, "",
                           "Row %3d, Col %3d, Src byte offset %3d  bit offset "
-                          "%2d  Dst offset %3d",
+                          "%2d  Dst offset %3zd",
                           row + 1, col + 1, src_byte, src_bit, dst - out);
                 dump_byte(dumpfile, format, "Final bits", bytebuff);
             }
@@ -5308,7 +5298,7 @@ static int combineSeparateTileSamples24bits(uint8_t *in[], uint8_t *out,
         {
             dump_info(dumpfile, format, "",
                       "Row %3d, Col %3d, Src byte offset %3d  bit offset %2d  "
-                      "Dst offset %3d",
+                      "Dst offset %3zd",
                       row + 1, col + 1, src_byte, src_bit, dst - out);
 
             dump_long(dumpfile, format, "Match bits ", matchbits);
@@ -5451,7 +5441,7 @@ static int combineSeparateTileSamples32bits(uint8_t *in[], uint8_t *out,
         {
             dump_info(dumpfile, format, "",
                       "Row %3d, Col %3d, Src byte offset %3d  bit offset %2d  "
-                      "Dst offset %3d",
+                      "Dst offset %3zd",
                       row + 1, col + 1, src_byte, src_bit, dst - out);
 
             dump_wide(dumpfile, format, "Match bits ", matchbits);
@@ -9381,7 +9371,6 @@ static int rotateContigSamples16bits(uint16_t rotation, uint16_t spp,
             }
             else
             { /* add another bps bits to the buffer */
-                bytebuff = 0;
                 buff2 = (buff2 | (buff1 >> ready_bits));
             }
             ready_bits += bps;
@@ -9474,7 +9463,6 @@ static int rotateContigSamples24bits(uint16_t rotation, uint16_t spp,
             }
             else
             { /* add another bps bits to the buffer */
-                bytebuff1 = bytebuff2 = 0;
                 buff2 = (buff2 | (buff1 >> ready_bits));
             }
             ready_bits += bps;
@@ -9577,7 +9565,6 @@ static int rotateContigSamples32bits(uint16_t rotation, uint16_t spp,
 
             if (ready_bits < 32)
             { /* add another bps bits to the buffer */
-                bytebuff1 = bytebuff2 = bytebuff3 = bytebuff4 = 0;
                 buff2 = (buff2 | (buff1 >> ready_bits));
             }
             else /* If we have a full buffer's worth, write it out */
@@ -10082,7 +10069,6 @@ static int reverseSamples16bits(uint16_t spp, uint16_t bps, uint32_t width,
 
             if (ready_bits < 8)
             { /* add another bps bits to the buffer */
-                bytebuff = 0;
                 buff2 = (buff2 | (buff1 >> ready_bits));
             }
             else /* If we have a full buffer's worth, write it out */
@@ -10158,7 +10144,6 @@ static int reverseSamples24bits(uint16_t spp, uint16_t bps, uint32_t width,
 
             if (ready_bits < 16)
             { /* add another bps bits to the buffer */
-                bytebuff1 = bytebuff2 = 0;
                 buff2 = (buff2 | (buff1 >> ready_bits));
             }
             else /* If we have a full buffer's worth, write it out */
@@ -10259,7 +10244,6 @@ static int reverseSamples32bits(uint16_t spp, uint16_t bps, uint32_t width,
 
             if (ready_bits < 32)
             { /* add another bps bits to the buffer */
-                bytebuff1 = bytebuff2 = bytebuff3 = bytebuff4 = 0;
                 buff2 = (buff2 | (buff1 >> ready_bits));
             }
             else /* If we have a full buffer's worth, write it out */