From a6dcffab9bf697b145f68cea9dd0266ce4b73d34 Mon Sep 17 00:00:00 2001 From: Theo Arends <11044339+arendst@users.noreply.github.com> Date: Fri, 3 Jul 2020 12:30:09 +0200 Subject: [PATCH] Fix PROGMEM related exceptions Fix PROGMEM related exceptions (#8828) --- tasmota/support_json.ino | 23 +++++++++++++---------- 1 file changed, 13 insertions(+), 10 deletions(-) diff --git a/tasmota/support_json.ino b/tasmota/support_json.ino index 902a6e926..b78c68eab 100644 --- a/tasmota/support_json.ino +++ b/tasmota/support_json.ino @@ -35,19 +35,21 @@ char EscapeJSONChar(char c) { } String EscapeJSONString(const char *str) { + // As this function is used in ResponseCmndChar() and ResponseCmndIdxChar() + // it needs to be PROGMEM safe! String r(""); if (nullptr == str) { return r; } bool needs_escape = false; size_t len_out = 1; - const char * c = str; - - while (*c) { - if (EscapeJSONChar(*c)) { + const char* c = str; + char ch = '.'; + while (ch != '\0') { + ch = pgm_read_byte(c++); + if (EscapeJSONChar(ch)) { len_out++; needs_escape = true; } - c++; len_out++; } @@ -57,20 +59,21 @@ String EscapeJSONString(const char *str) { r.reserve(len_out); c = str; char *d = r.begin(); - while (*c) { - char c2 = EscapeJSONChar(*c); + char ch = '.'; + while (ch != '\0') { + ch = pgm_read_byte(c++); + char c2 = EscapeJSONChar(ch); if (c2) { - c++; *d++ = '\\'; *d++ = c2; } else { - *d++ = *c++; + *d++ = ch; } } *d = 0; // add NULL terminator r = (char*) r.begin(); // assign the buffer to the string } else { - r = str; + r = FPSTR(str); } return r;