From 3a1145b999145903dffcf63a226ecce2fe305037 Mon Sep 17 00:00:00 2001 From: cosmoderp <36945803+cosmoderp@users.noreply.github.com> Date: Sun, 6 Sep 2020 18:34:08 -0400 Subject: [PATCH] Fix Ender 3 V2 (DWIN) buffer overrun (#19268) --- Marlin/src/lcd/dwin/dwin_lcd.cpp | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/Marlin/src/lcd/dwin/dwin_lcd.cpp b/Marlin/src/lcd/dwin/dwin_lcd.cpp index 0ca6b8a3ae..b3d969cc8c 100644 --- a/Marlin/src/lcd/dwin/dwin_lcd.cpp +++ b/Marlin/src/lcd/dwin/dwin_lcd.cpp @@ -37,9 +37,9 @@ #include "dwin_lcd.h" #include // for memset -// Make sure DWIN_SendBuf is large enough to hold the largest -// printed string plus the draw command and tail. -uint8_t DWIN_SendBuf[11 + 24] = { 0xAA }; +// Make sure DWIN_SendBuf is large enough to hold the largest string plus draw command and tail. +// Assume the narrowest (6 pixel) font and 2-byte gb2312-encoded characters. +uint8_t DWIN_SendBuf[11 + DWIN_WIDTH / 6 * 2] = { 0xAA }; uint8_t DWIN_BufTail[4] = { 0xCC, 0x33, 0xC3, 0x3C }; uint8_t databuf[26] = { 0 }; uint8_t receivedType; @@ -63,7 +63,7 @@ inline void DWIN_Long(size_t &i, const uint32_t lval) { } inline void DWIN_String(size_t &i, char * const string) { - const size_t len = strlen(string); + const size_t len = _MIN(sizeof(DWIN_SendBuf) - i, strlen(string)); memcpy(&DWIN_SendBuf[i+1], string, len); i += len; }