libtiff: Merge branch 'coverity_fixes' into 'master' (fb0ad)

From 6fd7d51025084fd2a157a0dbf433b411cd449389 Mon Sep 17 00:00:00 2001
From: Even Rouault <[EMAIL REDACTED]>
Date: Thu, 16 May 2024 18:49:21 +0200
Subject: [PATCH 1/3] tiffset.c: avoid false positive Coverity Scan warning on
 64-bit builds (CID 1518997)

---
 tools/tiffset.c | 8 +++++---
 1 file changed, 5 insertions(+), 3 deletions(-)

diff --git a/tools/tiffset.c b/tools/tiffset.c
index c768d989..c21acf7c 100644
--- a/tools/tiffset.c
+++ b/tools/tiffset.c
@@ -461,9 +461,11 @@ int main(int argc, char *argv[])
             fsize = TIFFftell(fp) + 1;
             rewind(fp);
 
-            if (fsize >
-                    TIFF_TMSIZE_T_MAX || /* for x32 tmsize_t is only int32_t */
-                fsize <= 0)
+            /* for x32 tmsize_t is only int32_t. The - 1 is just here to make
+             * Coverity Scan happy on 64 bit builds where the condition would
+             * be always true otherwise.
+             */
+            if (fsize > TIFF_TMSIZE_T_MAX - 1 || fsize <= 0)
             {
                 fprintf(
                     stderr,

From 4b0bbcfacfda8815913377a64901144116f6a4eb Mon Sep 17 00:00:00 2001
From: Even Rouault <even.rouault@spatialys.com>
Date: Thu, 16 May 2024 18:49:50 +0200
Subject: [PATCH 2/3] tiffdump.c: fix wrong printf formattre in error message
 (CID 1472932)

---
 tools/tiffdump.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/tools/tiffdump.c b/tools/tiffdump.c
index df1f9358..03504fc4 100644
--- a/tools/tiffdump.c
+++ b/tools/tiffdump.c
@@ -360,7 +360,7 @@ static uint64_t ReadDirectory(int fd, unsigned int ix, uint64_t off)
     {
         n /= direntrysize;
         Error("Could only read %" PRIu32 " of %" PRIu16
-              " entries in directory at offset %#" PRIu64,
+              " entries in directory at offset %" PRIu64,
               n, dircount, off);
         dircount = n;
         nextdiroff = 0;

From 2454463357bde2a3a652473d87fab29a2ce389cb Mon Sep 17 00:00:00 2001
From: Even Rouault <even.rouault@spatialys.com>
Date: Thu, 16 May 2024 18:50:46 +0200
Subject: [PATCH 3/3] contrib/addtiffo: validate return of
 TIFFWriteEncodedXXXX() calls (CID 1024680)

---
 contrib/addtiffo/tif_ovrcache.c | 34 +++++++++++++++++++++------------
 1 file changed, 22 insertions(+), 12 deletions(-)

diff --git a/contrib/addtiffo/tif_ovrcache.c b/contrib/addtiffo/tif_ovrcache.c
index 9cbf09d1..09d8ec58 100644
--- a/contrib/addtiffo/tif_ovrcache.c
+++ b/contrib/addtiffo/tif_ovrcache.c
@@ -198,8 +198,11 @@ static void TIFFWriteOvrRow(TIFFOvrCache *psCache)
                     nTileID = TIFFComputeTile(
                         psCache->hTIFF, iTileX * psCache->nBlockXSize,
                         iTileY * psCache->nBlockYSize, 0, (tsample_t)iSample);
-                    TIFFWriteEncodedTile(psCache->hTIFF, nTileID, pabyData,
-                                         TIFFTileSize(psCache->hTIFF));
+                    if (TIFFWriteEncodedTile(psCache->hTIFF, nTileID, pabyData,
+                                             TIFFTileSize(psCache->hTIFF)) < 0)
+                    {
+                        fprintf(stderr, "TIFFWriteEncodedTile() failed\n");
+                    }
                 }
                 else
                 {
@@ -210,9 +213,12 @@ static void TIFFWriteOvrRow(TIFFOvrCache *psCache)
                     if ((iTileY + 1) * psCache->nBlockYSize > psCache->nYSize)
                         RowsInStrip =
                             psCache->nYSize - iTileY * psCache->nBlockYSize;
-                    TIFFWriteEncodedStrip(
-                        psCache->hTIFF, nTileID, pabyData,
-                        TIFFVStripSize(psCache->hTIFF, RowsInStrip));
+                    if (TIFFWriteEncodedStrip(
+                            psCache->hTIFF, nTileID, pabyData,
+                            TIFFVStripSize(psCache->hTIFF, RowsInStrip)) < 0)
+                    {
+                        fprintf(stderr, "TIFFWriteEncodedStrip() failed\n");
+                    }
                 }
             }
         }
@@ -225,8 +231,11 @@ static void TIFFWriteOvrRow(TIFFOvrCache *psCache)
                 nTileID = TIFFComputeTile(psCache->hTIFF,
                                           iTileX * psCache->nBlockXSize,
                                           iTileY * psCache->nBlockYSize, 0, 0);
-                TIFFWriteEncodedTile(psCache->hTIFF, nTileID, pabyData,
-                                     TIFFTileSize(psCache->hTIFF));
+                if (TIFFWriteEncodedTile(psCache->hTIFF, nTileID, pabyData,
+                                         TIFFTileSize(psCache->hTIFF)) < 0)
+                {
+                    fprintf(stderr, "TIFFWriteEncodedTile() failed\n");
+                }
             }
             else
             {
@@ -236,14 +245,15 @@ static void TIFFWriteOvrRow(TIFFOvrCache *psCache)
                 if ((iTileY + 1) * psCache->nBlockYSize > psCache->nYSize)
                     RowsInStrip =
                         psCache->nYSize - iTileY * psCache->nBlockYSize;
-                TIFFWriteEncodedStrip(
-                    psCache->hTIFF, nTileID, pabyData,
-                    TIFFVStripSize(psCache->hTIFF, RowsInStrip));
+                if (TIFFWriteEncodedStrip(
+                        psCache->hTIFF, nTileID, pabyData,
+                        TIFFVStripSize(psCache->hTIFF, RowsInStrip)) < 0)
+                {
+                    fprintf(stderr, "TIFFWriteEncodedStrip() failed\n");
+                }
             }
         }
     }
-    /* TODO: add checks on error status return of TIFFWriteEncodedTile and
-     * TIFFWriteEncodedStrip */
 
     /* -------------------------------------------------------------------- */
     /*      Rotate buffers.                                                 */