Commit 6dcbd4ff authored by Alexei Filippov's avatar Alexei Filippov Committed by Commit Bot

[tracing] Escape UTF8 strings in JSON compliant form.

JSON format does not support \xNN encoding. Use \uNNNN instead.

+ Add myself to the src/tracing/OWNERS

BUG=chromium:895974

Change-Id: I410a09d44af08b61837579fa09d263d897b9385e
Reviewed-on: https://chromium-review.googlesource.com/c/1303535Reviewed-by: 's avatarYang Guo <yangguo@chromium.org>
Reviewed-by: 's avatarDmitry Gozman <dgozman@chromium.org>
Commit-Queue: Alexei Filippov <alph@chromium.org>
Cr-Commit-Position: refs/heads/master@{#57180}
parent 5f9b9b99
alph@chromium.org
...@@ -29,12 +29,21 @@ void EscapeAndAppendString(const char* value, std::string* result) { ...@@ -29,12 +29,21 @@ void EscapeAndAppendString(const char* value, std::string* result) {
while (*value) { while (*value) {
unsigned char c = *value++; unsigned char c = *value++;
switch (c) { switch (c) {
case '\t': case '\b':
*result += "\\t"; *result += "\\b";
break;
case '\f':
*result += "\\f";
break; break;
case '\n': case '\n':
*result += "\\n"; *result += "\\n";
break; break;
case '\r':
*result += "\\r";
break;
case '\t':
*result += "\\t";
break;
case '\"': case '\"':
*result += "\\\""; *result += "\\\"";
break; break;
...@@ -44,9 +53,8 @@ void EscapeAndAppendString(const char* value, std::string* result) { ...@@ -44,9 +53,8 @@ void EscapeAndAppendString(const char* value, std::string* result) {
default: default:
if (c < '\x20' || c == '\x7F') { if (c < '\x20' || c == '\x7F') {
char number_buffer[8]; char number_buffer[8];
base::OS::SNPrintF( base::OS::SNPrintF(number_buffer, arraysize(number_buffer), "\\u%04X",
number_buffer, arraysize(number_buffer), "\\x%02X", static_cast<unsigned>(c));
static_cast<unsigned>(static_cast<unsigned char>(c)));
*result += number_buffer; *result += number_buffer;
} else { } else {
*result += c; *result += c;
......
...@@ -116,10 +116,11 @@ TEST(Escaping) { ...@@ -116,10 +116,11 @@ TEST(Escaping) {
// Cannot use the expected value literal directly in CHECK_EQ // Cannot use the expected value literal directly in CHECK_EQ
// as it fails to process the # character on Windows. // as it fails to process the # character on Windows.
const char* expected = const char* expected =
R"({"a":"abc\"'\\\\x\"y'z\n\t\x17","b":"\x01\x02\x03\x04\x05\x06\x07\x08)" R"({"a":"abc\"'\\\\x\"y'z\n\t\u0017","b":"\u0001\u0002\u0003\u0004\u0005)"
R"(\t\n\x0B\x0C\x0D\x0E\x0F\x10\x11\x12\x13\x14\x15\x16\x17\x18\x19\x1A)" R"(\u0006\u0007\b\t\n\u000B\f\r\u000E\u000F\u0010\u0011\u0012\u0013)"
R"(\x1B\x1C\x1D\x1E\x1F !\"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNO)" R"(\u0014\u0015\u0016\u0017\u0018\u0019\u001A\u001B\u001C\u001D\u001E)"
R"(PQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~\x7F"})"; R"(\u001F !\"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ)"
R"([\\]^_`abcdefghijklmnopqrstuvwxyz{|}~\u007F"})";
CHECK_EQ(expected, json); CHECK_EQ(expected, json);
} }
...@@ -129,11 +130,13 @@ TEST(Utf8) { ...@@ -129,11 +130,13 @@ TEST(Utf8) {
auto value = TracedValue::Create(); auto value = TracedValue::Create();
value->SetString("a", string1); value->SetString("a", string1);
value->SetString("b", string2); value->SetString("b", string2);
// Surrogate pair test. Smile emoji === U+1F601 === \xf0\x9f\x98\x81
value->SetString("c", "\U0001F601");
std::string json; std::string json;
value->AppendAsTraceFormat(&json); value->AppendAsTraceFormat(&json);
const char* expected = const char* expected =
"{\"a\":\"\u041b\u044e\u0431\u043b\u044e \u0442\u0435\u0431\u044f, \u041f" "{\"a\":\"\u041B\u044E\u0431\u043B\u044E \u0442\u0435\u0431\u044F, \u041F"
"\u0435\u0442\u0440\u0430 \u0442\u0432\u043e\u0440\u0435\u043d\u044c" "\u0435\u0442\u0440\u0430 \u0442\u0432\u043E\u0440\u0435\u043D\u044C"
"\u0435\",\"b\":\"\u2600\u2600\u26ff\"}"; "\u0435\",\"b\":\"\u2600\u2600\u26FF\",\"c\":\"\xf0\x9f\x98\x81\"}";
CHECK_EQ(expected, json); CHECK_EQ(expected, json);
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment