From effe52368ca797ec4de7d822c2ff4c7a4e42946e Mon Sep 17 00:00:00 2001 From: Jean-Louis Date: Thu, 24 Dec 2020 12:18:07 +0100 Subject: [PATCH] No need to allocate a full size buffer for small files --- code/Common/ZipArchiveIOSystem.cpp | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/code/Common/ZipArchiveIOSystem.cpp b/code/Common/ZipArchiveIOSystem.cpp index 407601220..870dd702d 100644 --- a/code/Common/ZipArchiveIOSystem.cpp +++ b/code/Common/ZipArchiveIOSystem.cpp @@ -218,16 +218,19 @@ ZipFile *ZipFileInfo::Extract(unzFile zip_handle) const { ZipFile *zip_file = new ZipFile(m_Size); // Unzip has a limit of UINT16_MAX bytes buffer - std::unique_ptr unzipBuffer = std::unique_ptr(new uint8_t[UINT16_MAX]); + uint16_t unzipBufferSize = zip_file->m_Size <= UINT16_MAX ? static_cast(zip_file->m_Size) : UINT16_MAX; + std::unique_ptr unzipBuffer = std::unique_ptr(new uint8_t[unzipBufferSize]); size_t readCount = 0; - while (readCount < zip_file->m_Size) { + while (readCount < zip_file->m_Size) + { size_t bufferSize = zip_file->m_Size - readCount; if (bufferSize > UINT16_MAX) { bufferSize = UINT16_MAX; } int ret = unzReadCurrentFile(zip_handle, unzipBuffer.get(), static_cast(bufferSize)); - if (ret != static_cast(bufferSize)) { + if (ret != static_cast(bufferSize)) + { // Failed, release the memory delete zip_file; zip_file = nullptr;