BugFix-pri formatovani JSON a zkraceni textu v bufferu doslo k poskozeni

This commit is contained in:
Pavel Brychta 2018-05-09 19:01:48 +02:00
parent 5ddb7195a9
commit ab635dd013

View File

@ -52,40 +52,10 @@ static void print(uint8_t severity, const char *buffer, int length)
while (0 != *buffer)
{
switch (*buffer)// uprava escape sekvenci pro JSON/HTML
{
case '"':
lin[lineptr++] = '\\';
lin[lineptr] = '"';
break;
case '\\':
lin[lineptr++] = '\\';
lin[lineptr] = '\\';
break;
case '/':
lin[lineptr++] = '\\';
lin[lineptr] = '/';
break;
case '<':
strcpy_P(&lin[lineptr], PSTR("&lt;"));
lineptr += 3;
break;
case '>':
strcpy_P(&lin[lineptr], PSTR("&gt;"));
lineptr += 3;
break;
default:
if (*buffer > 0x1f)
lin[lineptr] = *buffer;
else
lin[lineptr] = '?';
break;
}
if (*buffer > 0x1f)
lin[lineptr] = *buffer;
else
lin[lineptr] = '?';
++lineptr;
++buffer;
}
@ -110,6 +80,45 @@ void trace_init(void)
trace_print(TRACE_INFO, F("Trace: Starting..."));
}
static String _getText(const char *buffer)
{
String res;
res.reserve(MAX_LINE_LEN * 4);
while (0 != *buffer)
{
switch (*buffer)// uprava escape sekvenci pro JSON/HTML
{
case '"':
res.concat(F("\\\""));
break;
case '\\':
res.concat(F("\\\\"));
break;
case '/':
res.concat(F("\\/"));
break;
case '<':
res.concat(F("&lt;"));
break;
case '>':
res.concat(F("&gt;"));
break;
default:
res.concat(*buffer);
break;
}
++buffer;
}
return res;
}
void trace_dumpJSON(String &str)
{
@ -124,7 +133,7 @@ void trace_dumpJSON(String &str)
str.concat(F(",\"s\":"));
str.concat(line._severity);
str.concat(F(",\"d\":\""));
str.concat(line._text);
str.concat(_getText(line._text));
str.concat(F("\"}"));
}
}