libtiff: TIFFHashSetInsert(): fix potential memleak

From a03faa56c532faa04b0a7ac574c6f25382433bda Mon Sep 17 00:00:00 2001
From: Even Rouault <[EMAIL REDACTED]>
Date: Tue, 13 Dec 2022 19:43:00 +0100
Subject: [PATCH] TIFFHashSetInsert(): fix potential memleak

---
 libtiff/tif_hash_set.c | 7 ++++++-
 1 file changed, 6 insertions(+), 1 deletion(-)

diff --git a/libtiff/tif_hash_set.c b/libtiff/tif_hash_set.c
index 5fbfbbea..49718ce2 100644
--- a/libtiff/tif_hash_set.c
+++ b/libtiff/tif_hash_set.c
@@ -433,7 +433,8 @@ static void **TIFFHashSetFindPtr(TIFFHashSet *set, const void *elt)
  * @param set the hash set
  * @param elt the new element to insert in the hash set
  *
- * @return true if success
+ * @return true if success. If false is returned, elt has not been inserted,
+ * but TIFFHashSetInsert() will have run the free function if provided.
  */
 
 bool TIFFHashSetInsert(TIFFHashSet *set, void *elt)
@@ -457,6 +458,8 @@ bool TIFFHashSetInsert(TIFFHashSet *set, void *elt)
         if (!TIFFHashSetRehash(set))
         {
             set->nIndiceAllocatedSize--;
+            if (set->fnFreeEltFunc)
+                set->fnFreeEltFunc(elt);
             return false;
         }
     }
@@ -470,6 +473,8 @@ bool TIFFHashSetInsert(TIFFHashSet *set, void *elt)
     TIFFList *new_elt = TIFFHashSetGetNewListElt(set);
     if (new_elt == NULL)
     {
+        if (set->fnFreeEltFunc)
+            set->fnFreeEltFunc(elt);
         return false;
     }
     new_elt->pData = elt;