0
0
Fork 0
mirror of https://github.com/MarlinFirmware/Marlin.git synced 2025-03-13 01:40:09 +00:00

🐛 Prevent MString infinite recursion (#26037)

Followup to #24390
This commit is contained in:
Scott Lahteine 2023-07-01 04:33:53 -05:00 committed by GitHub
parent f9c8703e77
commit 4985acafad
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 21 additions and 9 deletions

View file

@ -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 @@ public:
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 @@ public:
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 @@ public:
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 @@ public:
template <typename T, typename... Args>
MString(T arg1, Args... more) { set(arg1); append(more...); }
// Catch unhandled types to prevent infinite recursion
template<typename T> MString& append(T) { return append('?'); }
// Take a list of any number of arguments and append them to the string
template<typename T, typename... Args>
MString& append(T arg1, Args... more) { return append(arg1).append(more...); }
@ -299,6 +308,8 @@ public:
};
#pragma GCC diagnostic pop
#ifndef TS_SIZE
#define TS_SIZE 63
#endif

View file

@ -52,16 +52,17 @@ void runStartupTests() {
// 100 dashes, but chopped down to DEFAULT_MSTRING_SIZE (20)
TSS(repchr_t('-', 100)).echoln();
// Hello World!-123456------ <spaces!
// Hello World!-123456------ <spaces!33
// ^ eol! ... 1234.50*2345.602 = 2895645.67
SString<100> 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!";