Skip to content

Commit 5b634a9

Browse files
committed
Update sd_sdfat2_esp32.cpp
Add edge case sanity check
1 parent 63a5ea2 commit 5b634a9

1 file changed

Lines changed: 8 additions & 1 deletion

File tree

‎src/modules/filesystem/sd/sd_sdfat2_esp32.cpp‎

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -500,10 +500,17 @@ ESP_SDFile ESP_SDFile::openNextFile() {
500500
ESP3D_File tmp = dir.openNextFile();
501501
// SdFat stops at a long name entry whose checksum doesn't match the short name
502502
// that follows it, e.g. a bootloader renaming FIRMWARE.BIN to FIRMWARE.CUR.
503-
// Skip such entries and stop only at the end of the directory or on an error.
503+
// Skip such entries and stop only at the end of the directory or on an error,
504+
// or if the position did not advance.
505+
uint64_t lastPos = UINT64_MAX;
504506
while (!tmp && !ESP3D_SD_Card.sdErrorCode()) {
505507
// The entry SdFat stopped at is the last one it read
506508
const uint64_t pos = dir.curPosition();
509+
// No progress since last attempt: end of cluster chain, stop here
510+
if (pos == lastPos) {
511+
break;
512+
}
513+
lastPos = pos;
507514
uint8_t entry[32];
508515
if (pos < sizeof(entry) || !dir.seekSet(pos - sizeof(entry)) ||
509516
dir.read(entry, sizeof(entry)) != sizeof(entry) || entry[0] == 0x00) {

0 commit comments

Comments
 (0)