From: Christian Hofstaedtler Date: Wed, 26 Feb 2014 09:15:59 +0000 (+0100) Subject: JSON API: Catch wrong container types X-Git-Tag: rec-3.6.0-rc1~160^2~1 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=2b7c19865a7c9fcd9007af0523e8bc378a4c898e;p=thirdparty%2Fpdns.git JSON API: Catch wrong container types Avoids an assert/abort in rapidjson. --- diff --git a/pdns/json.cc b/pdns/json.cc index b60cf2e3a2..1ae341a022 100644 --- a/pdns/json.cc +++ b/pdns/json.cc @@ -10,6 +10,9 @@ using namespace rapidjson; int intFromJson(const Value& container, const char* key) { + if (!container.IsObject()) { + throw JsonException("Container was not an object."); + } const Value& val = container[key]; if (val.IsInt()) { return val.GetInt(); @@ -22,6 +25,9 @@ int intFromJson(const Value& container, const char* key) int intFromJson(const Value& container, const char* key, const int default_value) { + if (!container.IsObject()) { + throw JsonException("Container was not an object."); + } const Value& val = container[key]; if (val.IsInt()) { return val.GetInt(); @@ -35,6 +41,9 @@ int intFromJson(const Value& container, const char* key, const int default_value string stringFromJson(const Value& container, const char* key) { + if (!container.IsObject()) { + throw JsonException("Container was not an object."); + } const Value& val = container[key]; if (val.IsString()) { return val.GetString(); @@ -45,6 +54,9 @@ string stringFromJson(const Value& container, const char* key) string stringFromJson(const Value& container, const char* key, const string& default_value) { + if (!container.IsObject()) { + throw JsonException("Container was not an object."); + } const Value& val = container[key]; if (val.IsString()) { return val.GetString(); @@ -56,6 +68,9 @@ string stringFromJson(const Value& container, const char* key, const string& def bool boolFromJson(const rapidjson::Value& container, const char* key) { + if (!container.IsObject()) { + throw JsonException("Container was not an object."); + } const Value& val = container[key]; if (val.IsBool()) { return val.GetBool();