libtiff: TIFFClose() avoid NULL pointer dereferencing. fix#515 (a10f2)

From a10f201b31d28868ad850968099706d553be69a3 Mon Sep 17 00:00:00 2001
From: Su_Laus <[EMAIL REDACTED]>
Date: Fri, 3 Feb 2023 17:38:55 +0100
Subject: [PATCH] TIFFClose() avoid NULL pointer dereferencing. fix#515

Closes #515
(cherry picked from commit d63de61b1ec3385f6383ef9a1f453e4b8b11d536)
---
 libtiff/tif_close.c | 10 ++++++----
 tools/tiffcrop.c    |  5 ++++-
 2 files changed, 10 insertions(+), 5 deletions(-)

diff --git a/libtiff/tif_close.c b/libtiff/tif_close.c
index 9618b77f..7e23aac6 100644
--- a/libtiff/tif_close.c
+++ b/libtiff/tif_close.c
@@ -125,11 +125,13 @@ TIFFCleanup(TIFF* tif)
 void
 TIFFClose(TIFF* tif)
 {
-	TIFFCloseProc closeproc = tif->tif_closeproc;
-	thandle_t fd = tif->tif_clientdata;
+	if (tif != NULL) {
+	    TIFFCloseProc closeproc = tif->tif_closeproc;
+	    thandle_t fd = tif->tif_clientdata;
 
-	TIFFCleanup(tif);
-	(void) (*closeproc)(fd);
+	    TIFFCleanup(tif);
+	    (void)(*closeproc)(fd);
+	}
 }
 
 /* vim: set ts=8 sts=8 sw=8 noet: */
diff --git a/tools/tiffcrop.c b/tools/tiffcrop.c
index 05194c85..d90de4b9 100644
--- a/tools/tiffcrop.c
+++ b/tools/tiffcrop.c
@@ -2561,7 +2561,10 @@ main(int argc, char* argv[])
       }
     }
 
-  TIFFClose(out);
+  if (out != NULL)
+    {
+      TIFFClose(out);
+    }
 
   return (0);
   } /* end main */