From a16f0812ed167378199adeaa5c30f938d49df0bc Mon Sep 17 00:00:00 2001 From: Mateusz Malicki Date: Fri, 9 Jan 2015 16:23:19 +0100 Subject: [PATCH] Prevent for writing unset union [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 | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/src/config/fields-union.hpp b/src/config/fields-union.hpp index 53620ca..ec4ea3c 100644 --- a/src/config/fields-union.hpp +++ b/src/config/fields-union.hpp @@ -26,6 +26,8 @@ #define CONFIG_FIELDS_UNION_HPP #include "config/fields.hpp" +#include "config/exception.hpp" + #include #include #include @@ -104,10 +106,12 @@ private: template \ void visitOption(Visitor& v, const std::string& name) { \ GENERATE_CODE(GENERATE_UNION_VISIT__, __VA_ARGS__) \ + throw config::ConfigException("Union type error. Unsupported type"); \ } \ template \ 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 \ 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 \ @@ -159,6 +160,7 @@ public: template \ Type& set(const Type& src) { \ getHolder() = std::forward(src); \ + assert(!getOptionName().empty() && "Unsupported type"); \ return as(); \ } \ @@ -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()); \ + return; \ } #define GENERATE_UNION_NAME__(r, _, TYPE_) \ -- 2.7.4