From 54d866c679ea34ed87d813a6ee50ec4632bb0df1 Mon Sep 17 00:00:00 2001 From: IFAKA Date: Fri, 19 Dec 2025 01:11:04 +0100 Subject: [PATCH] Handle empty spine in getBookSize() and calculateProgress() getBookSize() would crash when accessing index -1 if the spine was empty. calculateProgress() would then divide by zero. Added guards to return 0 in these edge cases. --- lib/Epub/Epub.cpp | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/lib/Epub/Epub.cpp b/lib/Epub/Epub.cpp index 1477d72..b322c53 100644 --- a/lib/Epub/Epub.cpp +++ b/lib/Epub/Epub.cpp @@ -348,13 +348,21 @@ int Epub::getTocIndexForSpineIndex(const int spineIndex) const { return -1; } -size_t Epub::getBookSize() const { return getCumulativeSpineItemSize(getSpineItemsCount() - 1); } +size_t Epub::getBookSize() const { + if (spine.empty()) { + return 0; + } + return getCumulativeSpineItemSize(getSpineItemsCount() - 1); +} // Calculate progress in book uint8_t Epub::calculateProgress(const int currentSpineIndex, const float currentSpineRead) { + size_t bookSize = getBookSize(); + if (bookSize == 0) { + return 0; + } size_t prevChapterSize = (currentSpineIndex >= 1) ? getCumulativeSpineItemSize(currentSpineIndex - 1) : 0; size_t curChapterSize = getCumulativeSpineItemSize(currentSpineIndex) - prevChapterSize; - size_t bookSize = getBookSize(); size_t sectionProgSize = currentSpineRead * curChapterSize; return round(static_cast(prevChapterSize + sectionProgSize) / bookSize * 100.0); }