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(); }