libtiff: Merge branch 'fix_513' into 'master'

From 8a4f6b587be4fa7bb39fe17f5f9dec52182ab26e Mon Sep 17 00:00:00 2001
From: Even Rouault <[EMAIL REDACTED]>
Date: Sat, 21 Jan 2023 16:54:21 +0100
Subject: [PATCH 1/2] Fax3: fix failure to decode some fax3 images (fixes #513)

Patch by @jsummers26
---
 libtiff/tif_fax3.c  | 2 +-
 test/CMakeLists.txt | 3 ++-
 test/Makefile.am    | 5 ++++-
 3 files changed, 7 insertions(+), 3 deletions(-)

diff --git a/libtiff/tif_fax3.c b/libtiff/tif_fax3.c
index 9d66fc0e..a3c645cb 100644
--- a/libtiff/tif_fax3.c
+++ b/libtiff/tif_fax3.c
@@ -537,7 +537,7 @@ static int Fax3SetupState(TIFF *tif)
       TIFFroundup and TIFFSafeMultiply return zero on integer overflow
     */
     dsp->runs = (uint32_t *)NULL;
-    dsp->nruns = TIFFroundup_32(rowpixels, 32);
+    dsp->nruns = TIFFroundup_32(rowpixels + 1, 32);
     if (needsRefLine)
     {
         dsp->nruns = TIFFSafeMultiply(uint32_t, dsp->nruns, 2);
diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt
index 907aed8f..1c24b660 100644
--- a/test/CMakeLists.txt
+++ b/test/CMakeLists.txt
@@ -46,7 +46,8 @@ set(TIFFIMAGES
     images/rgb-3c-8b.tiff
     images/quad-lzw-compat.tiff
     images/lzw-single-strip.tiff
-    images/testfax4.tiff)
+    images/testfax4.tiff
+    images/testfax3_bug_513.tiff)
 # TIFF images using old-jpeg.  Unused unless JPEG support is enabled.
 set(TIFFIMAGES_OJPEG)
 
diff --git a/test/Makefile.am b/test/Makefile.am
index 4ba5e07c..b53b27fa 100644
--- a/test/Makefile.am
+++ b/test/Makefile.am
@@ -158,6 +158,7 @@ TESTSCRIPTS = \
 	tiff2rgba-rgb-3c-16b.sh \
 	tiff2rgba-rgb-3c-8b.sh \
 	testfax4.sh \
+	testfax3_bug_513.sh \
 	testdeflatelaststripextradata.sh \
 	$(JPEG_DEPENDENT_TESTSCRIPTS)
 
@@ -169,6 +170,7 @@ REFFILES = \
 	refs/o-tiff2ps-PS2.ps \
 	refs/o-tiff2ps-PS3.ps \
 	refs/o-testfax4.tiff \
+	refs/o-testfax3_bug_513.sh.tiff \
 	refs/o-deflate-last-strip-extra-data.tiff
 
 # This list should contain all of the TIFF files in the 'images'
@@ -193,7 +195,8 @@ TIFFIMAGES = \
 	images/ojpeg_single_strip_no_rowsperstrip.tiff \
 	images/testfax4.tiff \
 	images/deflate-last-strip-extra-data.tiff \
-	images/custom_dir_EXIF_GPS.tiff
+	images/custom_dir_EXIF_GPS.tiff \
+	images/testfax3_bug_513.tiff
 
 PNMIMAGES = \
 	images/minisblack-1c-8b.pgm \

From 9477634d2aba898f2b390d4e8c28f6aa556f8aee Mon Sep 17 00:00:00 2001
From: Even Rouault <even.rouault@spatialys.com>
Date: Sat, 21 Jan 2023 16:54:59 +0100
Subject: [PATCH 2/2] Add test for Fax3 decoding issues (refs #513)

---
 test/CMakeLists.txt               |   3 ++-
 test/Makefile.am                  |   2 +-
 test/images/testfax3_bug_513.tiff | Bin 0 -> 198 bytes
 test/refs/o-testfax3_bug_513.tiff | Bin 0 -> 154 bytes
 test/testfax3_bug_513.sh          |  24 ++++++++++++++++++++++++
 5 files changed, 27 insertions(+), 2 deletions(-)
 create mode 100644 test/images/testfax3_bug_513.tiff
 create mode 100644 test/refs/o-testfax3_bug_513.tiff
 create mode 100755 test/testfax3_bug_513.sh

diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt
index 1c24b660..b00c1f68 100644
--- a/test/CMakeLists.txt
+++ b/test/CMakeLists.txt
@@ -47,7 +47,8 @@ set(TIFFIMAGES
     images/quad-lzw-compat.tiff
     images/lzw-single-strip.tiff
     images/testfax4.tiff
-    images/testfax3_bug_513.tiff)
+    images/testfax3_bug_513.tiff
+)
 # TIFF images using old-jpeg.  Unused unless JPEG support is enabled.
 set(TIFFIMAGES_OJPEG)
 
diff --git a/test/Makefile.am b/test/Makefile.am
index b53b27fa..b0fe276a 100644
--- a/test/Makefile.am
+++ b/test/Makefile.am
@@ -170,7 +170,7 @@ REFFILES = \
 	refs/o-tiff2ps-PS2.ps \
 	refs/o-tiff2ps-PS3.ps \
 	refs/o-testfax4.tiff \
-	refs/o-testfax3_bug_513.sh.tiff \
+	refs/o-testfax3_bug_513.tiff \
 	refs/o-deflate-last-strip-extra-data.tiff
 
 # This list should contain all of the TIFF files in the 'images'
diff --git a/test/images/testfax3_bug_513.tiff b/test/images/testfax3_bug_513.tiff
new file mode 100644
index 0000000000000000000000000000000000000000..41544849bc750acdd966109aeb7f2bafacd54431
GIT binary patch
literal 198
zcmebD)M7AT00Q9<D_OpF=Y<zul)xATjOZdIRy+(0jLZy-Kn)5&%m`&O0ohDYHb{;c
w%4P<#*`RD7lR=P?1+13?$QD9UFN(w#gR((pibK_F0NFA~YE+=&Kw&Td0MFPGm;e9(

literal 0
HcmV?d00001

diff --git a/test/refs/o-testfax3_bug_513.tiff b/test/refs/o-testfax3_bug_513.tiff
new file mode 100644
index 0000000000000000000000000000000000000000..cf3b8687941c65d7153e5e73d968bd2d2c525a60
GIT binary patch
literal 154
zcmebD)M5}|U|?7U1ff8{&A`CO%)kf~Q2=5_D4Pk$W`eRoa?D6<HYgh?%pl0f0@lj`
ZWD6mw5k+E)LD?WP#gWv@AgKYF1pxYD1=9ck

literal 0
HcmV?d00001

diff --git a/test/testfax3_bug_513.sh b/test/testfax3_bug_513.sh
new file mode 100755
index 00000000..751db960
--- /dev/null
+++ b/test/testfax3_bug_513.sh
@@ -0,0 +1,24 @@
+#!/bin/sh
+#
+# check decoding of a CCITT Group 3 encoded TIFF
+# hitting https://gitlab.com/libtiff/libtiff/-/issues/513
+. ${srcdir:-.}/common.sh
+infile="${IMAGES}/testfax3_bug_513.tiff"
+outfile="o-testfax3_bug_513.tiff"
+rm -f $outfile
+echo "$MEMCHECK ${TIFFCP} -c none $infile $outfile"
+eval "$MEMCHECK ${TIFFCP} -c none $infile $outfile"
+status=$?
+if [ $status != 0 ] ; then
+  echo "Returned failed status $status!"
+  echo "Output (if any) is in \"${outfile}\"."
+  exit $status
+fi
+echo "$MEMCHECK ${TIFFCMP} $outfile ${REFS}/$outfile"
+eval "$MEMCHECK ${TIFFCMP} $outfile ${REFS}/$outfile"
+status=$?
+if [ $status != 0 ] ; then
+  echo "Returned failed status $status!"
+  echo "\"${outfile}\" differs from reference file."
+  exit $status
+fi