From a3b5497c01871dafb4013aba338266522d206a75 Mon Sep 17 00:00:00 2001 From: Scott Lahteine Date: Sat, 1 Jul 2023 04:33:53 -0500 Subject: [PATCH] =?UTF-8?q?=F0=9F=90=9B=20Prevent=20MString=20infinite=20r?= =?UTF-8?q?ecursion=20(#26037)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Followup to #24390 --- Marlin/src/core/mstring.h | 25 ++++++++++++++++++------- Marlin/src/tests/marlin_tests.cpp | 5 +++-- 2 files changed, 21 insertions(+), 9 deletions(-) diff --git a/Marlin/src/core/mstring.h b/Marlin/src/core/mstring.h index 67591ba3aead..77a03d7ff140 100644 --- a/Marlin/src/core/mstring.h +++ b/Marlin/src/core/mstring.h @@ -70,6 +70,9 @@ extern void SERIAL_CHAR(char c); #define SNPRINTF_P(V...) snprintf_P(V) #endif +#pragma GCC diagnostic push +#pragma GCC diagnostic ignored "-Wdeprecated-declarations" + /** * @brief MString class template * @details A class template providing convenient string operators, @@ -112,6 +115,7 @@ class MString { MString& set(const MString &s) { strncpy(str, s.str, SIZE); debug(F("MString")); return *this; } MString& set(const bool &b) { return set(b ? F("true") : F("false")); } MString& set(const char c) { str[0] = c; if (1 < SIZE) str[1] = '\0'; debug(F("char")); return *this; } + MString& set(const int8_t &i) { SNPRINTF_P(str, SIZE, PSTR("%d"), i); debug(F("int8_t")); return *this; } MString& set(const short &i) { SNPRINTF_P(str, SIZE, PSTR("%d"), i); debug(F("short")); return *this; } MString& set(const int &i) { SNPRINTF_P(str, SIZE, PSTR("%d"), i); debug(F("int")); return *this; } MString& set(const long &l) { SNPRINTF_P(str, SIZE, PSTR("%ld"), l); debug(F("long")); return *this; } @@ -157,6 +161,7 @@ class MString { MString& append(const bool &b) { return append(b ? F("true") : F("false")); } MString& append(const char c) { int sz = length(); if (sz < SIZE) { str[sz] = c; if (sz < SIZE - 1) str[sz + 1] = '\0'; } return *this; } #if ENABLED(FASTER_APPEND) + MString& append(const int8_t &i) { int sz = length(); SNPRINTF(&str[sz], SIZE - sz, "%d", i); return *this; } MString& append(const short &i) { int sz = length(); SNPRINTF(&str[sz], SIZE - sz, "%d", i); return *this; } MString& append(const int &i) { int sz = length(); SNPRINTF(&str[sz], SIZE - sz, "%d", i); return *this; } MString& append(const long &l) { int sz = length(); SNPRINTF(&str[sz], SIZE - sz, "%ld", l); return *this; } @@ -165,13 +170,14 @@ class MString { MString& append(const unsigned int &i) { int sz = length(); SNPRINTF(&str[sz], SIZE - sz, "%u", i); return *this; } MString& append(const unsigned long &l) { int sz = length(); SNPRINTF(&str[sz], SIZE - sz, "%lu", l); return *this; } #else - MString& append(const short &i) { char buf[20]; sprintf(buf, "%d", i); return append(buf); } - MString& append(const int &i) { char buf[20]; sprintf(buf, "%d", i); return append(buf); } - MString& append(const long &l) { char buf[20]; sprintf(buf, "%ld", l); return append(buf); } - MString& append(const unsigned char &i) { char buf[20]; sprintf(buf, "%u", i); return append(buf); } - MString& append(const unsigned short &i) { char buf[20]; sprintf(buf, "%u", i); return append(buf); } - MString& append(const unsigned int &i) { char buf[20]; sprintf(buf, "%u", i); return append(buf); } - MString& append(const unsigned long &l) { char buf[20]; sprintf(buf, "%lu", l); return append(buf); } + MString& append(const int8_t &i) { char buf[ 5]; sprintf(buf, "%d", i); return append(buf); } + MString& append(const short &i) { char buf[12]; sprintf(buf, "%d", i); return append(buf); } + MString& append(const int &i) { char buf[12]; sprintf(buf, "%d", i); return append(buf); } + MString& append(const long &l) { char buf[12]; sprintf(buf, "%ld", l); return append(buf); } + MString& append(const unsigned char &i) { char buf[ 5]; sprintf(buf, "%u", i); return append(buf); } + MString& append(const unsigned short &i) { char buf[11]; sprintf(buf, "%u", i); return append(buf); } + MString& append(const unsigned int &i) { char buf[11]; sprintf(buf, "%u", i); return append(buf); } + MString& append(const unsigned long &l) { char buf[11]; sprintf(buf, "%lu", l); return append(buf); } #endif MString& append(const float &f) { return append(p_float_t(f, SERIAL_FLOAT_PRECISION)); } MString& append(const p_float_t &pf) { return append(w_float_t(pf.value, 1, pf.prec)); } @@ -220,6 +226,9 @@ class MString { template MString(T arg1, Args... more) { set(arg1); append(more...); } + // Catch unhandled types to prevent infinite recursion + template MString& append(T) { return append('?'); } + // Take a list of any number of arguments and append them to the string template MString& append(T arg1, Args... more) { return append(arg1).append(more...); } @@ -299,6 +308,8 @@ class MString { }; +#pragma GCC diagnostic pop + #ifndef TS_SIZE #define TS_SIZE 63 #endif diff --git a/Marlin/src/tests/marlin_tests.cpp b/Marlin/src/tests/marlin_tests.cpp index 3d14f094a2bf..f61f840176f2 100644 --- a/Marlin/src/tests/marlin_tests.cpp +++ b/Marlin/src/tests/marlin_tests.cpp @@ -52,16 +52,17 @@ void runStartupTests() { // 100 dashes, but chopped down to DEFAULT_MSTRING_SIZE (20) TSS(repchr_t('-', 100)).echoln(); - // Hello World!-123456------ str(F("Hello")); str.append(F(" World!")); str += '-'; - str += "123"; + str += uint8_t(123); str += F("456"); str += repchr_t('-', 6); str += Spaces(3); str += "< spaces!"; + str += int8_t(33); str.eol(); str += "^ eol!";