Skip to content

Commit 877de6b

Browse files
fix memory leaks in function JpegEncoder::Encode
1 parent 59b5856 commit 877de6b

File tree

1 file changed

+8
-0
lines changed

1 file changed

+8
-0
lines changed

examples/encoder_jpeg.cc

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,9 @@ bool JpegEncoder::Encode(const struct heif_image_handle* handle,
179179
uint32_t skip = (exifdata[0]<<24) | (exifdata[1]<<16) | (exifdata[2]<<8) | exifdata[3];
180180
if (skip > (exifsize - 4)) {
181181
fprintf(stderr, "Invalid EXIF data (offset too large)\n");
182+
free(exifdata);
183+
jpeg_destroy_compress(&cinfo);
184+
fclose(fp);
182185
return false;
183186
}
184187
skip += 4;
@@ -188,6 +191,9 @@ bool JpegEncoder::Encode(const struct heif_image_handle* handle,
188191

189192
if (size > std::numeric_limits<uint32_t>::max()) {
190193
fprintf(stderr, "EXIF larger than 4GB is not supported");
194+
free(exifdata);
195+
jpeg_destroy_compress(&cinfo);
196+
fclose(fp);
191197
return false;
192198
}
193199

@@ -258,6 +264,8 @@ bool JpegEncoder::Encode(const struct heif_image_handle* handle,
258264

259265
if (heif_image_get_bits_per_pixel(image, heif_channel_Y) != 8) {
260266
fprintf(stderr, "JPEG writer cannot handle image with >8 bpp.\n");
267+
jpeg_destroy_compress(&cinfo);
268+
fclose(fp);
261269
return false;
262270
}
263271

0 commit comments

Comments
 (0)