From 7a8cd47de272dc260ee8ee3fd153e8bcac4d2815 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nagy-Egri=20M=C3=A1t=C3=A9=20Ferenc?= Date: Tue, 7 Jan 2025 04:40:40 +0100 Subject: [PATCH] Fix crash on loading EXR 2.0 input (#973) Fixes #966 --- tools/imageio/exr.imageio/exrinput.cc | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/tools/imageio/exr.imageio/exrinput.cc b/tools/imageio/exr.imageio/exrinput.cc index 3e3302d3a8..018c36477f 100644 --- a/tools/imageio/exr.imageio/exrinput.cc +++ b/tools/imageio/exr.imageio/exrinput.cc @@ -37,7 +37,9 @@ class ExrInput final : public ImageInput { } ~ExrInput() { // FreeEXRVersion(&version); // No need to call, no such function - FreeEXRImage(&image); + if (image.width != 0 && image.height != 0) { + FreeEXRImage(&image); + } FreeEXRHeader(&header); FreeEXRErrorMessage(err); } @@ -223,6 +225,16 @@ void ExrInput::readImage(void* outputBuffer, size_t bufferByteCount, "Requested format conversion from the input type is not supported.")); } + // Load image version + EXRVersion exr_version; + ec = ParseEXRVersionFromMemory(&exr_version, exrBuffer.data(), exrBuffer.size()); + if (ec != TINYEXR_SUCCESS) + throw std::runtime_error( + fmt::format("EXR load error: {} - {}.", ec, "Failed to parse EXR version")); + if (exr_version.multipart || exr_version.non_image) + throw std::runtime_error( + fmt::format("EXR load error: {}.", "Unsupported EXR version (2.0)")); + // Load image data ec = LoadEXRImageFromMemory(&image, &header, exrBuffer.data(), exrBuffer.size(), &err); if (ec != TINYEXR_SUCCESS)