From 2d3928ed819862690becf015be560439a0b053c2 Mon Sep 17 00:00:00 2001 From: IFAKA <99131130+IFAKA@users.noreply.github.com> Date: Fri, 19 Dec 2025 13:27:08 +0100 Subject: [PATCH] Validate file handle when reading progress.bin (#66) ## Problem Reading progress.bin used `SD.exists()` then `SD.open()` without checking if open succeeded. Race conditions or SD errors could cause file handle to be invalid. ## Fix - Removed redundant `SD.exists()` check - Check if file opened successfully before reading - Verify correct number of bytes were read ## Testing - Builds successfully with `pio run` - Affects: `src/activities/reader/EpubReaderActivity.cpp` --- 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 3bae96d..dd6dd04 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(); }