]> granicus.if.org Git - icinga2/blobdiff - lib/base/serializer.cpp
Add missing lock in DeserializeObject().
[icinga2] / lib / base / serializer.cpp
index 449222ce68940902cd35809658d94b3c0b27dcb2..88c2ad7ac669229edb12534d2a86eb0950cff853 100644 (file)
@@ -1,6 +1,6 @@
 /******************************************************************************
 * Icinga 2                                                                   *
-* Copyright (C) 2012-2013 Icinga Development Team (http://www.icinga.org/)   *
+* Copyright (C) 2012-2014 Icinga Development Team (http://www.icinga.org)    *
 *                                                                            *
 * This program is free software; you can redistribute it and/or              *
 * modify it under the terms of the GNU General Public License                *
@@ -22,7 +22,6 @@
 #include "base/application.h"
 #include "base/objectlock.h"
 #include <boost/foreach.hpp>
-#include <boost/tuple/tuple.hpp>
 #include <cJSON.h>
 
 using namespace icinga;
@@ -90,10 +89,8 @@ static Dictionary::Ptr SerializeDictionary(const Dictionary::Ptr& input, int att
 
        ObjectLock olock(input);
 
-       String key;
-       Value value;
-       BOOST_FOREACH(boost::tie(key, value), input) {
-               result->Set(key, Serialize(value, attributeTypes));
+       BOOST_FOREACH(const Dictionary::Pair& kv, input) {
+               result->Set(kv.first, Serialize(kv.second, attributeTypes));
        }
 
        return result;
@@ -116,69 +113,77 @@ static Object::Ptr SerializeObject(const Object::Ptr& input, int attributeTypes)
                fields->Set(field.Name, Serialize(input->GetField(i), attributeTypes));
        }
 
-       fields->Set("__type", type->GetName());
+       fields->Set("type", type->GetName());
 
        return fields;
 }
 
-static Array::Ptr DeserializeArray(const Array::Ptr& input, int attributeTypes)
+static Array::Ptr DeserializeArray(const Array::Ptr& input, bool safe_mode, int attributeTypes)
 {
        Array::Ptr result = make_shared<Array>();
 
        ObjectLock olock(input);
 
        BOOST_FOREACH(const Value& value, input) {
-               result->Add(Deserialize(value, attributeTypes));
+               result->Add(Deserialize(value, safe_mode, attributeTypes));
        }
 
        return result;
 }
 
-static Dictionary::Ptr DeserializeDictionary(const Dictionary::Ptr& input, int attributeTypes)
+static Dictionary::Ptr DeserializeDictionary(const Dictionary::Ptr& input, bool safe_mode, int attributeTypes)
 {
        Dictionary::Ptr result = make_shared<Dictionary>();
 
        ObjectLock olock(input);
 
-       String key;
-       Value value;
-       BOOST_FOREACH(boost::tie(key, value), input) {
-               result->Set(key, Deserialize(value, attributeTypes));
+       BOOST_FOREACH(const Dictionary::Pair& kv, input) {
+               result->Set(kv.first, Deserialize(kv.second, attributeTypes));
        }
 
        return result;
 }
 
-static Object::Ptr DeserializeObject(const Object::Ptr& object, const Dictionary::Ptr& input, int attributeTypes)
+static Object::Ptr DeserializeObject(const Object::Ptr& object, const Dictionary::Ptr& input, bool safe_mode, int attributeTypes)
 {
        const Type *type;
 
        if (object)
                type = object->GetReflectionType();
        else
-               type = Type::GetByName(input->Get("__type"));
+               type = Type::GetByName(input->Get("type"));
 
        if (!type)
                return object;
 
        Object::Ptr instance = object;
 
-       if (!instance)
+       if (!instance) {
+               if (safe_mode && !type->IsSafe())
+                       BOOST_THROW_EXCEPTION(std::runtime_error("Tried to instantiate type '" + type->GetName() + "' which is not marked as safe."));
+
                instance = type->Instantiate();
+       }
 
-       for (int i = 0; i < type->GetFieldCount(); i++) {
-               Field field = type->GetFieldInfo(i);
+       ObjectLock olock(input);
+       BOOST_FOREACH(const Dictionary::Pair& kv, input) {
+               if (kv.first.IsEmpty())
+                       continue;
 
-               if ((field.Attributes & attributeTypes) == 0)
+               int fid = type->GetFieldId(kv.first);
+       
+               if (fid < 0)
                        continue;
 
-               if (!input->Contains(field.Name))
+               Field field = type->GetFieldInfo(fid);
+
+               if ((field.Attributes & attributeTypes) == 0)
                        continue;
 
                try {
-                       instance->SetField(i, Deserialize(input->Get(field.Name), attributeTypes));
+                       instance->SetField(fid, Deserialize(kv.second, safe_mode, attributeTypes));
                } catch (const std::exception&) {
-                       instance->SetField(i, Empty);
+                       instance->SetField(fid, Empty);
                }
        }
 
@@ -205,12 +210,12 @@ Value icinga::Serialize(const Value& value, int attributeTypes)
        return SerializeObject(input, attributeTypes);
 }
 
-Value icinga::Deserialize(const Value& value, int attributeTypes)
+Value icinga::Deserialize(const Value& value, bool safe_mode, int attributeTypes)
 {
-       return Deserialize(Object::Ptr(), value, attributeTypes);
+       return Deserialize(Object::Ptr(), value, safe_mode, attributeTypes);
 }
 
-Value icinga::Deserialize(const Object::Ptr& object, const Value& value, int attributeTypes)
+Value icinga::Deserialize(const Object::Ptr& object, const Value& value, bool safe_mode, int attributeTypes)
 {
        if (!value.IsObject())
                return value;
@@ -220,14 +225,14 @@ Value icinga::Deserialize(const Object::Ptr& object, const Value& value, int att
        Array::Ptr array = dynamic_pointer_cast<Array>(input);
 
        if (array != NULL)
-               return DeserializeArray(array, attributeTypes);
+               return DeserializeArray(array, safe_mode, attributeTypes);
 
        Dictionary::Ptr dict = dynamic_pointer_cast<Dictionary>(input);
 
        ASSERT(dict != NULL);
 
-       if (!dict->Contains("__type"))
-               return DeserializeDictionary(dict, attributeTypes);
+       if (!dict->Contains("type"))
+               return DeserializeDictionary(dict, safe_mode, attributeTypes);
 
-       return DeserializeObject(object, dict, attributeTypes);
+       return DeserializeObject(object, dict, safe_mode, attributeTypes);
 }