Fix robustness issue: empty JSON {} in cfg.json or wsec.json (#355)

* Treat empty or effectively-empty configuration files as invalid: restore defaults, optionally fall back to stored settings, and avoid silently using blank configurations.
* Improve presets handling by detecting and recreating empty or undersized preset files so presets are reliably initialized and usable.

---------

Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com>
Co-authored-by: softhack007 <91616163+softhack007@users.noreply.github.com>
This commit is contained in:
Copilot
2026-04-05 17:47:33 +02:00
committed by GitHub
parent 101cb8b5ba
commit 72b1f59e42
3 changed files with 12 additions and 4 deletions

View File

@@ -659,7 +659,7 @@ void deserializeConfigFromFS() {
DEBUG_PRINTLN(F("Reading settings from /cfg.json...")); DEBUG_PRINTLN(F("Reading settings from /cfg.json..."));
success = readObjectFromFile("/cfg.json", nullptr, &doc); success = readObjectFromFile("/cfg.json", nullptr, &doc);
if (!success) { // if file does not exist, optionally try reading from EEPROM and then save defaults to FS if (!success || doc.size() == 0) { // if file does not exist or contains only empty JSON (e.g. "{}"), try reading from EEPROM (if supported) and then save defaults to FS
releaseJSONBufferLock(); releaseJSONBufferLock();
#ifdef WLED_ADD_EEPROM_SUPPORT #ifdef WLED_ADD_EEPROM_SUPPORT
deEEPSettings(); deEEPSettings();
@@ -1122,7 +1122,7 @@ bool deserializeConfigSec() {
if (!requestJSONBufferLock(3)) return false; if (!requestJSONBufferLock(3)) return false;
bool success = readObjectFromFile("/wsec.json", nullptr, &doc); bool success = readObjectFromFile("/wsec.json", nullptr, &doc);
if (!success) { if (!success || doc.size() == 0) { // treat empty JSON (e.g. "{}") the same as a missing file
releaseJSONBufferLock(); releaseJSONBufferLock();
return false; return false;
} }

View File

@@ -223,7 +223,7 @@ bool appendObjectToFile(const char* key, JsonDocument* content, uint32_t s, uint
uint32_t pos = 0; uint32_t pos = 0;
if (!f) return false; if (!f) return false;
if (f.size() < 3) { if (f.size() < 4) { // file uninitialized -> write minimal skeleton
char init[12]; char init[12];
strcpy_P(init, PSTR("{\"0\":{}}")); strcpy_P(init, PSTR("{\"0\":{}}"));
f.print(init); f.print(init);

View File

@@ -123,7 +123,15 @@ bool getPresetName(byte index, String& name)
void initPresetsFile() void initPresetsFile()
{ {
if (WLED_FS.exists(getFileName())) return; if (WLED_FS.exists(getFileName())) {
// treat an empty JSON file (e.g. "{}", "{ }") the same as a missing file:
// f.size() < 4 is the same threshold used in appendObjectToFile() to detect an uninitialized file
File f = WLED_FS.open(getFileName(), "r");
bool empty = f && f.size() < 4; // file does exist due to previous "if"
if (f) f.close();
if (empty) WLED_FS.remove(getFileName()); // remove the empty file so it can be recreated below
else return; // file not empty -> keep (nothing to init)
}
StaticJsonDocument<64> doc; StaticJsonDocument<64> doc;
JsonObject sObj = doc.to<JsonObject>(); JsonObject sObj = doc.to<JsonObject>();