From fe04f0f8051f20ade7053d2e32ec63669d689699 Mon Sep 17 00:00:00 2001 From: Varun Talwar Date: Mon, 24 Jul 2017 15:33:25 -0700 Subject: [PATCH] check for valid json object before parsing Change-Id: If9b6ff9f0aeb8fdd88f7d87eadcfa46245cede09 --- src/mapzen/mapzen_jsonparser.cpp | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/src/mapzen/mapzen_jsonparser.cpp b/src/mapzen/mapzen_jsonparser.cpp index 7cc3257..6293136 100644 --- a/src/mapzen/mapzen_jsonparser.cpp +++ b/src/mapzen/mapzen_jsonparser.cpp @@ -20,6 +20,8 @@ #include "mapzen_jsonparser.hpp" #include "rapidjson/document.h" +#include "rapidjson/error/error.h" +#include "rapidjson/error/en.h" extern "C" { #include "mapzen_queue.h" @@ -159,6 +161,12 @@ static void __parse_geocode_response(char *response, int size, int *status, GLis //crack open that json rapidjson::Document document; document.Parse(std::string(response, size).c_str()); + + if (document.HasParseError()) { + MAP_DEBUG("Error Parsing JSON Response: %s (%u)", GetParseError_En(document.GetParseError()), document.GetErrorOffset()); + return; + } + rapidjson::Value::ConstMemberIterator features = document.FindMember("features"); if(features != document.MemberEnd() && features->value.IsArray()) { //for each feature @@ -204,6 +212,12 @@ static void __parse_revgeocode_response(char *response, int size, int *status, m //crack open that json rapidjson::Document document; document.Parse(std::string(response, size).c_str()); + + if (document.HasParseError()) { + MAP_DEBUG("Error Parsing JSON Response: %s (%u)", GetParseError_En(document.GetParseError()), document.GetErrorOffset()); + return; + } + rapidjson::Value::ConstMemberIterator features = document.FindMember("features"); if(features != document.MemberEnd() && features->value.IsArray()) { //for each feature @@ -268,6 +282,12 @@ static void __parse_place_response(char *response, int size, GList **placeList) //crack open that json rapidjson::Document document; document.Parse(std::string(response, size).c_str()); + + if (document.HasParseError()) { + MAP_DEBUG("Error Parsing JSON Response: %s (%u)", GetParseError_En(document.GetParseError()), document.GetErrorOffset()); + return; + } + rapidjson::Value::ConstMemberIterator features = document.FindMember("features"); if(features != document.MemberEnd() && features->value.IsArray()) { //for each feature @@ -520,6 +540,12 @@ static void __parse_route_response(char *response, int size, int *status, mapzen rapidjson::Document document; document.Parse(std::string(response, size).c_str()); + + if (document.HasParseError()) { + MAP_DEBUG("Error Parsing JSON Response: %s (%u)", GetParseError_En(document.GetParseError()), document.GetErrorOffset()); + return; + } + rapidjson::Value::MemberIterator trip = document.FindMember("trip"); if (trip == document.MemberEnd()) { MAP_DEBUG(">>>>> PROCESS __parse_route_response: trip object not found in response"); -- 2.7.4