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

**URL:** <https://discourse.libsdl.org/t/libtiff-tiffclose-avoid-null-pointer-dereferencing-fix-515-d63de/42426>\
**Category:** SDL Commits\
**Created:** [February 5, 2023, 6:50pm UTC](https://discourse.libsdl.org/t/libtiff-tiffclose-avoid-null-pointer-dereferencing-fix-515-d63de/42426 "2023-02-05T18:50:09Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![SDLPushPostBot](https://discourse.libsdl.org/user_avatar/discourse.libsdl.org/sdlpushpostbot/32/4370_2.png) [@SDLPushPostBot](https://discourse.libsdl.org/u/SDLPushPostBot)\
**Post date:** [February 5, 2023, 6:50pm UTC](https://discourse.libsdl.org/t/libtiff-tiffclose-avoid-null-pointer-dereferencing-fix-515-d63de/42426/1 "2023-02-05T18:50:09Z")

</div>

> <https://github.com/libsdl-org/libtiff/commit/d63de61b1ec3385f6383ef9a1f453e4b8b11d536>
>
> Closes #515

```diff
From d63de61b1ec3385f6383ef9a1f453e4b8b11d536 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
---
 libtiff/tif_close.c | 11 +++++++----
 tools/tiffcrop.c | 5 ++++-
 2 files changed, 11 insertions(+), 5 deletions(-)

diff --git a/libtiff/tif_close.c b/libtiff/tif_close.c
index 985d290c..907d7f13 100644
--- a/libtiff/tif_close.c
+++ b/libtiff/tif_close.c
@@ -147,9 +147,12 @@ void _TIFFCleanupIFDOffsetAndNumberMaps(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);
+ }
 }
diff --git a/tools/tiffcrop.c b/tools/tiffcrop.c
index 7db69883..84e26ac6 100644
--- a/tools/tiffcrop.c
+++ b/tools/tiffcrop.c
@@ -2920,7 +2920,10 @@ int main(int argc, char *argv[])
         }
     }
 
- TIFFClose(out);
+ if (out != NULL)
+ {
+ TIFFClose(out);
+ }
 
     return (0);
 } /* end main */

```
