From c1d5f5d5629758f0b645f002775fc0c3d663cd82 Mon Sep 17 00:00:00 2001 From: IFAKA <99131130+IFAKA@users.noreply.github.com> Date: Fri, 19 Dec 2025 13:28:43 +0100 Subject: [PATCH] Add NULL checks after fopen() in ZipFile (#68) ## Problem Three `fopen()` calls in ZipFile.cpp did not check for NULL before using the file handle. If files cannot be opened, `fseek`/`fread`/`fclose` receive NULL and crash. ## Fix Added NULL checks with appropriate error logging and early returns for all three locations: - `getDataOffset()` - `readFileToMemory()` - `readFileToStream()` ## Testing - Builds successfully with `pio run` - Affects: `lib/ZipFile/ZipFile.cpp` --- lib/ZipFile/ZipFile.cpp | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/lib/ZipFile/ZipFile.cpp b/lib/ZipFile/ZipFile.cpp index 30b44f8..f55bb85 100644 --- a/lib/ZipFile/ZipFile.cpp +++ b/lib/ZipFile/ZipFile.cpp @@ -62,6 +62,10 @@ long ZipFile::getDataOffset(const mz_zip_archive_file_stat& fileStat) const { const uint64_t fileOffset = fileStat.m_local_header_ofs; FILE* file = fopen(filePath.c_str(), "r"); + if (!file) { + Serial.printf("[%lu] [ZIP] Failed to open file for reading local header\n", millis()); + return -1; + } fseek(file, fileOffset, SEEK_SET); const size_t read = fread(pLocalHeader, 1, localHeaderSize, file); fclose(file); @@ -104,6 +108,10 @@ uint8_t* ZipFile::readFileToMemory(const char* filename, size_t* size, const boo } FILE* file = fopen(filePath.c_str(), "rb"); + if (!file) { + Serial.printf("[%lu] [ZIP] Failed to open file for reading\n", millis()); + return nullptr; + } fseek(file, fileOffset, SEEK_SET); const auto deflatedDataSize = static_cast(fileStat.m_comp_size); @@ -175,6 +183,10 @@ bool ZipFile::readFileToStream(const char* filename, Print& out, const size_t ch } FILE* file = fopen(filePath.c_str(), "rb"); + if (!file) { + Serial.printf("[%lu] [ZIP] Failed to open file for streaming\n", millis()); + return false; + } fseek(file, fileOffset, SEEK_SET); const auto deflatedDataSize = static_cast(fileStat.m_comp_size);