From: Mukund Sivaraman Date: Mon, 17 Dec 2012 06:20:05 +0000 (+0530) Subject: [2565] Refactor code so that RRParamRegistry::textToClassCode() does not throw X-Git-Tag: bind10-1.0.0-rc-release~95^2~21^2~26 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=ed7622df90ee865b5be03b0d13ed5977466bebfc;p=thirdparty%2Fkea.git [2565] Refactor code so that RRParamRegistry::textToClassCode() does not throw --- diff --git a/src/lib/dns/rrclass.cc b/src/lib/dns/rrclass.cc index ac5823cf62..cc5ba481ff 100644 --- a/src/lib/dns/rrclass.cc +++ b/src/lib/dns/rrclass.cc @@ -31,7 +31,11 @@ namespace isc { namespace dns { RRClass::RRClass(const std::string& classstr) { - classcode_ = RRParamRegistry::getRegistry().textToClassCode(classstr); + if (!RRParamRegistry::getRegistry().textToClassCode(classstr, + classcode_)) { + isc_throw(InvalidRRClass, + "Unrecognized RR parameter string: " + classstr); + } } RRClass::RRClass(InputBuffer& buffer) { diff --git a/src/lib/dns/rrparamregistry-placeholder.cc b/src/lib/dns/rrparamregistry-placeholder.cc index 16ec23cbf9..9729d86eee 100644 --- a/src/lib/dns/rrparamregistry-placeholder.cc +++ b/src/lib/dns/rrparamregistry-placeholder.cc @@ -421,14 +421,15 @@ removeParam(uint16_t code, MC& codemap, MS& stringmap) { return (false); } -template -inline uint16_t -textToCode(const string& code_str, MS& stringmap) { +template +inline bool +textToCode(const string& code_str, MS& stringmap, uint16_t& class_code) { typename MS::const_iterator found; found = stringmap.find(code_str); if (found != stringmap.end()) { - return (found->second->code_); + class_code = found->second->code_; + return (true); } size_t l = code_str.size(); @@ -441,10 +442,12 @@ textToCode(const string& code_str, MS& stringmap) { l - PT::UNKNOWN_PREFIXLEN())); iss >> dec >> code; if (iss.rdstate() == ios::eofbit && code <= PT::MAX_CODE) { - return (code); + class_code = code; + return (true); } } - isc_throw(ET, "Unrecognized RR parameter string: " + code_str); + + return (false); } template @@ -477,8 +480,15 @@ RRParamRegistry::removeType(uint16_t code) { uint16_t RRParamRegistry::textToTypeCode(const string& type_string) const { - return (textToCode(type_string, impl_->str2typemap)); + uint16_t code; + + if (!textToCode + (type_string, impl_->str2typemap, code)) { + isc_throw(InvalidRRType, + "Unrecognized RR parameter string: " + type_string); + } + + return (code); } string @@ -499,10 +509,12 @@ RRParamRegistry::removeClass(uint16_t code) { impl_->str2classmap)); } -uint16_t -RRParamRegistry::textToClassCode(const string& class_string) const { - return (textToCode(class_string, impl_->str2classmap)); +bool +RRParamRegistry::textToClassCode(const string& class_string, + uint16_t& class_code) const +{ + return (textToCode + (class_string, impl_->str2classmap, class_code)); } string diff --git a/src/lib/dns/rrparamregistry.h b/src/lib/dns/rrparamregistry.h index 56ae981614..6924e05220 100644 --- a/src/lib/dns/rrparamregistry.h +++ b/src/lib/dns/rrparamregistry.h @@ -415,16 +415,20 @@ public: /// \brief Convert a textual representation of an RR class to the /// corresponding 16-bit integer code. /// - /// This method searches the \c RRParamRegistry for the mapping from the - /// given textual representation of RR class to the corresponding integer - /// code. If a mapping is found, it returns the associated class code; - /// otherwise, if the given string is in the form of "CLASSnnnn", it returns - /// the corresponding number as the class code; otherwise, it throws an - /// exception of class \c InvalidRRClass. + /// This method searches the \c RRParamRegistry for the mapping from + /// the given textual representation of RR class to the + /// corresponding integer code. If a mapping is found, it returns + /// true with the associated class code in \c class_code; otherwise, + /// if the given string is in the form of "CLASSnnnn", it returns + /// true with the corresponding number as the class code in \c + /// class_code; otherwise, it returns false and \c class_code is + /// untouched. /// /// \param class_string The textual representation of the RR class. - /// \return The RR class code for \c class_string. - uint16_t textToClassCode(const std::string& class_string) const; + /// \param class_code Returns the RR class code in this argument. + /// \return true if conversion is successful, false otherwise. + bool textToClassCode(const std::string& class_string, + uint16_t& class_code) const; /// \brief Convert class code into its textual representation. ///