Browse Source

[4540] Added hexadecimal support for integer options

Francis Dupont 8 years ago
parent
commit
814b007edf

+ 11 - 3
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 <boost/algorithm/string/classification.hpp>
 #include <boost/algorithm/string/predicate.hpp>
 #include <boost/dynamic_bitset.hpp>
+#include <sstream>
 
 using namespace std;
 using namespace isc::util;
@@ -489,8 +490,15 @@ OptionDefinition::lexicalCastWithRangeCheck(const std::string& value_str)
         result = boost::lexical_cast<int64_t>(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<T>::integer_type) {

+ 2 - 2
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.

+ 2 - 1
src/lib/dhcp/tests/option_definition_unittest.cc

@@ -1190,7 +1190,8 @@ TEST_F(OptionDefinitionTest, uint32ArrayTokenized) {
     OptionPtr option_v6;
     std::vector<std::string> 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");