Skip to content

Commit a3b5497

Browse files
thinkyheadAndy-Big
authored andcommitted
🐛 Prevent MString infinite recursion (MarlinFirmware#26037)
Followup to MarlinFirmware#24390
1 parent e5ced32 commit a3b5497

File tree

2 files changed

+21
-9
lines changed

2 files changed

+21
-9
lines changed

Marlin/src/core/mstring.h

+18-7
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,9 @@ extern void SERIAL_CHAR(char c);
7070
#define SNPRINTF_P(V...) snprintf_P(V)
7171
#endif
7272

73+
#pragma GCC diagnostic push
74+
#pragma GCC diagnostic ignored "-Wdeprecated-declarations"
75+
7376
/**
7477
* @brief MString class template
7578
* @details A class template providing convenient string operators,
@@ -112,6 +115,7 @@ class MString {
112115
MString& set(const MString &s) { strncpy(str, s.str, SIZE); debug(F("MString")); return *this; }
113116
MString& set(const bool &b) { return set(b ? F("true") : F("false")); }
114117
MString& set(const char c) { str[0] = c; if (1 < SIZE) str[1] = '\0'; debug(F("char")); return *this; }
118+
MString& set(const int8_t &i) { SNPRINTF_P(str, SIZE, PSTR("%d"), i); debug(F("int8_t")); return *this; }
115119
MString& set(const short &i) { SNPRINTF_P(str, SIZE, PSTR("%d"), i); debug(F("short")); return *this; }
116120
MString& set(const int &i) { SNPRINTF_P(str, SIZE, PSTR("%d"), i); debug(F("int")); return *this; }
117121
MString& set(const long &l) { SNPRINTF_P(str, SIZE, PSTR("%ld"), l); debug(F("long")); return *this; }
@@ -157,6 +161,7 @@ class MString {
157161
MString& append(const bool &b) { return append(b ? F("true") : F("false")); }
158162
MString& append(const char c) { int sz = length(); if (sz < SIZE) { str[sz] = c; if (sz < SIZE - 1) str[sz + 1] = '\0'; } return *this; }
159163
#if ENABLED(FASTER_APPEND)
164+
MString& append(const int8_t &i) { int sz = length(); SNPRINTF(&str[sz], SIZE - sz, "%d", i); return *this; }
160165
MString& append(const short &i) { int sz = length(); SNPRINTF(&str[sz], SIZE - sz, "%d", i); return *this; }
161166
MString& append(const int &i) { int sz = length(); SNPRINTF(&str[sz], SIZE - sz, "%d", i); return *this; }
162167
MString& append(const long &l) { int sz = length(); SNPRINTF(&str[sz], SIZE - sz, "%ld", l); return *this; }
@@ -165,13 +170,14 @@ class MString {
165170
MString& append(const unsigned int &i) { int sz = length(); SNPRINTF(&str[sz], SIZE - sz, "%u", i); return *this; }
166171
MString& append(const unsigned long &l) { int sz = length(); SNPRINTF(&str[sz], SIZE - sz, "%lu", l); return *this; }
167172
#else
168-
MString& append(const short &i) { char buf[20]; sprintf(buf, "%d", i); return append(buf); }
169-
MString& append(const int &i) { char buf[20]; sprintf(buf, "%d", i); return append(buf); }
170-
MString& append(const long &l) { char buf[20]; sprintf(buf, "%ld", l); return append(buf); }
171-
MString& append(const unsigned char &i) { char buf[20]; sprintf(buf, "%u", i); return append(buf); }
172-
MString& append(const unsigned short &i) { char buf[20]; sprintf(buf, "%u", i); return append(buf); }
173-
MString& append(const unsigned int &i) { char buf[20]; sprintf(buf, "%u", i); return append(buf); }
174-
MString& append(const unsigned long &l) { char buf[20]; sprintf(buf, "%lu", l); return append(buf); }
173+
MString& append(const int8_t &i) { char buf[ 5]; sprintf(buf, "%d", i); return append(buf); }
174+
MString& append(const short &i) { char buf[12]; sprintf(buf, "%d", i); return append(buf); }
175+
MString& append(const int &i) { char buf[12]; sprintf(buf, "%d", i); return append(buf); }
176+
MString& append(const long &l) { char buf[12]; sprintf(buf, "%ld", l); return append(buf); }
177+
MString& append(const unsigned char &i) { char buf[ 5]; sprintf(buf, "%u", i); return append(buf); }
178+
MString& append(const unsigned short &i) { char buf[11]; sprintf(buf, "%u", i); return append(buf); }
179+
MString& append(const unsigned int &i) { char buf[11]; sprintf(buf, "%u", i); return append(buf); }
180+
MString& append(const unsigned long &l) { char buf[11]; sprintf(buf, "%lu", l); return append(buf); }
175181
#endif
176182
MString& append(const float &f) { return append(p_float_t(f, SERIAL_FLOAT_PRECISION)); }
177183
MString& append(const p_float_t &pf) { return append(w_float_t(pf.value, 1, pf.prec)); }
@@ -220,6 +226,9 @@ class MString {
220226
template <typename T, typename... Args>
221227
MString(T arg1, Args... more) { set(arg1); append(more...); }
222228

229+
// Catch unhandled types to prevent infinite recursion
230+
template<typename T> MString& append(T) { return append('?'); }
231+
223232
// Take a list of any number of arguments and append them to the string
224233
template<typename T, typename... Args>
225234
MString& append(T arg1, Args... more) { return append(arg1).append(more...); }
@@ -299,6 +308,8 @@ class MString {
299308

300309
};
301310

311+
#pragma GCC diagnostic pop
312+
302313
#ifndef TS_SIZE
303314
#define TS_SIZE 63
304315
#endif

Marlin/src/tests/marlin_tests.cpp

+3-2
Original file line numberDiff line numberDiff line change
@@ -52,16 +52,17 @@ void runStartupTests() {
5252
// 100 dashes, but chopped down to DEFAULT_MSTRING_SIZE (20)
5353
TSS(repchr_t('-', 100)).echoln();
5454

55-
// Hello World!-123456------ <spaces!
55+
// Hello World!-123456------ <spaces!33
5656
// ^ eol! ... 1234.50*2345.602 = 2895645.67
5757
SString<100> str(F("Hello"));
5858
str.append(F(" World!"));
5959
str += '-';
60-
str += "123";
60+
str += uint8_t(123);
6161
str += F("456");
6262
str += repchr_t('-', 6);
6363
str += Spaces(3);
6464
str += "< spaces!";
65+
str += int8_t(33);
6566
str.eol();
6667
str += "^ eol!";
6768

0 commit comments

Comments
 (0)