Skip to content

Commit

Permalink
fix(core): Properly serialize newlines in label values
Browse files Browse the repository at this point in the history
  • Loading branch information
gjasny committed Nov 3, 2019
1 parent 0152d14 commit 2be75c5
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 4 deletions.
19 changes: 16 additions & 3 deletions core/src/text_serializer.cc
Expand Up @@ -24,10 +24,23 @@ void WriteValue(std::ostream& out, double value) {

void WriteValue(std::ostream& out, const std::string& value) {
for (auto c : value) {
if (c == '\\' || c == '"' || c == '\n') {
out << "\\";
switch (c) {
case '\n':
out << '\\' << 'n';
break;

case '\\':
out << '\\' << c;
break;

case '"':
out << '\\' << c;
break;

default:
out << c;
break;
}
out << c;
}
}

Expand Down
2 changes: 1 addition & 1 deletion core/tests/text_serializer_test.cc
Expand Up @@ -56,7 +56,7 @@ TEST_F(TextSerializerTest, shouldEscapeBackslash) {
TEST_F(TextSerializerTest, shouldEscapeNewline) {
metric.label.resize(1, ClientMetric::Label{"k", "v\nv"});
EXPECT_THAT(Serialize(MetricType::Gauge),
testing::HasSubstr(name + "{k=\"v\\\nv\"}"));
testing::HasSubstr(name + "{k=\"v\\nv\"}"));
}

TEST_F(TextSerializerTest, shouldEscapeDoubleQuote) {
Expand Down

0 comments on commit 2be75c5

Please sign in to comment.