diff --git a/lib/Epub/Epub/parsers/ContentOpfParser.cpp b/lib/Epub/Epub/parsers/ContentOpfParser.cpp index 5651d5ab..4818558e 100644 --- a/lib/Epub/Epub/parsers/ContentOpfParser.cpp +++ b/lib/Epub/Epub/parsers/ContentOpfParser.cpp @@ -267,18 +267,25 @@ void XMLCALL ContentOpfParser::startElement(void* userData, const XML_Char* name } // EPUB 3 collection metadata: - // Series Name + // Series Name (character data) + // (attribute, some generators) // 1 if (metaProperty) { if (strcmp(metaProperty, "belongs-to-collection") == 0 && self->series.empty()) { - self->series.clear(); - self->state = IN_BOOK_SERIES; - return; + if (metaContent) { + self->series = std::string(metaContent).substr(0, MAX_DESCRIPTION_LENGTH); + } else { + self->state = IN_BOOK_SERIES; + return; + } } if (strcmp(metaProperty, "group-position") == 0 && self->seriesIndex.empty()) { - self->seriesIndex.clear(); - self->state = IN_BOOK_SERIES_INDEX; - return; + if (metaContent) { + self->seriesIndex = std::string(metaContent).substr(0, MAX_DESCRIPTION_LENGTH); + } else { + self->state = IN_BOOK_SERIES_INDEX; + return; + } } } diff --git a/lib/Serialization/Serialization.h b/lib/Serialization/Serialization.h index 37df1c83..c1be4982 100644 --- a/lib/Serialization/Serialization.h +++ b/lib/Serialization/Serialization.h @@ -41,7 +41,10 @@ constexpr uint32_t MAX_STRING_LENGTH = 4096; static bool readString(std::istream& is, std::string& s) { uint32_t len; readPod(is, len); - if (len > MAX_STRING_LENGTH) return false; + if (len > MAX_STRING_LENGTH) { + is.seekg(len, std::ios::cur); // skip payload to keep stream aligned + return false; + } s.resize(len); is.read(&s[0], len); return true; @@ -50,7 +53,10 @@ static bool readString(std::istream& is, std::string& s) { static bool readString(FsFile& file, std::string& s) { uint32_t len; readPod(file, len); - if (len > MAX_STRING_LENGTH) return false; + if (len > MAX_STRING_LENGTH) { + file.seekCur(len); // skip payload to keep file position aligned + return false; + } s.resize(len); file.read(reinterpret_cast(&s[0]), len); return true; diff --git a/src/RecentBooksStore.cpp b/src/RecentBooksStore.cpp index a013db7a..53a0f333 100644 --- a/src/RecentBooksStore.cpp +++ b/src/RecentBooksStore.cpp @@ -122,8 +122,8 @@ bool RecentBooksStore::loadFromBinaryFile() { // Old version, just read paths uint8_t count; serialization::readPod(inputFile, count); - recentBooks.clear(); - recentBooks.reserve(count); + std::vector tmpRecentBooks; + tmpRecentBooks.reserve(count); for (uint8_t i = 0; i < count; i++) { std::string path; if (!serialization::readString(inputFile, path)) { @@ -142,17 +142,18 @@ bool RecentBooksStore::loadFromBinaryFile() { inputFile.close(); return false; } - recentBooks.push_back({path, title, author, "", ""}); + tmpRecentBooks.push_back({path, title, author, "", ""}); } else { - recentBooks.push_back(book); + tmpRecentBooks.push_back(book); } } + recentBooks = std::move(tmpRecentBooks); } else if (version == 3) { uint8_t count; serialization::readPod(inputFile, count); - recentBooks.clear(); - recentBooks.reserve(count); + std::vector tmpRecentBooks; + tmpRecentBooks.reserve(count); uint8_t omitted = 0; for (uint8_t i = 0; i < count; i++) { @@ -170,8 +171,9 @@ bool RecentBooksStore::loadFromBinaryFile() { continue; } - recentBooks.push_back({path, title, author, "", coverBmpPath}); + tmpRecentBooks.push_back({path, title, author, "", coverBmpPath}); } + recentBooks = std::move(tmpRecentBooks); if (omitted > 0) { inputFile.close();