Prevent for writing unset union 47/33447/1
authorMateusz Malicki <m.malicki2@samsung.com>
Fri, 9 Jan 2015 15:23:19 +0000 (16:23 +0100)
committerMateusz Malicki <m.malicki2@samsung.com>
Fri, 9 Jan 2015 15:34:32 +0000 (16:34 +0100)
[Bug]           Unset can be written to file (can't be read)
[Cause]         N/A
[Solution]      Assert in set and accept function,
                throw config::ConfigException when unsupported type is
                read or writen
[Verification]  N/A

Change-Id: I5e30d53fe64a9fb132178c1f191af5a5910336d4

src/config/fields-union.hpp

index 53620ca..ec4ea3c 100644 (file)
@@ -26,6 +26,8 @@
 #define CONFIG_FIELDS_UNION_HPP
 
 #include "config/fields.hpp"
+#include "config/exception.hpp"
+
 #include <utility>
 #include <boost/any.hpp>
 #include <cassert>
@@ -104,10 +106,12 @@ private:
     template<typename Visitor>                                                                  \
     void visitOption(Visitor& v, const std::string& name) {                                     \
         GENERATE_CODE(GENERATE_UNION_VISIT__, __VA_ARGS__)                                      \
+        throw config::ConfigException("Union type error. Unsupported type");                    \
     }                                                                                           \
     template<typename Visitor>                                                                  \
     void visitOption(Visitor& v, const std::string& name) const {                               \
         GENERATE_CODE(GENERATE_UNION_VISIT_CONST__, __VA_ARGS__)                                \
+        throw config::ConfigException("Union type error. Unsupported type");                    \
     }                                                                                           \
     std::string getOptionName() const {                                                         \
         GENERATE_CODE(GENERATE_UNION_NAME__, __VA_ARGS__)                                       \
@@ -131,12 +135,9 @@ public:
     template<typename Visitor>                                                                  \
     void accept(Visitor v) const {                                                              \
         const std::string name = getOptionName();                                               \
-        if (!name.empty()) {                                                                    \
-            v.visit("type", name);                                                              \
-            visitOption(v, name);                                                               \
-        } else {                                                                                \
-           /* Unsupported type */                                                               \
-        }                                                                                       \
+        assert(!name.empty() && "Type is not set");                                             \
+        v.visit("type", name);                                                                  \
+        visitOption(v, name);                                                                   \
     }                                                                                           \
                                                                                                 \
     template<typename Type>                                                                     \
@@ -159,6 +160,7 @@ public:
     template<typename Type>                                                                     \
     Type& set(const Type& src) {                                                                \
         getHolder() = std::forward<const Type>(src);                                            \
+        assert(!getOptionName().empty() && "Unsupported type");                                 \
         return as<Type>();                                                                      \
     }                                                                                           \
 
@@ -168,11 +170,13 @@ public:
 #define GENERATE_UNION_VISIT__(r, _, TYPE_)                                                     \
     if (#TYPE_ == name) {                                                                       \
         v.visit("value", set(std::move(TYPE_())));                                              \
+        return;                                                                                 \
     }
 
 #define GENERATE_UNION_VISIT_CONST__(r, _, TYPE_)                                               \
     if (#TYPE_ == name) {                                                                       \
         v.visit("value", as<const TYPE_>());                                                    \
+        return;                                                                                 \
     }
 
 #define GENERATE_UNION_NAME__(r, _, TYPE_)                                                      \