Skip to content

Commit 72b1f59

Browse files
Copilotsofthack007
andauthored
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>
1 parent 101cb8b commit 72b1f59

3 files changed

Lines changed: 12 additions & 4 deletions

File tree

wled00/cfg.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -659,7 +659,7 @@ void deserializeConfigFromFS() {
659659
DEBUG_PRINTLN(F("Reading settings from /cfg.json..."));
660660

661661
success = readObjectFromFile("/cfg.json", nullptr, &doc);
662-
if (!success) { // if file does not exist, optionally try reading from EEPROM and then save defaults to FS
662+
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
663663
releaseJSONBufferLock();
664664
#ifdef WLED_ADD_EEPROM_SUPPORT
665665
deEEPSettings();
@@ -1122,7 +1122,7 @@ bool deserializeConfigSec() {
11221122
if (!requestJSONBufferLock(3)) return false;
11231123

11241124
bool success = readObjectFromFile("/wsec.json", nullptr, &doc);
1125-
if (!success) {
1125+
if (!success || doc.size() == 0) { // treat empty JSON (e.g. "{}") the same as a missing file
11261126
releaseJSONBufferLock();
11271127
return false;
11281128
}

wled00/file.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -223,7 +223,7 @@ bool appendObjectToFile(const char* key, JsonDocument* content, uint32_t s, uint
223223
uint32_t pos = 0;
224224
if (!f) return false;
225225

226-
if (f.size() < 3) {
226+
if (f.size() < 4) { // file uninitialized -> write minimal skeleton
227227
char init[12];
228228
strcpy_P(init, PSTR("{\"0\":{}}"));
229229
f.print(init);

wled00/presets.cpp

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,15 @@ bool getPresetName(byte index, String& name)
123123

124124
void initPresetsFile()
125125
{
126-
if (WLED_FS.exists(getFileName())) return;
126+
if (WLED_FS.exists(getFileName())) {
127+
// treat an empty JSON file (e.g. "{}", "{ }") the same as a missing file:
128+
// f.size() < 4 is the same threshold used in appendObjectToFile() to detect an uninitialized file
129+
File f = WLED_FS.open(getFileName(), "r");
130+
bool empty = f && f.size() < 4; // file does exist due to previous "if"
131+
if (f) f.close();
132+
if (empty) WLED_FS.remove(getFileName()); // remove the empty file so it can be recreated below
133+
else return; // file not empty -> keep (nothing to init)
134+
}
127135

128136
StaticJsonDocument<64> doc;
129137
JsonObject sObj = doc.to<JsonObject>();

0 commit comments

Comments
 (0)