From bd2c156bab5bc6c83fd46674b0c543495d5de7bc Mon Sep 17 00:00:00 2001 From: IFAKA Date: Fri, 19 Dec 2025 01:10:20 +0100 Subject: [PATCH] Validate file handle and read result for progress.bin The progress.bin reading code assumed SD.open() always succeeded after SD.exists() returned true. This could fail due to race conditions or other SD errors. Now properly checks if the file opened and if the expected number of bytes were read. --- src/activities/reader/EpubReaderActivity.cpp | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/src/activities/reader/EpubReaderActivity.cpp b/src/activities/reader/EpubReaderActivity.cpp index f4e4536..431dee7 100644 --- a/src/activities/reader/EpubReaderActivity.cpp +++ b/src/activities/reader/EpubReaderActivity.cpp @@ -33,13 +33,14 @@ void EpubReaderActivity::onEnter() { epub->setupCacheDir(); - if (SD.exists((epub->getCachePath() + "/progress.bin").c_str())) { - File f = SD.open((epub->getCachePath() + "/progress.bin").c_str()); + File f = SD.open((epub->getCachePath() + "/progress.bin").c_str()); + if (f) { uint8_t data[4]; - f.read(data, 4); - currentSpineIndex = data[0] + (data[1] << 8); - nextPageNumber = data[2] + (data[3] << 8); - Serial.printf("[%lu] [ERS] Loaded cache: %d, %d\n", millis(), currentSpineIndex, nextPageNumber); + if (f.read(data, 4) == 4) { + currentSpineIndex = data[0] + (data[1] << 8); + nextPageNumber = data[2] + (data[3] << 8); + Serial.printf("[%lu] [ERS] Loaded cache: %d, %d\n", millis(), currentSpineIndex, nextPageNumber); + } f.close(); }