From 21b7f7eb769f4490687929b84f2a182c9b11a2ef Mon Sep 17 00:00:00 2001 From: IFAKA Date: Fri, 19 Dec 2025 01:12:09 +0100 Subject: [PATCH] Add NULL checks after fopen() calls in ZipFile Three fopen() calls in ZipFile.cpp did not check if the file was successfully opened before using the handle. This could cause crashes if the file couldn't be opened. Added NULL checks with appropriate error handling. --- 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);