From 881a070194783561fd209b7c789a4e75566f7f37 Mon Sep 17 00:00:00 2001
From: zhailiangliang <zhailiangliang@loongson.cn>
Date: Tue, 7 Mar 2023 15:02:08 +0800
Subject: [PATCH] Fix memory leak in tiffcrop.c

Reference:https://gitlab.com/libtiff/libtiff/-/merge_requests/475/diffs
Conflict:Adaptation Context

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

diff --git a/tools/tiffcrop.c b/tools/tiffcrop.c
index e2f8b83..39156b5 100644
--- a/tools/tiffcrop.c
+++ b/tools/tiffcrop.c
@@ -7917,8 +7917,13 @@ createCroppedImage(struct image_data *image, struct crop_mask *crop,
 
   read_buff = *read_buff_ptr;
 
+  /* Memory is freed before crop_buff_ptr is overwritten */
+  if (*crop_buff_ptr != NULL)
+  {
+    _TIFFfree(*crop_buff_ptr);
+  }
+
   /* process full image, no crop buffer needed */
-  crop_buff = read_buff;
   *crop_buff_ptr = read_buff;
   crop->combined_width = image->width;
   crop->combined_length = image->length;
-- 
2.27.0

