From 877de6b398198bca387df791b9232922c5721c80 Mon Sep 17 00:00:00 2001
From: zhailiangliang <zhailiangliang@loongson.cn>
Date: Thu, 21 Dec 2023 15:45:29 +0800
Subject: [PATCH] fix memory leaks in function JpegEncoder::Encode

---
 examples/encoder_jpeg.cc | 8 ++++++++
 1 file changed, 8 insertions(+)

diff --git a/examples/encoder_jpeg.cc b/examples/encoder_jpeg.cc
index 6a8bae163a..b82f8aa39b 100644
--- a/examples/encoder_jpeg.cc
+++ b/examples/encoder_jpeg.cc
@@ -179,6 +179,9 @@ bool JpegEncoder::Encode(const struct heif_image_handle* handle,
       uint32_t skip = (exifdata[0]<<24) | (exifdata[1]<<16) | (exifdata[2]<<8) | exifdata[3];
       if (skip > (exifsize - 4)) {
         fprintf(stderr, "Invalid EXIF data (offset too large)\n");
+        free(exifdata);
+        jpeg_destroy_compress(&cinfo);
+        fclose(fp);
         return false;
       }
       skip += 4;
@@ -188,6 +191,9 @@ bool JpegEncoder::Encode(const struct heif_image_handle* handle,
 
       if (size > std::numeric_limits<uint32_t>::max()) {
         fprintf(stderr, "EXIF larger than 4GB is not supported");
+        free(exifdata);
+        jpeg_destroy_compress(&cinfo);
+        fclose(fp);
         return false;
       }
 
@@ -258,6 +264,8 @@ bool JpegEncoder::Encode(const struct heif_image_handle* handle,
 
   if (heif_image_get_bits_per_pixel(image, heif_channel_Y) != 8) {
     fprintf(stderr, "JPEG writer cannot handle image with >8 bpp.\n");
+    jpeg_destroy_compress(&cinfo);
+    fclose(fp);
     return false;
   }
 
