Fix PROGMEM related exceptions

Fix PROGMEM related exceptions (#8828)
This commit is contained in:
Theo Arends 2020-07-03 12:30:09 +02:00
parent 911dc64e99
commit a6dcffab9b

View File

@ -35,19 +35,21 @@ char EscapeJSONChar(char c) {
} }
String EscapeJSONString(const char *str) { String EscapeJSONString(const char *str) {
// As this function is used in ResponseCmndChar() and ResponseCmndIdxChar()
// it needs to be PROGMEM safe!
String r(""); String r("");
if (nullptr == str) { return r; } if (nullptr == str) { return r; }
bool needs_escape = false; bool needs_escape = false;
size_t len_out = 1; size_t len_out = 1;
const char * c = str; const char* c = str;
char ch = '.';
while (*c) { while (ch != '\0') {
if (EscapeJSONChar(*c)) { ch = pgm_read_byte(c++);
if (EscapeJSONChar(ch)) {
len_out++; len_out++;
needs_escape = true; needs_escape = true;
} }
c++;
len_out++; len_out++;
} }
@ -57,20 +59,21 @@ String EscapeJSONString(const char *str) {
r.reserve(len_out); r.reserve(len_out);
c = str; c = str;
char *d = r.begin(); char *d = r.begin();
while (*c) { char ch = '.';
char c2 = EscapeJSONChar(*c); while (ch != '\0') {
ch = pgm_read_byte(c++);
char c2 = EscapeJSONChar(ch);
if (c2) { if (c2) {
c++;
*d++ = '\\'; *d++ = '\\';
*d++ = c2; *d++ = c2;
} else { } else {
*d++ = *c++; *d++ = ch;
} }
} }
*d = 0; // add NULL terminator *d = 0; // add NULL terminator
r = (char*) r.begin(); // assign the buffer to the string r = (char*) r.begin(); // assign the buffer to the string
} else { } else {
r = str; r = FPSTR(str);
} }
return r; return r;