From 104f391a29d11d504fc4d990eb135fda72eadb04 Mon Sep 17 00:00:00 2001 From: Uri Tauber <142022451+Uri-Tauber@users.noreply.github.com> Date: Thu, 9 Apr 2026 22:16:19 +0300 Subject: [PATCH] fix: two small memory leaks (#1628) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit ## Summary * **What is the goal of this PR?** Fix two issues pointed out by `CodeRabbit` in #1433: 1. Free the output buffer in `readFileToMemory` on early error paths (prevents memory leaks). 2. Check `out.write()` return value in the STORED path (same as in the DEFLATED path). I can confirm both issues were real (not hallucinations). Both fixes are minimal — no behavior change on success. --- ### AI Usage Did you use AI tools to help write this code? _**< NO >**_ --- lib/ZipFile/ZipFile.cpp | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/lib/ZipFile/ZipFile.cpp b/lib/ZipFile/ZipFile.cpp index f7342e5e6..2bb19147a 100644 --- a/lib/ZipFile/ZipFile.cpp +++ b/lib/ZipFile/ZipFile.cpp @@ -400,6 +400,7 @@ uint8_t* ZipFile::readFileToMemory(const char* filename, size_t* size, const boo const auto deflatedData = static_cast(malloc(deflatedDataSize)); if (deflatedData == nullptr) { LOG_ERR("ZIP", "Failed to allocate memory for decompression buffer"); + free(data); return nullptr; } @@ -430,6 +431,7 @@ uint8_t* ZipFile::readFileToMemory(const char* filename, size_t* size, const boo // Continue out of block with data set } else { LOG_ERR("ZIP", "Unsupported compression method"); + free(data); return nullptr; } @@ -469,7 +471,11 @@ bool ZipFile::readFileToStream(const char* filename, Print& out, const size_t ch return false; } - out.write(buffer, dataRead); + if (out.write(buffer, dataRead) != dataRead) { + LOG_ERR("ZIP", "Failed to write all output bytes to stream"); + free(buffer); + return false; + } remaining -= dataRead; }