Skip to content

Handle corrupt preferences files #129

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 30, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 19 additions & 5 deletions storage.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,11 @@ void dumpPrefs(fs::FS &fs){
if (fs.exists(PREFERENCES_FILE)) {
// Dump contents for debug
File file = fs.open(PREFERENCES_FILE, FILE_READ);
while (file.available()) Serial.print(char(file.read()));
int countSize = 0;
while (file.available() && countSize <= PREFERENCES_MAX_SIZE) {
Serial.print(char(file.read()));
countSize++;
}
Serial.println("");
file.close();
} else {
Expand All @@ -62,15 +66,25 @@ void loadPrefs(fs::FS &fs){
Serial.printf("Loading preferences from file %s\r\n", PREFERENCES_FILE);
File file = fs.open(PREFERENCES_FILE, FILE_READ);
if (!file) {
Serial.println("Failed to open preferences file");
Serial.println("Failed to open preferences file for reading, maybe corrupt, removing");
removePrefs(SPIFFS);
return;
}
size_t size = file.size();
if (size > 800) {
Serial.println("Preferences file size is too large, maybe corrupt");
if (size > PREFERENCES_MAX_SIZE) {
Serial.println("Preferences file size is too large, maybe corrupt, removing");
removePrefs(SPIFFS);
return;
}
while (file.available()) prefs += char(file.read());
while (file.available()) {
prefs += char(file.read());
if (prefs.length() > size) {
// corrupted SPIFFS files can return data beyond their declared size.
Serial.println("Preferences file failed to load properly, appears to be corrupt, removing");
removePrefs(SPIFFS);
return;
}
}
// get sensor reference
sensor_t * s = esp_camera_sensor_get();
// process all the settings
Expand Down
1 change: 1 addition & 0 deletions storage.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#include "SPIFFS.h"

#define FORMAT_SPIFFS_IF_FAILED true
#define PREFERENCES_MAX_SIZE 500

#define PREFERENCES_FILE "/esp32cam-preferences.json"

Expand Down