fix off-by-one array violation, debug message wghen segment name is too long

* fix possible off-by-one array overflow in drawText
* debug message when too-long segment name was dropped
This commit is contained in:
Frank
2025-11-20 18:43:15 +01:00
parent 3738735c84
commit 2c3592c3f3
2 changed files with 2 additions and 1 deletions

View File

@@ -899,7 +899,7 @@ void Segment::drawText(const unsigned char* text, size_t maxLen, int maxLetters,
uint16_t decoded_text[WLED_MAX_SEGNAME_LEN+1] = { 0 }; // UTF-16 converted text. Cannot be longer than WLED_MAX_SEGNAME_LEN
size_t utf16_index = 0;
for(const unsigned char* now = text; now != nullptr && now[0] != '\0'; now = nextUnicode(now, maxLen)) {
if (utf16_index <= WLED_MAX_SEGNAME_LEN) {
if (utf16_index < WLED_MAX_SEGNAME_LEN) {
decoded_text[utf16_index] = unicodeToWchar16(now, maxLen); // UTF-8 decode into decoded_text
decoded_text[utf16_index] = wchar16ToCodepage437(decoded_text[utf16_index]); // decoded_text to CP437 (in-place conversion)
// toDo: ensure that decoded_text[i] is between console_font_YxZ_first and console_font_YxZ_last

View File

@@ -149,6 +149,7 @@ bool deserializeSegment(JsonObject elem, byte it, byte presetId)
seg.name = new(std::nothrow) char[len+1];
if (seg.name) strlcpy(seg.name, name, len+1);
} else {
if (len > 0) { USER_PRINTF("! too-long segment name \"%s\" (%d chars) dropped.\n", name, len);}
// but is empty (already deleted above)
elem.remove("n");
}