Skip to content

Commit 868b2ca

Browse files
committed
savePreset: restore quickload on early exit
* restore quickload on early exit * replace magic numbers with constants
1 parent 80b9069 commit 868b2ca

File tree

1 file changed

+10
-4
lines changed

1 file changed

+10
-4
lines changed

wled00/presets.cpp

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,10 @@ static volatile byte presetToApply = 0;
1212
static volatile byte callModeToApply = 0;
1313
static volatile byte presetToSave = 0;
1414
static volatile int8_t saveLedmap = -1;
15-
static char quickLoad[12]; // WLEDMM 9->12 to prevent crashing with unicode
16-
static char saveName[33];
15+
#define QLOAD_BUFFER 12 // string needed for quickload // WLEDMM 9->12 to prevent crashing with unicode
16+
#define FNAME_BUFFER 32 // string needed for saveName
17+
static char quickLoad[QLOAD_BUFFER+1] = {'\0'}; // 1 extra byte for '\0'
18+
static char saveName[FNAME_BUFFER+1] = {'\0'}; // 1 extra byte for '\0'
1719
static bool includeBri = true, segBounds = true, selectedOnly = false, playlistSave = false;
1820

1921
static const char *getFileName(bool persist = true) {
@@ -305,15 +307,17 @@ void handlePresets()
305307
void savePreset(byte index, const char* pname, JsonObject sObj)
306308
{
307309
if (index == 0 || (index > 250 && index < 255)) return;
308-
if (pname) strlcpy(saveName, pname, 33);
310+
if (pname) strlcpy(saveName, pname, FNAME_BUFFER+1);
309311
else {
310-
if (sObj["n"].is<const char*>()) strlcpy(saveName, sObj["n"].as<const char*>(), 33);
312+
if (sObj["n"].is<const char*>()) strlcpy(saveName, sObj["n"].as<const char*>(), FNAME_BUFFER+1);
311313
else sprintf_P(saveName, PSTR("Preset %d"), index);
312314
}
313315

314316
DEBUG_PRINT(F("Saving preset (")); DEBUG_PRINT(index); DEBUG_PRINT(F(") ")); DEBUG_PRINTLN(saveName);
315317
auto oldpresetToSave = presetToSave; // for recovery in case that esp32SemTake(presetFileMux) fails
316318
auto oldplaylistSave = playlistSave;
319+
char oldQuickLoad[QLOAD_BUFFER+1];
320+
strlcpy(oldQuickLoad, quickLoad, sizeof(oldQuickLoad));
317321

318322
presetToSave = index;
319323
playlistSave = false;
@@ -334,6 +338,7 @@ void savePreset(byte index, const char* pname, JsonObject sObj)
334338
USER_PRINTLN(F("savePreset(): preset file busy, cannot write"));
335339
presetToSave = oldpresetToSave;
336340
playlistSave = oldplaylistSave;
341+
strlcpy(quickLoad, oldQuickLoad, sizeof(quickLoad));
337342
return; // early exit, no change
338343
}
339344

@@ -342,6 +347,7 @@ void savePreset(byte index, const char* pname, JsonObject sObj)
342347
esp32SemGive(presetFileMux); // Release file mutex
343348
presetToSave = oldpresetToSave; // bugfix: restore previous state on error exit
344349
playlistSave = oldplaylistSave;
350+
strlcpy(quickLoad, oldQuickLoad, sizeof(quickLoad));
345351
return; // cannot save API calls to temporary preset (255)
346352
}
347353
sObj.remove("o");

0 commit comments

Comments
 (0)