Skip to content

Commit

Permalink
Fix memory leak in libjpeg-turbo decoder implementation in case of co…
Browse files Browse the repository at this point in the history
…rrupted images (#4138)

Signed-off-by: Joaquin Anton <janton@nvidia.com>
  • Loading branch information
jantonguirao authored and stiepan committed Aug 18, 2022
1 parent c3a6ab7 commit 736babd
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 314 deletions.
24 changes: 8 additions & 16 deletions dali/image/jpeg.cc
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ JpegImage::DecodeImpl(DALIImageType type, const uint8 *jpeg, size_t length) cons
type = shape[2] == 3 ? DALI_RGB : DALI_GRAY;
}
const auto c = NumberOfChannels(type);
Image::Shape target_shape{h, w, c};

DALI_ENFORCE(jpeg != nullptr);
DALI_ENFORCE(length > 0);
Expand Down Expand Up @@ -97,32 +98,23 @@ JpegImage::DecodeImpl(DALIImageType type, const uint8 *jpeg, size_t length) cons
flags.crop_x = crop.anchor[1];
flags.crop_height = crop.shape[0];
flags.crop_width = crop.shape[1];
target_shape[0] = crop.shape[0];
target_shape[1] = crop.shape[1];
}

DALI_ENFORCE(type == DALI_RGB || type == DALI_BGR || type == DALI_GRAY,
"Color space not supported by libjpeg-turbo");
flags.color_space = type;

std::shared_ptr<uint8_t> decoded_image;
int cropped_h = 0;
int cropped_w = 0;
uint8_t* result = jpeg::Uncompress(
jpeg, length, flags, nullptr /* nwarn */,
[&decoded_image, &cropped_h, &cropped_w](int width, int height, int channels) -> uint8* {
decoded_image.reset(
new uint8_t[height * width * channels],
[](uint8_t* data){ delete [] data; } );
cropped_h = height;
cropped_w = width;
return decoded_image.get();
});

if (result == nullptr) {
std::shared_ptr<uint8_t> decoded_image(jpeg::Uncompress(jpeg, length, flags).release(),
[](uint8_t *data) { delete[] data; });

if (decoded_image == nullptr) {
// Failed to decode, fallback
return GenericImage::DecodeImpl(type, jpeg, length);
}

return {decoded_image, {cropped_h, cropped_w, c}};
return {decoded_image, target_shape};
#else // DALI_USE_JPEG_TURBO
return GenericImage::DecodeImpl(type, jpeg, length);
#endif // DALI_USE_JPEG_TURBO
Expand Down
Loading

0 comments on commit 736babd

Please sign in to comment.