From ca04badfed50b47921c64e41382ef3f2d5852fb1 Mon Sep 17 00:00:00 2001 From: jpirnay Date: Tue, 12 May 2026 12:19:44 +0200 Subject: [PATCH] Fail closed if the header patch seek fails --- lib/Epub/Epub/Section.cpp | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/lib/Epub/Epub/Section.cpp b/lib/Epub/Epub/Section.cpp index 58105e30..c44741d9 100644 --- a/lib/Epub/Epub/Section.cpp +++ b/lib/Epub/Epub/Section.cpp @@ -572,7 +572,13 @@ bool Section::createSectionFile(const int fontId, const float lineCompression, c } // Patch header with final parseComplete/pageCount and offsets. - file.seek(HEADER_TAIL_PARSE_COMPLETE_OFFSET); + const size_t headerPatchStart = HEADER_TAIL_PARSE_COMPLETE_OFFSET; + if (!file.seek(headerPatchStart)) { + LOG_ERR("SCT", "Failed to seek to section header patch offset %u", HEADER_TAIL_PARSE_COMPLETE_OFFSET); + file.close(); + Storage.remove(filePath.c_str()); + return false; + } serialization::writePod(file, parseComplete); serialization::writePod(file, pageCount); serialization::writePod(file, lutOffset); @@ -580,6 +586,16 @@ bool Section::createSectionFile(const int fontId, const float lineCompression, c serialization::writePod(file, paragraphLutOffset); file.flush(); + const size_t expectedHeaderPatchEnd = headerPatchStart + sizeof(parseComplete) + sizeof(pageCount) + + sizeof(lutOffset) + sizeof(anchorMapOffset) + sizeof(paragraphLutOffset); + if (file.position() != expectedHeaderPatchEnd) { + LOG_ERR("SCT", "Section header patch write failed: wrote %u bytes at offset %u", + static_cast(file.position() - headerPatchStart), static_cast(headerPatchStart)); + file.close(); + Storage.remove(filePath.c_str()); + return false; + } + if (cssParser) { cssParser->clear(); }