From 8270f9299fd8d3dbf658200f7f43bede170bfe71 Mon Sep 17 00:00:00 2001 From: Maxime Coste Date: Mon, 4 Jul 2016 19:31:09 +0100 Subject: [PATCH] Improve robustness of json parsing and execution Fixes #720 --- src/json_ui.cc | 11 ++++------- 1 file changed, 4 insertions(+), 7 deletions(-) diff --git a/src/json_ui.cc b/src/json_ui.cc index c7829b1b..8ce6aff5 100644 --- a/src/json_ui.cc +++ b/src/json_ui.cc @@ -360,6 +360,9 @@ parse_json(StringView json) { return parse_json(json.begin(), json.end()); } void JsonUI::eval_json(const Value& json) { + if (not json.is_a()) + throw runtime_error("json request is not an object"); + const JsonObject& object = json.as(); auto json_it = object.find("jsonrpc"); if (json_it == object.end() or json_it->value.as() != "2.0") @@ -436,14 +439,8 @@ void JsonUI::parse_requests(EventMode mode) { write_stderr(format("error while handling requests '{}': '{}'", m_requests, error.what())); - // try to salvage request by dropping its first line - auto eol = find(m_requests, '\n'); - if (eol != m_requests.end()) - m_requests = String{eol+1, m_requests.end()}; - else - m_requests = String{}; - + pos = std::min(m_requests.end(), find(m_requests, '\n')+1); } if (pos) m_requests = String{pos, m_requests.end()};