From e69d9d2e7fde9dff70ba36dbc46b60bdd000cebb Mon Sep 17 00:00:00 2001 From: Ram Mohan Date: Fri, 2 Jun 2023 17:44:45 +0530 Subject: ultrahdr: release memory if encode/decode fails If calls to encode/decode failed, release the allocated memory before returning the control to caller Bug: 285546217 Test: ./ultrahdr_dec_fuzzer Test: ./ultrahdr_enc_fuzzer Change-Id: I276c31cc56656aa41845a16f5d28783bc3adc772 --- libs/ultrahdr/jpegencoderhelper.cpp | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) (limited to 'libs/ultrahdr/jpegencoderhelper.cpp') diff --git a/libs/ultrahdr/jpegencoderhelper.cpp b/libs/ultrahdr/jpegencoderhelper.cpp index ab2f8c7b5a..a03547b538 100644 --- a/libs/ultrahdr/jpegencoderhelper.cpp +++ b/libs/ultrahdr/jpegencoderhelper.cpp @@ -107,12 +107,11 @@ bool JpegEncoderHelper::encode(const void* image, int width, int height, int jpe jpeg_write_marker(&cinfo, JPEG_APP0 + 2, static_cast(iccBuffer), iccSize); } - if (!compress(&cinfo, static_cast(image), isSingleChannel)) { - return false; - } + bool status = compress(&cinfo, static_cast(image), isSingleChannel); jpeg_finish_compress(&cinfo); jpeg_destroy_compress(&cinfo); - return true; + + return status; } void JpegEncoderHelper::setJpegDestination(jpeg_compress_struct* cinfo) { @@ -174,7 +173,7 @@ bool JpegEncoderHelper::compressYuv(jpeg_compress_struct* cinfo, const uint8_t* uint8_t* y_plane = const_cast(yuv); uint8_t* u_plane = const_cast(yuv + y_plane_size); uint8_t* v_plane = const_cast(yuv + y_plane_size + uv_plane_size); - std::unique_ptr empty(new uint8_t[cinfo->image_width]); + std::unique_ptr empty = std::make_unique(cinfo->image_width); memset(empty.get(), 0, cinfo->image_width); const int aligned_width = ALIGNM(cinfo->image_width, kCompressBatchSize); @@ -250,7 +249,7 @@ bool JpegEncoderHelper::compressSingleChannel(jpeg_compress_struct* cinfo, const JSAMPARRAY planes[1] {y}; uint8_t* y_plane = const_cast(image); - std::unique_ptr empty(new uint8_t[cinfo->image_width]); + std::unique_ptr empty = std::make_unique(cinfo->image_width); memset(empty.get(), 0, cinfo->image_width); const int aligned_width = ALIGNM(cinfo->image_width, kCompressBatchSize); -- cgit v1.2.3-59-g8ed1b