src: remove invalid casts in options parser

Fixes: https://github.com/nodejs/node/issues/26131

PR-URL: https://github.com/nodejs/node/pull/26139
Reviewed-By: Yang Guo <yangguo@chromium.org>
Reviewed-By: Minwoo Jung <minwoo@nodesource.com>
Reviewed-By: Joyee Cheung <joyeec9h3@gmail.com>
This commit is contained in:
Anna Henningsen 2019-02-15 19:42:03 +01:00
parent 45b7c98f09
commit 35e6070f14
No known key found for this signature in database
GPG Key ID: 9C63F3A6CD2AD8F9
2 changed files with 13 additions and 18 deletions

View File

@ -148,7 +148,7 @@ void OptionsParser<Options>::Implies(const std::string& from,
CHECK_NE(it, options_.end()); CHECK_NE(it, options_.end());
CHECK_EQ(it->second.type, kBoolean); CHECK_EQ(it->second.type, kBoolean);
implications_.emplace(from, Implication { implications_.emplace(from, Implication {
std::static_pointer_cast<OptionField<bool>>(it->second.field), true it->second.field, true
}); });
} }
@ -159,7 +159,7 @@ void OptionsParser<Options>::ImpliesNot(const std::string& from,
CHECK_NE(it, options_.end()); CHECK_NE(it, options_.end());
CHECK_EQ(it->second.type, kBoolean); CHECK_EQ(it->second.type, kBoolean);
implications_.emplace(from, Implication { implications_.emplace(from, Implication {
std::static_pointer_cast<OptionField<bool>>(it->second.field), false it->second.field, false
}); });
} }
@ -205,8 +205,7 @@ auto OptionsParser<Options>::Convert(
typename OptionsParser<ChildOptions>::Implication original, typename OptionsParser<ChildOptions>::Implication original,
ChildOptions* (Options::* get_child)()) { ChildOptions* (Options::* get_child)()) {
return Implication { return Implication {
std::static_pointer_cast<OptionField<bool>>( Convert(original.target_field, get_child),
Convert(original.target_field, get_child)),
original.target_value original.target_value
}; };
} }
@ -378,8 +377,10 @@ void OptionsParser<Options>::Parse(
{ {
auto implications = implications_.equal_range(name); auto implications = implications_.equal_range(name);
for (auto it = implications.first; it != implications.second; ++it) for (auto it = implications.first; it != implications.second; ++it) {
*it->second.target_field->Lookup(options) = it->second.target_value; *it->second.target_field->template Lookup<bool>(options) =
it->second.target_value;
}
} }
const OptionInfo& info = it->second; const OptionInfo& info = it->second;

View File

@ -326,23 +326,17 @@ class OptionsParser {
public: public:
virtual ~BaseOptionField() {} virtual ~BaseOptionField() {}
virtual void* LookupImpl(Options* options) const = 0; virtual void* LookupImpl(Options* options) const = 0;
};
// Represents a field of type T within `Options`.
template <typename T> template <typename T>
class OptionField : public BaseOptionField { inline T* Lookup(Options* options) const {
public: return static_cast<T*>(LookupImpl(options));
typedef T Type;
T* Lookup(Options* options) const {
return static_cast<T*>(this->LookupImpl(options));
} }
}; };
// Represents a field of type T within `Options` that can be looked up // Represents a field of type T within `Options` that can be looked up
// as a C++ member field. // as a C++ member field.
template <typename T> template <typename T>
class SimpleOptionField : public OptionField<T> { class SimpleOptionField : public BaseOptionField {
public: public:
explicit SimpleOptionField(T Options::* field) : field_(field) {} explicit SimpleOptionField(T Options::* field) : field_(field) {}
void* LookupImpl(Options* options) const override { void* LookupImpl(Options* options) const override {
@ -356,7 +350,7 @@ class OptionsParser {
template <typename T> template <typename T>
inline T* Lookup(std::shared_ptr<BaseOptionField> field, inline T* Lookup(std::shared_ptr<BaseOptionField> field,
Options* options) const { Options* options) const {
return std::static_pointer_cast<OptionField<T>>(field)->Lookup(options); return field->template Lookup<T>(options);
} }
// An option consists of: // An option consists of:
@ -373,7 +367,7 @@ class OptionsParser {
// An implied option is composed of the information on where to store a // An implied option is composed of the information on where to store a
// specific boolean value (if another specific option is encountered). // specific boolean value (if another specific option is encountered).
struct Implication { struct Implication {
std::shared_ptr<OptionField<bool>> target_field; std::shared_ptr<BaseOptionField> target_field;
bool target_value; bool target_value;
}; };