From 2b7c19865a7c9fcd9007af0523e8bc378a4c898e Mon Sep 17 00:00:00 2001 From: Christian Hofstaedtler Date: Wed, 26 Feb 2014 10:15:59 +0100 Subject: [PATCH] JSON API: Catch wrong container types Avoids an assert/abort in rapidjson. --- pdns/json.cc | 15 +++++++++++++++ 1 file changed, 15 insertions(+) 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(); -- 2.47.3