diff options
author | Jelte Jansen <jelte@isc.org> | 2012-03-29 17:10:45 +0200 |
---|---|---|
committer | Jelte Jansen <jelte@isc.org> | 2012-03-29 17:10:45 +0200 |
commit | 63e4fc15cc2c66b07168bb15e2e6af464c235a3d (patch) | |
tree | b463f42b901a5e4b52c9e08ff60bd9f24407d74c /src/lib/cc/data.cc | |
parent | [master] Show testnames when running Python unit tests (diff) | |
download | kea-63e4fc15cc2c66b07168bb15e2e6af464c235a3d.tar.xz kea-63e4fc15cc2c66b07168bb15e2e6af464c235a3d.zip |
[1626] escape JSON strings correctly
Diffstat (limited to 'src/lib/cc/data.cc')
-rw-r--r-- | src/lib/cc/data.cc | 28 |
1 files changed, 23 insertions, 5 deletions
diff --git a/src/lib/cc/data.cc b/src/lib/cc/data.cc index 77f948aea8..8c050fe40d 100644 --- a/src/lib/cc/data.cc +++ b/src/lib/cc/data.cc @@ -314,12 +314,21 @@ str_from_stringstream(std::istream &in, const std::string& file, const int line, } else { throwJSONError("String expected", file, line, pos); } + while (c != EOF && c != '"') { - ss << c; - if (c == '\\' && in.peek() == '"') { - ss << in.get(); - ++pos; + if (c == '\\') { + // next char must be either another \ or " + // see the spec for allowed escape characters + if (strchr("\"\\/\b\f\n\r\t", in.peek()) != NULL) { + // drop the escape + c = in.get(); + ++pos; + } else { + std::cout << "[XX] cur string: " << ss.str() << std::endl; + throwJSONError(std::string("Bad escape for: ") + (char)in.peek(), file, line, pos); + } } + ss << c; c = in.get(); ++pos; } @@ -642,7 +651,16 @@ NullElement::toJSON(std::ostream& ss) const { void StringElement::toJSON(std::ostream& ss) const { ss << "\""; - ss << stringValue(); + char c; + const std::string& str = stringValue(); + for (size_t i = 0; i < str.size(); ++i) { + c = str[i]; + // Escape characters as defined in JSON spec + if (strchr("\"\\/\b\f\n\r\t", c) != NULL) { + ss << '\\'; + } + ss << c; + } ss << "\""; } |