From 53f37142282ddfb1266f1e05b9eab4632743652b Mon Sep 17 00:00:00 2001 From: imdanberry Date: Fri, 30 Oct 2026 09:42:55 +0000 Subject: [PATCH] Capture errors in LayerInfo::write's parallel compress loop An exception escaping a std::execution::par callable calls std::terminate, so any error raised while compressing a layer's channels (a codec or allocation failure, or PSAPI_LOG_ERROR's throw for an unsupported bit depth) ended the host process instead of reaching the caller. Capture the first exception and rethrow it once the loop has finished, before any layer record is written. Behaviour on success is unchanged. Co-Authored-By: Claude Opus 5.5 --- .../PhotoshopFile/LayerAndMaskInformation.cpp | 65 ++++++++++++------- 1 file changed, 42 insertions(+), 23 deletions(-) diff --git a/PhotoshopAPI/src/PhotoshopFile/LayerAndMaskInformation.cpp b/PhotoshopAPI/src/PhotoshopFile/LayerAndMaskInformation.cpp index 5b747ae2..3f5253b3 100644 --- a/PhotoshopAPI/src/PhotoshopFile/LayerAndMaskInformation.cpp +++ b/PhotoshopAPI/src/PhotoshopFile/LayerAndMaskInformation.cpp @@ -1120,37 +1120,56 @@ void LayerInfo::write(File& document, const FileHeader& header, ProgressCallback // but we do not bother with that at this point WriteBinaryData(document, static_cast(m_LayerRecords.size())); - // Loop over the individual layers and compress them while also storing the channel information + // Loop over the individual layers and compress them while also storing the channel information. An exception + // escaping a std::execution::par callable calls std::terminate, so capture the first error + // and rethrow it once every layer has finished. + std::exception_ptr firstError = nullptr; + std::mutex errorMutex; std::for_each(std::execution::par, m_ChannelImageData.begin(), m_ChannelImageData.end(), [&](ChannelImageData& channel) { - // Get a unique index for each of the layers to compress them in random order - const size_t index = &channel - &m_ChannelImageData[0]; - callback.setTask("Compressing Layer: " + std::string(m_LayerRecords[index].m_LayerName.getString())); - std::vector lrChannelInfo; - std::vector lrCompression; - - if (header.m_Depth == Enum::BitDepth::BD_8) - { - compressedData[index] = channel.compressData(header, lrChannelInfo, lrCompression); - } - else if (header.m_Depth == Enum::BitDepth::BD_16) + try { - compressedData[index] = channel.compressData(header, lrChannelInfo, lrCompression); + // Get a unique index for each of the layers to compress them in random order + const size_t index = &channel - &m_ChannelImageData[0]; + callback.setTask("Compressing Layer: " + std::string(m_LayerRecords[index].m_LayerName.getString())); + std::vector lrChannelInfo; + std::vector lrCompression; + + if (header.m_Depth == Enum::BitDepth::BD_8) + { + compressedData[index] = channel.compressData(header, lrChannelInfo, lrCompression); + } + else if (header.m_Depth == Enum::BitDepth::BD_16) + { + compressedData[index] = channel.compressData(header, lrChannelInfo, lrCompression); + } + else if (header.m_Depth == Enum::BitDepth::BD_32) + { + compressedData[index] = channel.compressData(header, lrChannelInfo, lrCompression); + } + else + { + PSAPI_LOG_ERROR("LayerInfo", "Unsupported BitDepth encountered, currently only 8-, 16- and 32-bit files are supported"); + } + channelInfos[index] = lrChannelInfo; + channelCompression[index] = lrCompression; + callback.setTask("Compressed Layer: " + std::string(m_LayerRecords[index].m_LayerName.getString())); + callback.increment(); } - else if (header.m_Depth == Enum::BitDepth::BD_32) + catch (...) { - compressedData[index] = channel.compressData(header, lrChannelInfo, lrCompression); + std::lock_guard lock(errorMutex); + if (!firstError) + { + firstError = std::current_exception(); + } } - else - { - PSAPI_LOG_ERROR("LayerInfo", "Unsupported BitDepth encountered, currently only 8-, 16- and 32-bit files are supported"); - } - channelInfos[index] = lrChannelInfo; - channelCompression[index] = lrCompression; - callback.setTask("Compressed Layer: " + std::string(m_LayerRecords[index].m_LayerName.getString())); - callback.increment(); }); + if (firstError) + { + std::rethrow_exception(firstError); + } // Write the layer records for (int i = 0; i < m_LayerRecords.size(); ++i)