From 868b2cad4e66fd9847dee374e2712e9a5b73bef8 Mon Sep 17 00:00:00 2001 From: Frank <91616163+softhack007@users.noreply.github.com> Date: Tue, 30 Dec 2025 02:20:10 +0100 Subject: [PATCH] savePreset: restore quickload on early exit * restore quickload on early exit * replace magic numbers with constants --- wled00/presets.cpp | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/wled00/presets.cpp b/wled00/presets.cpp index 0fbaf57f..c4fe6574 100644 --- a/wled00/presets.cpp +++ b/wled00/presets.cpp @@ -12,8 +12,10 @@ static volatile byte presetToApply = 0; static volatile byte callModeToApply = 0; static volatile byte presetToSave = 0; static volatile int8_t saveLedmap = -1; -static char quickLoad[12]; // WLEDMM 9->12 to prevent crashing with unicode -static char saveName[33]; +#define QLOAD_BUFFER 12 // string needed for quickload // WLEDMM 9->12 to prevent crashing with unicode +#define FNAME_BUFFER 32 // string needed for saveName +static char quickLoad[QLOAD_BUFFER+1] = {'\0'}; // 1 extra byte for '\0' +static char saveName[FNAME_BUFFER+1] = {'\0'}; // 1 extra byte for '\0' static bool includeBri = true, segBounds = true, selectedOnly = false, playlistSave = false; static const char *getFileName(bool persist = true) { @@ -305,15 +307,17 @@ void handlePresets() void savePreset(byte index, const char* pname, JsonObject sObj) { if (index == 0 || (index > 250 && index < 255)) return; - if (pname) strlcpy(saveName, pname, 33); + if (pname) strlcpy(saveName, pname, FNAME_BUFFER+1); else { - if (sObj["n"].is()) strlcpy(saveName, sObj["n"].as(), 33); + if (sObj["n"].is()) strlcpy(saveName, sObj["n"].as(), FNAME_BUFFER+1); else sprintf_P(saveName, PSTR("Preset %d"), index); } DEBUG_PRINT(F("Saving preset (")); DEBUG_PRINT(index); DEBUG_PRINT(F(") ")); DEBUG_PRINTLN(saveName); auto oldpresetToSave = presetToSave; // for recovery in case that esp32SemTake(presetFileMux) fails auto oldplaylistSave = playlistSave; + char oldQuickLoad[QLOAD_BUFFER+1]; + strlcpy(oldQuickLoad, quickLoad, sizeof(oldQuickLoad)); presetToSave = index; playlistSave = false; @@ -334,6 +338,7 @@ void savePreset(byte index, const char* pname, JsonObject sObj) USER_PRINTLN(F("savePreset(): preset file busy, cannot write")); presetToSave = oldpresetToSave; playlistSave = oldplaylistSave; + strlcpy(quickLoad, oldQuickLoad, sizeof(quickLoad)); return; // early exit, no change } @@ -342,6 +347,7 @@ void savePreset(byte index, const char* pname, JsonObject sObj) esp32SemGive(presetFileMux); // Release file mutex presetToSave = oldpresetToSave; // bugfix: restore previous state on error exit playlistSave = oldplaylistSave; + strlcpy(quickLoad, oldQuickLoad, sizeof(quickLoad)); return; // cannot save API calls to temporary preset (255) } sObj.remove("o");