From 06402cf1c6d4778f5bba50e2404344c40584ec3e Mon Sep 17 00:00:00 2001 From: Blaz Kristan Date: Thu, 17 Aug 2023 17:10:29 +0200 Subject: [PATCH] Making buffer fix easy to maintain and portable. Thanks to @softhack007 --- wled00/json.cpp | 8 ++++---- wled00/util.cpp | 8 ++++---- 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/wled00/json.cpp b/wled00/json.cpp index 6fc0e979..c85324d7 100644 --- a/wled00/json.cpp +++ b/wled00/json.cpp @@ -980,8 +980,8 @@ void serializeModeData(JsonArray fxdata) { char lineBuffer[256]; for (size_t i = 0; i < strip.getModeCount(); i++) { - strncpy_P(lineBuffer, strip.getModeData(i), 255); - lineBuffer[255] = '\0'; // terminate string + strncpy_P(lineBuffer, strip.getModeData(i), sizeof(lineBuffer)/sizeof(char)-1); + lineBuffer[sizeof(lineBuffer)/sizeof(char)-1] = '\0'; // terminate string if (lineBuffer[0] != 0) { char* dataPtr = strchr(lineBuffer,'@'); if (dataPtr) fxdata.add(dataPtr+1); @@ -996,8 +996,8 @@ void serializeModeNames(JsonArray arr) { char lineBuffer[256]; for (size_t i = 0; i < strip.getModeCount(); i++) { - strncpy_P(lineBuffer, strip.getModeData(i), 255); - lineBuffer[255] = '\0'; // terminate string + strncpy_P(lineBuffer, strip.getModeData(i), sizeof(lineBuffer)/sizeof(char)-1); + lineBuffer[sizeof(lineBuffer)/sizeof(char)-1] = '\0'; // terminate string if (lineBuffer[0] != 0) { char* dataPtr = strchr(lineBuffer,'@'); if (dataPtr) *dataPtr = 0; // terminate mode data after name diff --git a/wled00/util.cpp b/wled00/util.cpp index 220b4773..3c58bf9c 100644 --- a/wled00/util.cpp +++ b/wled00/util.cpp @@ -233,8 +233,8 @@ uint8_t extractModeName(uint8_t mode, const char *src, char *dest, uint8_t maxLe if (mode < strip.getModeCount()) { char lineBuffer[256]; //strcpy_P(lineBuffer, (const char*)pgm_read_dword(&(WS2812FX::_modeData[mode]))); - strncpy_P(lineBuffer, strip.getModeData(mode), 255); - lineBuffer[255] = '\0'; // terminate string + strncpy_P(lineBuffer, strip.getModeData(mode), sizeof(lineBuffer)/sizeof(char)-1); + lineBuffer[sizeof(lineBuffer)/sizeof(char)-1] = '\0'; // terminate string size_t len = strlen(lineBuffer); size_t j = 0; for (; j < maxLen && j < len; j++) { @@ -357,8 +357,8 @@ int16_t extractModeDefaults(uint8_t mode, const char *segVar) { if (mode < strip.getModeCount()) { char lineBuffer[256]; - strncpy_P(lineBuffer, strip.getModeData(mode), 255); - lineBuffer[255] = '\0'; // terminate string + strncpy_P(lineBuffer, strip.getModeData(mode), sizeof(lineBuffer)/sizeof(char)-1); + lineBuffer[sizeof(lineBuffer)/sizeof(char)-1] = '\0'; // terminate string if (lineBuffer[0] != 0) { char* startPtr = strrchr(lineBuffer, ';'); // last ";" in FX data if (!startPtr) return -1;