mirror of
https://gitlab.isc.org/isc-projects/kea
synced 2025-08-31 05:55:28 +00:00
[1626] fix non-printable escaping in json conv
This commit is contained in:
@@ -318,13 +318,37 @@ str_from_stringstream(std::istream &in, const std::string& file, const int line,
|
|||||||
while (c != EOF && c != '"') {
|
while (c != EOF && c != '"') {
|
||||||
if (c == '\\') {
|
if (c == '\\') {
|
||||||
// see the spec for allowed escape characters
|
// see the spec for allowed escape characters
|
||||||
if (strchr("\"\\/\b\f\n\r\t", in.peek()) != NULL) {
|
switch (in.peek()) {
|
||||||
// drop the escape
|
case '"':
|
||||||
c = in.get();
|
c = '"';
|
||||||
++pos;
|
break;
|
||||||
} else {
|
case '/':
|
||||||
|
c = '/';
|
||||||
|
break;
|
||||||
|
case '\\':
|
||||||
|
c = '\\';
|
||||||
|
break;
|
||||||
|
case 'b':
|
||||||
|
c = '\b';
|
||||||
|
break;
|
||||||
|
case 'f':
|
||||||
|
c = '\f';
|
||||||
|
break;
|
||||||
|
case 'n':
|
||||||
|
c = '\n';
|
||||||
|
break;
|
||||||
|
case 'r':
|
||||||
|
c = '\r';
|
||||||
|
break;
|
||||||
|
case 't':
|
||||||
|
c = '\t';
|
||||||
|
break;
|
||||||
|
default:
|
||||||
throwJSONError("Bad escape", file, line, pos);
|
throwJSONError("Bad escape", file, line, pos);
|
||||||
}
|
}
|
||||||
|
// drop the escaped char
|
||||||
|
in.get();
|
||||||
|
++pos;
|
||||||
}
|
}
|
||||||
ss << c;
|
ss << c;
|
||||||
c = in.get();
|
c = in.get();
|
||||||
@@ -656,10 +680,31 @@ StringElement::toJSON(std::ostream& ss) const {
|
|||||||
// Escape characters as defined in JSON spec
|
// Escape characters as defined in JSON spec
|
||||||
// Note that we do not escape forward slash; this
|
// Note that we do not escape forward slash; this
|
||||||
// is allowed, but not mandatory.
|
// is allowed, but not mandatory.
|
||||||
if (strchr("\"\\\b\f\n\r\t", c) != NULL) {
|
switch (c) {
|
||||||
ss << '\\';
|
case '"':
|
||||||
|
ss << '\\' << c;
|
||||||
|
break;
|
||||||
|
case '\\':
|
||||||
|
ss << '\\' << c;
|
||||||
|
break;
|
||||||
|
case '\b':
|
||||||
|
ss << '\\' << 'b';
|
||||||
|
break;
|
||||||
|
case '\f':
|
||||||
|
ss << '\\' << 'f';
|
||||||
|
break;
|
||||||
|
case '\n':
|
||||||
|
ss << '\\' << 'n';
|
||||||
|
break;
|
||||||
|
case '\r':
|
||||||
|
ss << '\\' << 'r';
|
||||||
|
break;
|
||||||
|
case '\t':
|
||||||
|
ss << '\\' << 't';
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
ss << c;
|
||||||
}
|
}
|
||||||
ss << c;
|
|
||||||
}
|
}
|
||||||
ss << "\"";
|
ss << "\"";
|
||||||
}
|
}
|
||||||
|
@@ -321,11 +321,11 @@ TEST(Element, escape) {
|
|||||||
// String elements.
|
// String elements.
|
||||||
escapeHelper("foo\"bar", "\"foo\\\"bar\"");
|
escapeHelper("foo\"bar", "\"foo\\\"bar\"");
|
||||||
escapeHelper("foo\\bar", "\"foo\\\\bar\"");
|
escapeHelper("foo\\bar", "\"foo\\\\bar\"");
|
||||||
escapeHelper("foo\bbar", "\"foo\\\bbar\"");
|
escapeHelper("foo\bbar", "\"foo\\bbar\"");
|
||||||
escapeHelper("foo\fbar", "\"foo\\\fbar\"");
|
escapeHelper("foo\fbar", "\"foo\\fbar\"");
|
||||||
escapeHelper("foo\nbar", "\"foo\\\nbar\"");
|
escapeHelper("foo\nbar", "\"foo\\nbar\"");
|
||||||
escapeHelper("foo\rbar", "\"foo\\\rbar\"");
|
escapeHelper("foo\rbar", "\"foo\\rbar\"");
|
||||||
escapeHelper("foo\tbar", "\"foo\\\tbar\"");
|
escapeHelper("foo\tbar", "\"foo\\tbar\"");
|
||||||
// Bad escapes
|
// Bad escapes
|
||||||
EXPECT_THROW(Element::fromJSON("\\a"), JSONError);
|
EXPECT_THROW(Element::fromJSON("\\a"), JSONError);
|
||||||
EXPECT_THROW(Element::fromJSON("\\"), JSONError);
|
EXPECT_THROW(Element::fromJSON("\\"), JSONError);
|
||||||
|
Reference in New Issue
Block a user