Fix: Crashing when writing unset union type 65/33665/2
authorMateusz Malicki <m.malicki2@samsung.com>
Tue, 13 Jan 2015 14:03:39 +0000 (15:03 +0100)
committerMateusz Malicki <m.malicki2@samsung.com>
Wed, 14 Jan 2015 08:24:05 +0000 (09:24 +0100)
[Bug]           Assert instead exception
[Cause]         Assert
[Solution]      Assert was removed
[Verification]  Build in debug mode, run test

Change-Id: I93e9a481d008ea50882b7886f0a63c9f6136bbd0

src/config/fields-union.hpp

index ec4ea3c..d37791d 100644 (file)
@@ -30,7 +30,6 @@
 
 #include <utility>
 #include <boost/any.hpp>
-#include <cassert>
 
 /**
  * Use this macro to declare and register config fields
@@ -135,7 +134,9 @@ public:
     template<typename Visitor>                                                                  \
     void accept(Visitor v) const {                                                              \
         const std::string name = getOptionName();                                               \
-        assert(!name.empty() && "Type is not set");                                             \
+        if (name.empty()) {                                                                     \
+           throw config::ConfigException("Type is not set");                                    \
+        }                                                                                       \
         v.visit("type", name);                                                                  \
         visitOption(v, name);                                                                   \
     }                                                                                           \
@@ -146,12 +147,16 @@ public:
     }                                                                                           \
     template<typename Type>                                                                     \
     typename std::enable_if<!std::is_const<Type>::value, Type>::type& as() {                    \
-        assert(!getHolder().empty());                                                           \
+        if (getHolder().empty()) {                                                              \
+            throw config::ConfigException("Type is not set");                                   \
+        }                                                                                       \
         return boost::any_cast<Type&>(getHolder());                                             \
     }                                                                                           \
     template<typename Type>                                                                     \
     const Type& as() const {                                                                    \
-        assert(!getHolder().empty());                                                           \
+        if (getHolder().empty()) {                                                              \
+            throw config::ConfigException("Type is not set");                                   \
+        }                                                                                       \
         return boost::any_cast<const Type&>(getHolder());                                       \
     }                                                                                           \
     bool isSet() {                                                                              \
@@ -160,7 +165,9 @@ public:
     template<typename Type>                                                                     \
     Type& set(const Type& src) {                                                                \
         getHolder() = std::forward<const Type>(src);                                            \
-        assert(!getOptionName().empty() && "Unsupported type");                                 \
+        if (getOptionName().empty()) {                                                          \
+           throw config::ConfigException("Unsupported type");                                   \
+        }                                                                                       \
         return as<Type>();                                                                      \
     }                                                                                           \