Improve robustness of json parsing and execution

Fixes #720
This commit is contained in:
Maxime Coste 2016-07-04 19:31:09 +01:00
parent 469eb6ec44
commit 8270f9299f

View File

@ -360,6 +360,9 @@ parse_json(StringView json) { return parse_json(json.begin(), json.end()); }
void JsonUI::eval_json(const Value& json) void JsonUI::eval_json(const Value& json)
{ {
if (not json.is_a<JsonObject>())
throw runtime_error("json request is not an object");
const JsonObject& object = json.as<JsonObject>(); const JsonObject& object = json.as<JsonObject>();
auto json_it = object.find("jsonrpc"); auto json_it = object.find("jsonrpc");
if (json_it == object.end() or json_it->value.as<String>() != "2.0") if (json_it == object.end() or json_it->value.as<String>() != "2.0")
@ -436,14 +439,8 @@ void JsonUI::parse_requests(EventMode mode)
{ {
write_stderr(format("error while handling requests '{}': '{}'", write_stderr(format("error while handling requests '{}': '{}'",
m_requests, error.what())); m_requests, error.what()));
// try to salvage request by dropping its first line // try to salvage request by dropping its first line
auto eol = find(m_requests, '\n'); pos = std::min(m_requests.end(), find(m_requests, '\n')+1);
if (eol != m_requests.end())
m_requests = String{eol+1, m_requests.end()};
else
m_requests = String{};
} }
if (pos) if (pos)
m_requests = String{pos, m_requests.end()}; m_requests = String{pos, m_requests.end()};