From: Francis Dupont Date: Thu, 12 Jan 2017 13:07:56 +0000 (+0100) Subject: [4540] Added hexadecimal support for integer options X-Git-Tag: trac5087_base^2~2 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=814b007edf904bb0d47b597b9f71892f25c08e07;p=thirdparty%2Fkea.git [4540] Added hexadecimal support for integer options --- diff --git a/src/lib/dhcp/option_definition.cc b/src/lib/dhcp/option_definition.cc index aa2d949f2c..50ae169d90 100644 --- a/src/lib/dhcp/option_definition.cc +++ b/src/lib/dhcp/option_definition.cc @@ -1,4 +1,4 @@ -// Copyright (C) 2012-2016 Internet Systems Consortium, Inc. ("ISC") +// Copyright (C) 2012-2017 Internet Systems Consortium, Inc. ("ISC") // // This Source Code Form is subject to the terms of the Mozilla Public // License, v. 2.0. If a copy of the MPL was not distributed with this @@ -29,6 +29,7 @@ #include #include #include +#include using namespace std; using namespace isc::util; @@ -489,8 +490,15 @@ OptionDefinition::lexicalCastWithRangeCheck(const std::string& value_str) result = boost::lexical_cast(value_str); } catch (const boost::bad_lexical_cast&) { - isc_throw(BadDataTypeCast, "unable to convert the value '" - << value_str << "' to integer data type"); + // boost::lexical_cast does not handle hexadecimal + // but stringstream does so do it the hard way. + std::stringstream ss; + ss << std::hex << value_str; + ss >> result; + if (ss.fail() || !ss.eof()) { + isc_throw(BadDataTypeCast, "unable to convert the value '" + << value_str << "' to integer data type"); + } } // Perform range checks. if (OptionDataTypeTraits::integer_type) { diff --git a/src/lib/dhcp/option_definition.h b/src/lib/dhcp/option_definition.h index b90f5d5a79..efd7afd584 100644 --- a/src/lib/dhcp/option_definition.h +++ b/src/lib/dhcp/option_definition.h @@ -1,4 +1,4 @@ -// Copyright (C) 2012-2016 Internet Systems Consortium, Inc. ("ISC") +// Copyright (C) 2012-2017 Internet Systems Consortium, Inc. ("ISC") // // This Source Code Form is subject to the terms of the Mozilla Public // License, v. 2.0. If a copy of the MPL was not distributed with this @@ -624,7 +624,7 @@ private: /// This function performs lexical cast of a string value to integer /// value and checks if the resulting value is within a range of a /// target type. The target type should be one of the supported - /// integer types. + /// integer types. Hexadecimal input is supported. /// /// @param value_str input value given as string. /// @tparam T target integer type for lexical cast. diff --git a/src/lib/dhcp/tests/option_definition_unittest.cc b/src/lib/dhcp/tests/option_definition_unittest.cc index fbb67a9a3e..bed5d9a98f 100644 --- a/src/lib/dhcp/tests/option_definition_unittest.cc +++ b/src/lib/dhcp/tests/option_definition_unittest.cc @@ -1190,7 +1190,8 @@ TEST_F(OptionDefinitionTest, uint32ArrayTokenized) { OptionPtr option_v6; std::vector str_values; str_values.push_back("123456"); - str_values.push_back("7"); + // Try with hexadecimal + str_values.push_back("0x7"); str_values.push_back("256"); str_values.push_back("1111");