libtiff: Merge branch 'fix_readrgbastrip_fpe' into 'master'

From 5e9d713ed497f01c91cda06141354cf04c234ed8 Mon Sep 17 00:00:00 2001
From: Zurab Tsinadze <[EMAIL REDACTED]>
Date: Tue, 9 Apr 2024 23:36:49 +0200
Subject: [PATCH] Avoid FPEs (division by zero) in tif_getimage.c

---
 libtiff/tif_getimage.c | 38 ++++++++++++++++++++++++++++++++++++++
 1 file changed, 38 insertions(+)

diff --git a/libtiff/tif_getimage.c b/libtiff/tif_getimage.c
index 6fee35db..0ada2697 100644
--- a/libtiff/tif_getimage.c
+++ b/libtiff/tif_getimage.c
@@ -760,6 +760,13 @@ static int gtTileContig(TIFFRGBAImage *img, uint32_t *raster, uint32_t w,
         toskew = -(int32_t)(tw - w);
     }
 
+    if (tw == 0 || th == 0)
+    {
+        TIFFErrorExtR(tif, TIFFFileName(tif), "tile width or height is zero");
+        return (0);
+    }
+
+
     /*
      *	Leftmost tile is clipped on left side if col_offset > 0.
      */
@@ -916,6 +923,12 @@ static int gtTileSeparate(TIFFRGBAImage *img, uint32_t *raster, uint32_t w,
             break;
     }
 
+    if (tw == 0 || th == 0)
+    {
+        TIFFErrorExtR(tif, TIFFFileName(tif), "tile width or height is zero");
+        return (0);
+    }
+
     /*
      *	Leftmost tile is clipped on left side if col_offset > 0.
      */
@@ -1092,6 +1105,12 @@ static int gtStripContig(TIFFRGBAImage *img, uint32_t *raster, uint32_t w,
     }
 
     TIFFGetFieldDefaulted(tif, TIFFTAG_ROWSPERSTRIP, &rowsperstrip);
+    if (rowsperstrip == 0)
+    {
+        TIFFErrorExtR(tif, TIFFFileName(tif), "rowsperstrip is zero");
+        return (0);
+    }
+
 
     scanline = TIFFScanlineSize(tif);
     fromskew = (w < imagewidth ? imagewidth - w : 0);
@@ -1216,6 +1235,12 @@ static int gtStripSeparate(TIFFRGBAImage *img, uint32_t *raster, uint32_t w,
     }
 
     TIFFGetFieldDefaulted(tif, TIFFTAG_ROWSPERSTRIP, &rowsperstrip);
+    if (rowsperstrip == 0)
+    {
+        TIFFErrorExtR(tif, TIFFFileName(tif), "rowsperstrip is zero");
+        return (0);
+    }
+
     scanline = TIFFScanlineSize(tif);
     fromskew = (w < imagewidth ? imagewidth - w : 0);
     for (row = 0; row < h; row += nrow)
@@ -3213,6 +3238,13 @@ int TIFFReadRGBAStripExt(TIFF *tif, uint32_t row, uint32_t *raster,
     }
 
     TIFFGetFieldDefaulted(tif, TIFFTAG_ROWSPERSTRIP, &rowsperstrip);
+
+    if (rowsperstrip == 0)
+    {
+        TIFFErrorExtR(tif, TIFFFileName(tif), "rowsperstrip is zero");
+        return (0);
+    }
+
     if ((row % rowsperstrip) != 0)
     {
         TIFFErrorExtR(
@@ -3289,6 +3321,12 @@ int TIFFReadRGBATileExt(TIFF *tif, uint32_t col, uint32_t row, uint32_t *raster,
 
     TIFFGetFieldDefaulted(tif, TIFFTAG_TILEWIDTH, &tile_xsize);
     TIFFGetFieldDefaulted(tif, TIFFTAG_TILELENGTH, &tile_ysize);
+    if (tile_xsize == 0 || tile_ysize == 0)
+    {
+        TIFFErrorExtR(tif, TIFFFileName(tif), "tile_xsize or tile_ysize is zero");
+        return (0);
+    }
+
     if ((col % tile_xsize) != 0 || (row % tile_ysize) != 0)
     {
         TIFFErrorExtR(tif, TIFFFileName(tif),