libtiff: Remove use of tif_dirnumber

From c074eb73d3b0618ecb9d95941bc82a39de727e1c Mon Sep 17 00:00:00 2001
From: Even Rouault <[EMAIL REDACTED]>
Date: Fri, 6 Jan 2023 15:23:06 +0100
Subject: [PATCH] Remove use of tif_dirnumber

---
 libtiff/tif_dir.c      |  2 --
 libtiff/tif_dirread.c  | 12 ++----------
 libtiff/tif_hash_set.c |  2 --
 libtiff/tif_hash_set.h |  4 ++--
 libtiff/tif_open.c     |  1 -
 libtiff/tiffiop.h      |  1 -
 6 files changed, 4 insertions(+), 18 deletions(-)

diff --git a/libtiff/tif_dir.c b/libtiff/tif_dir.c
index 4a3211e2..8c275e04 100644
--- a/libtiff/tif_dir.c
+++ b/libtiff/tif_dir.c
@@ -2104,7 +2104,6 @@ int TIFFSetSubDirectory(TIFF *tif, uint64_t diroff)
     {
         /* Reset IFD list to start new one for SubIFD chain and also start
          * SubIFD chain with tif_curdir=0. */
-        tif->tif_dirnumber = 0;
         _TIFFCleanupIFDOffsetAndNumberMaps(tif); /* invalidate IFD loop lists */
         tif->tif_curdir = 0; /* first directory of new chain */
         /* add this offset to new IFD list */
@@ -2233,7 +2232,6 @@ int TIFFUnlinkDirectory(TIFF *tif, tdir_t dirn)
     tif->tif_row = (uint32_t)-1;
     tif->tif_curstrip = (uint32_t)-1;
     tif->tif_curdir = TIFF_NON_EXISTENT_DIR_NUMBER;
-    tif->tif_dirnumber = 0;
     _TIFFCleanupIFDOffsetAndNumberMaps(tif);
     return (1);
 }
diff --git a/libtiff/tif_dirread.c b/libtiff/tif_dirread.c
index 35425b4b..ef21f7f5 100644
--- a/libtiff/tif_dirread.c
+++ b/libtiff/tif_dirread.c
@@ -5448,7 +5448,8 @@ int _TIFFCheckDirNumberAndOffset(TIFF *tif, tdir_t dirn, uint64_t diroff)
     }
 
     /* Arbitrary (hopefully big enough) limit */
-    if (tif->tif_dirnumber >= TIFF_MAX_DIR_COUNT)
+    if (TIFFHashSetSize(tif->tif_map_dir_offset_to_number) >=
+        TIFF_MAX_DIR_COUNT)
     {
         TIFFErrorExtR(tif, "_TIFFCheckDirNumberAndOffset",
                       "Cannot handle more than %u TIFF directories",
@@ -5481,8 +5482,6 @@ int _TIFFCheckDirNumberAndOffset(TIFF *tif, tdir_t dirn, uint64_t diroff)
         return 0;
     }
 
-    tif->tif_dirnumber++;
-
     return 1;
 } /* --- _TIFFCheckDirNumberAndOffset() ---*/
 
@@ -5497,13 +5496,6 @@ int _TIFFGetDirNumberFromOffset(TIFF *tif, uint64_t diroff, tdir_t *dirn)
 {
     if (diroff == 0) /* no more directories */
         return 0;
-    if (tif->tif_dirnumber >= TIFF_MAX_DIR_COUNT)
-    {
-        TIFFErrorExtR(tif, "_TIFFGetDirNumberFromOffset",
-                      "Cannot handle more than %u TIFF directories",
-                      TIFF_MAX_DIR_COUNT);
-        return 0;
-    }
 
     /* Check if offset is already in the list and return matching directory
      * number. Otherwise update IFD list using TIFFNumberOfDirectories() and
diff --git a/libtiff/tif_hash_set.c b/libtiff/tif_hash_set.c
index 37adf61a..22f05d03 100644
--- a/libtiff/tif_hash_set.c
+++ b/libtiff/tif_hash_set.c
@@ -162,7 +162,6 @@ TIFFHashSet *TIFFHashSetNew(TIFFHashSetHashFunc fnHashFunc,
     return set;
 }
 
-#ifdef notdef
 /************************************************************************/
 /*                          TIFFHashSetSize()                            */
 /************************************************************************/
@@ -182,7 +181,6 @@ int TIFFHashSetSize(const TIFFHashSet *set)
     assert(set != NULL);
     return set->nSize;
 }
-#endif
 
 /************************************************************************/
 /*                       TIFFHashSetGetNewListElt()                      */
diff --git a/libtiff/tif_hash_set.h b/libtiff/tif_hash_set.h
index 1a3f8da6..f60e2c67 100644
--- a/libtiff/tif_hash_set.h
+++ b/libtiff/tif_hash_set.h
@@ -71,11 +71,11 @@ extern "C"
 
     void TIFFHashSetDestroy(TIFFHashSet *set);
 
+    int TIFFHashSetSize(const TIFFHashSet *set);
+
 #ifdef notused
     void TIFFHashSetClear(TIFFHashSet *set);
 
-    int TIFFHashSetSize(const TIFFHashSet *set);
-
     /** TIFFHashSetIterEltFunc */
     typedef int (*TIFFHashSetIterEltFunc)(void *elt, void *user_data);
 
diff --git a/libtiff/tif_open.c b/libtiff/tif_open.c
index 8a86a269..e84ef6d2 100644
--- a/libtiff/tif_open.c
+++ b/libtiff/tif_open.c
@@ -485,7 +485,6 @@ TIFF *TIFFClientOpenExt(const char *name, const char *mode,
             goto bad;
         tif->tif_diroff = 0;
         tif->tif_lastdiroff = 0;
-        tif->tif_dirnumber = 0;
         return (tif);
     }
     /*
diff --git a/libtiff/tiffiop.h b/libtiff/tiffiop.h
index f593163e..c419608d 100644
--- a/libtiff/tiffiop.h
+++ b/libtiff/tiffiop.h
@@ -148,7 +148,6 @@ struct tiff
     uint64_t tif_lastdiroff; /* file offset of last directory written so far */
     TIFFHashSet *tif_map_dir_offset_to_number;
     TIFFHashSet *tif_map_dir_number_to_offset;
-    tdir_t tif_dirnumber;  /* number of already seen directories */
     TIFFDirectory tif_dir; /* internal rep of current directory */
     TIFFDirectory
         tif_customdir; /* custom IFDs are separated from the main ones */