Add protection against memory leaking during deserialization 05/184505/5
authorTomasz Swierczek <t.swierczek@samsung.com>
Tue, 17 Jul 2018 12:14:18 +0000 (14:14 +0200)
committerDariusz Michaluk <d.michaluk@samsung.com>
Thu, 16 Aug 2018 08:39:49 +0000 (08:39 +0000)
Change-Id: I1fbcd7daf1674dd1ad6b9eaffdba76263bda370b

src/manager/dpl/core/include/dpl/serialization.h

index 5d4ed2b..4448923 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2011 Samsung Electronics Co., Ltd All Rights Reserved
+ * Copyright (c) 2011 - 2018 Samsung Electronics Co., Ltd All Rights Reserved
  *
  *    Licensed under the Apache License, Version 2.0 (the "License");
  *    you may not use this file except in compliance with the License.
@@ -260,6 +260,17 @@ struct Deserialization {
                object = new T(stream);
        }
 
+       // *& deserialization template to simplify rest of the code
+       template <typename T>
+       static inline void DeserializePtr(IStream &stream, T *&value)
+       {
+               T *tmp = new T;
+               std::unique_ptr<T> ptr(tmp);
+               Deserialize(stream, *tmp);
+               ptr.release();
+               value = tmp;
+       }
+
        // char
        static void Deserialize(IStream &stream, char &value)
        {
@@ -267,8 +278,7 @@ struct Deserialization {
        }
        static void Deserialize(IStream &stream, char *&value)
        {
-               value = new char;
-               stream.Read(sizeof(*value), value);
+               DeserializePtr(stream, value);
        }
 
        // unsigned char
@@ -278,8 +288,7 @@ struct Deserialization {
        }
        static void Deserialize(IStream &stream, unsigned char *&value)
        {
-               value = new unsigned char;
-               stream.Read(sizeof(*value), value);
+               DeserializePtr(stream, value);
        }
 
        // unsigned int32
@@ -289,8 +298,7 @@ struct Deserialization {
        }
        static void Deserialize(IStream &stream, uint32_t *&value)
        {
-               value = new uint32_t;
-               stream.Read(sizeof(*value), value);
+               DeserializePtr(stream, value);
        }
 
        // int32
@@ -300,8 +308,7 @@ struct Deserialization {
        }
        static void Deserialize(IStream &stream, int32_t *&value)
        {
-               value = new int32_t;
-               stream.Read(sizeof(*value), value);
+               DeserializePtr(stream, value);
        }
 
        // unsigned int64
@@ -311,8 +318,7 @@ struct Deserialization {
        }
        static void Deserialize(IStream &stream, uint64_t *&value)
        {
-               value = new uint64_t;
-               stream.Read(sizeof(*value), value);
+               DeserializePtr(stream, value);
        }
 
        // int64
@@ -322,8 +328,7 @@ struct Deserialization {
        }
        static void Deserialize(IStream &stream, int64_t *&value)
        {
-               value = new int64_t;
-               stream.Read(sizeof(*value), value);
+               DeserializePtr(stream, value);
        }
 
        // bool
@@ -333,8 +338,7 @@ struct Deserialization {
        }
        static void Deserialize(IStream &stream, bool *&value)
        {
-               value = new bool;
-               stream.Read(sizeof(*value), value);
+               DeserializePtr(stream, value);
        }
 
        template <typename T, typename R, typename A>
@@ -375,8 +379,7 @@ struct Deserialization {
        template <typename T>
        static void Deserialize(IStream &stream, std::list<T> *&list)
        {
-               list = new std::list<T>;
-               Deserialize(stream, *list);
+               DeserializePtr(stream, list);
        }
 
        // RawBuffer
@@ -392,8 +395,7 @@ struct Deserialization {
        template <typename A>
        static void Deserialize(IStream &stream, std::vector<unsigned char, A> *&vec)
        {
-               vec = new std::vector<unsigned char, A>;
-               Deserialize(stream, *vec);
+               DeserializePtr<std::vector<unsigned char, A>>(stream, vec);
        }
 
        // std::vector
@@ -412,8 +414,7 @@ struct Deserialization {
        template <typename T, typename A>
        static void Deserialize(IStream &stream, std::vector<T, A> *&vec)
        {
-               vec = new std::vector<T, A>;
-               Deserialize(stream, *vec);
+               DeserializePtr(stream, vec);
        }
 
        // std::pair
@@ -426,8 +427,7 @@ struct Deserialization {
        template <typename A, typename B>
        static void Deserialize(IStream &stream, std::pair<A, B> *&p)
        {
-               p = new std::pair<A, B>;
-               Deserialize(stream, *p);
+               DeserializePtr(stream, p);
        }
 
        // std::map
@@ -448,8 +448,7 @@ struct Deserialization {
        template <typename K, typename T>
        static void Deserialize(IStream &stream, std::map<K, T> *&map)
        {
-               map = new std::map<K, T>;
-               Deserialize(stream, *map);
+               DeserializePtr(stream, map);
        }
 }; // struct Deserialization
 
@@ -486,6 +485,17 @@ struct Deserializer<First, Args...> : public Deserializer<Args...> {
                Deserialization::Deserialize(stream, f);
                Deserializer<Args...>::Deserialize(stream, args...);
        }
+
+       static void Deserialize(IStream &stream, First *&f, Args &... args)
+       {
+               First *tmp = NULL;
+               Deserialization::Deserialize(stream, tmp);
+               std::unique_ptr<First> ptr(tmp);
+               Deserializer<Args...>::Deserialize(stream, args...);
+               ptr.release();
+               f = tmp;
+       }
+
 };
 
 // end of recursion