greiman / SdFat

Arduino FAT16/FAT32 exFAT Library
MIT License
1.08k stars 507 forks source link

code freezes during recursion on malformed sdcard #378

Open Tarnak-public opened 2 years ago

Tarnak-public commented 2 years ago

Hi,

I've an problem with recursion on corrupted sdcard which results in freezing of mine SAMD. I presume issue is related to bad files/dirs names or general fat structure malforming.

Suggested solution:

sdfat: 2.1.2 ide: vscode + platformio board: MKR Zero sdfatconfig.h is default

Below code used on correctly structured sdcard works great:

void ls(char *path, uint16_t maxLevel)
{
    SdBaseFile dir;
    if (!dir.open(path, O_READ) || !dir.isDir())
    {
        DBG_SDCARD("--- bad_dir");
        return;
    }
    dir.ls(&debugSerialPort, LS_R);
}

code is executed with: SD.ls(&debugSerialPort, LS_R); //full recursion, on corrupted card it fails

I've prepared image with HDDRawCopy tool which can write/read images of files, link to raw image: https://github.com/Tarnak-public/filehosting_for_issues/blob/b5622e9fa7623ae8ed1ce3dbe7849688353938a2/sdfat/recursion_on_malformed_sdcard/SDcardMalformed.7z

greiman commented 2 years ago

It is not practical to detect a corrupt filesystem with the limited memory of a microprocessor like SAMD.

SdFat can detect some problems but corrupt FAT chains or corrupt directory trees can cause problems with ls.

Filesystem corruption can occur due to crashes or failure to close files open for write. Best to check and repair the SD on a PC with Linux or windows.