Skip to content

Commit

Permalink
🐛 Prevent MString infinite recursion (MarlinFirmware#26037)
Browse files Browse the repository at this point in the history
  • Loading branch information
thinkyhead authored and Andy-Big committed Jul 20, 2023
1 parent e5ced32 commit a3b5497
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 9 deletions.
25 changes: 18 additions & 7 deletions Marlin/src/core/mstring.h
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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; }
Expand Down Expand Up @@ -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; }
Expand All @@ -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)); }
Expand Down Expand Up @@ -220,6 +226,9 @@ class MString {
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...); }
Expand Down Expand Up @@ -299,6 +308,8 @@ class MString {

};

#pragma GCC diagnostic pop

#ifndef TS_SIZE
#define TS_SIZE 63
#endif
Expand Down
5 changes: 3 additions & 2 deletions Marlin/src/tests/marlin_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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!";

Expand Down

0 comments on commit a3b5497

Please sign in to comment.